当前位置:首页 » 密码管理 » vb本机加密

vb本机加密

发布时间: 2023-10-18 23:03:03

1. 用VB实现MD5加密

md5加密运算是不可逆的,就是说不能通过那一串古古怪怪的东西算出它原始的样子。

以下提供VB可用的16位和32位MD5加密函数代码:

PrivateConstBITS_TO_A_BYTE=8
PrivateConstBYTES_TO_A_WORD=4
PrivateConstBITS_TO_A_WORD=32

Privatem_lOnBits(30)
Privatem_l2Power(30)

PrivateFunctionLShift(lValue,iShiftBits)
IfiShiftBits=0Then
LShift=lValue
ExitFunction
ElseIfiShiftBits=31Then
IflValueAnd1Then
LShift=&H80000000
Else
LShift=0
EndIf
ExitFunction
ElseIfiShiftBits<0OriShiftBits>31Then
Err.Raise6
EndIf

If(lValueAndm_l2Power(31-iShiftBits))Then
LShift=((lValueAndm_lOnBits(31-(iShiftBits+1)))*m_l2Power(iShiftBits))Or&H80000000
Else
LShift=((lValueAndm_lOnBits(31-iShiftBits))*m_l2Power(iShiftBits))
EndIf
EndFunction

PrivateFunctionRShift(lValue,iShiftBits)
IfiShiftBits=0Then
RShift=lValue
ExitFunction
ElseIfiShiftBits=31Then
IflValueAnd&H80000000Then
RShift=1
Else
RShift=0
EndIf
ExitFunction
ElseIfiShiftBits<0OriShiftBits>31Then
Err.Raise6
EndIf

RShift=(lValueAnd&H7FFFFFFE)m_l2Power(iShiftBits)

If(lValueAnd&H80000000)Then
RShift=(RShiftOr(&H40000000m_l2Power(iShiftBits-1)))
EndIf
EndFunction

PrivateFunctionRotateLeft(lValue,iShiftBits)
RotateLeft=LShift(lValue,iShiftBits)OrRShift(lValue,(32-iShiftBits))
EndFunction

PrivateFunctionAddUnsigned(lX,lY)
DimlX4
DimlY4
DimlX8
DimlY8
DimlResult

lX8=lXAnd&H80000000
lY8=lYAnd&H80000000
lX4=lXAnd&H40000000
lY4=lYAnd&H40000000

lResult=(lXAnd&H3FFFFFFF)+(lYAnd&H3FFFFFFF)

IflX4AndlY4Then
lResult=lResultXor&H80000000XorlX8XorlY8
ElseIflX4OrlY4Then
IflResultAnd&H40000000Then
lResult=lResultXor&HC0000000XorlX8XorlY8
Else
lResult=lResultXor&H40000000XorlX8XorlY8
EndIf
Else
lResult=lResultXorlX8XorlY8
EndIf

AddUnsigned=lResult
EndFunction

PrivateFunctionmd5_F(x,y,z)
md5_F=(xAndy)Or((Notx)Andz)
EndFunction

PrivateFunctionmd5_G(x,y,z)
md5_G=(xAndz)Or(yAnd(Notz))
EndFunction

PrivateFunctionmd5_H(x,y,z)
md5_H=(xXoryXorz)
EndFunction

PrivateFunctionmd5_I(x,y,z)
md5_I=(yXor(xOr(Notz)))
EndFunction

PrivateSubmd5_FF(a,b,c,d,x,s,ac)
a=AddUnsigned(a,AddUnsigned(AddUnsigned(md5_F(b,c,d),x),ac))
a=RotateLeft(a,s)
a=AddUnsigned(a,b)
EndSub

PrivateSubmd5_GG(a,b,c,d,x,s,ac)
a=AddUnsigned(a,AddUnsigned(AddUnsigned(md5_G(b,c,d),x),ac))
a=RotateLeft(a,s)
a=AddUnsigned(a,b)
EndSub

PrivateSubmd5_HH(a,b,c,d,x,s,ac)
a=AddUnsigned(a,AddUnsigned(AddUnsigned(md5_H(b,c,d),x),ac))
a=RotateLeft(a,s)
a=AddUnsigned(a,b)
EndSub

PrivateSubmd5_II(a,b,c,d,x,s,ac)
a=AddUnsigned(a,AddUnsigned(AddUnsigned(md5_I(b,c,d),x),ac))
a=RotateLeft(a,s)
a=AddUnsigned(a,b)
EndSub

(sMessage)
DimlMessageLength
DimlNumberOfWords
DimlWordArray()
DimlBytePosition
DimlByteCount
DimlWordCount

ConstMODULUS_BITS=512
ConstCONGRUENT_BITS=448

lMessageLength=Len(sMessage)

lNumberOfWords=(((lMessageLength+((MODULUS_BITS-CONGRUENT_BITS)BITS_TO_A_BYTE))(MODULUS_BITSBITS_TO_A_BYTE))+1)*(MODULUS_BITSBITS_TO_A_WORD)
ReDimlWordArray(lNumberOfWords-1)

lBytePosition=0
lByteCount=0
DoUntillByteCount>=lMessageLength
lWordCount=lByteCountBYTES_TO_A_WORD
lBytePosition=(lByteCountModBYTES_TO_A_WORD)*BITS_TO_A_BYTE
lWordArray(lWordCount)=lWordArray(lWordCount)OrLShift(Asc(Mid(sMessage,lByteCount+1,1)),lBytePosition)
lByteCount=lByteCount+1
Loop

lWordCount=lByteCountBYTES_TO_A_WORD
lBytePosition=(lByteCountModBYTES_TO_A_WORD)*BITS_TO_A_BYTE

lWordArray(lWordCount)=lWordArray(lWordCount)OrLShift(&H80,lBytePosition)

lWordArray(lNumberOfWords-2)=LShift(lMessageLength,3)
lWordArray(lNumberOfWords-1)=RShift(lMessageLength,29)

ConvertToWordArray=lWordArray
EndFunction

PrivateFunctionWordToHex(lValue)
DimlByte
DimlCount

ForlCount=0To3
lByte=RShift(lValue,lCount*BITS_TO_A_BYTE)Andm_lOnBits(BITS_TO_A_BYTE-1)
WordToHex=WordToHex&Right("0"&Hex(lByte),2)
Next
EndFunction

PublicFunctionMD5(sMessage,stype)
m_lOnBits(0)=CLng(1)
m_lOnBits(1)=CLng(3)
m_lOnBits(2)=CLng(7)
m_lOnBits(3)=CLng(15)
m_lOnBits(4)=CLng(31)
m_lOnBits(5)=CLng(63)
m_lOnBits(6)=CLng(127)
m_lOnBits(7)=CLng(255)
m_lOnBits(8)=CLng(511)
m_lOnBits(9)=CLng(1023)
m_lOnBits(10)=CLng(2047)
m_lOnBits(11)=CLng(4095)
m_lOnBits(12)=CLng(8191)
m_lOnBits(13)=CLng(16383)
m_lOnBits(14)=CLng(32767)
m_lOnBits(15)=CLng(65535)
m_lOnBits(16)=CLng(131071)
m_lOnBits(17)=CLng(262143)
m_lOnBits(18)=CLng(524287)
m_lOnBits(19)=CLng(1048575)
m_lOnBits(20)=CLng(2097151)
m_lOnBits(21)=CLng(4194303)
m_lOnBits(22)=CLng(8388607)
m_lOnBits(23)=CLng(16777215)
m_lOnBits(24)=CLng(33554431)
m_lOnBits(25)=CLng(67108863)
m_lOnBits(26)=CLng(134217727)
m_lOnBits(27)=CLng(268435455)
m_lOnBits(28)=CLng(536870911)
m_lOnBits(29)=CLng(1073741823)
m_lOnBits(30)=CLng(2147483647)

m_l2Power(0)=CLng(1)
m_l2Power(1)=CLng(2)
m_l2Power(2)=CLng(4)
m_l2Power(3)=CLng(8)
m_l2Power(4)=CLng(16)
m_l2Power(5)=CLng(32)
m_l2Power(6)=CLng(64)
m_l2Power(7)=CLng(128)
m_l2Power(8)=CLng(256)
m_l2Power(9)=CLng(512)
m_l2Power(10)=CLng(1024)
m_l2Power(11)=CLng(2048)
m_l2Power(12)=CLng(4096)
m_l2Power(13)=CLng(8192)
m_l2Power(14)=CLng(16384)
m_l2Power(15)=CLng(32768)
m_l2Power(16)=CLng(65536)
m_l2Power(17)=CLng(131072)
m_l2Power(18)=CLng(262144)
m_l2Power(19)=CLng(524288)
m_l2Power(20)=CLng(1048576)
m_l2Power(21)=CLng(2097152)
m_l2Power(22)=CLng(4194304)
m_l2Power(23)=CLng(8388608)
m_l2Power(24)=CLng(16777216)
m_l2Power(25)=CLng(33554432)
m_l2Power(26)=CLng(67108864)
m_l2Power(27)=CLng(134217728)
m_l2Power(28)=CLng(268435456)
m_l2Power(29)=CLng(536870912)
m_l2Power(30)=CLng(1073741824)


Dimx
Dimk
DimAA
DimBB
DimCC
DimDD
Dima
Dimb
Dimc
Dimd

ConstS11=7
ConstS12=12
ConstS13=17
ConstS14=22
ConstS21=5
ConstS22=9
ConstS23=14
ConstS24=20
ConstS31=4
ConstS32=11
ConstS33=16
ConstS34=23
ConstS41=6
ConstS42=10
ConstS43=15
ConstS44=21

x=ConvertToWordArray(sMessage)

a=&H67452301
b=&HEFCDAB89
c=&H98BADCFE
d=&H10325476

Fork=0ToUBound(x)Step16
AA=a
BB=b
CC=c
DD=d

md5_FFa,b,c,d,x(k+0),S11,&HD76AA478
md5_FFd,a,b,c,x(k+1),S12,&HE8C7B756
md5_FFc,d,a,b,x(k+2),S13,&H242070DB
md5_FFb,c,d,a,x(k+3),S14,&HC1BDCEEE
md5_FFa,b,c,d,x(k+4),S11,&HF57C0FAF
md5_FFd,a,b,c,x(k+5),S12,&H4787C62A
md5_FFc,d,a,b,x(k+6),S13,&HA8304613
md5_FFb,c,d,a,x(k+7),S14,&HFD469501
md5_FFa,b,c,d,x(k+8),S11,&H698098D8
md5_FFd,a,b,c,x(k+9),S12,&H8B44F7AF
md5_FFc,d,a,b,x(k+10),S13,&HFFFF5BB1
md5_FFb,c,d,a,x(k+11),S14,&H895CD7BE
md5_FFa,b,c,d,x(k+12),S11,&H6B901122
md5_FFd,a,b,c,x(k+13),S12,&HFD987193
md5_FFc,d,a,b,x(k+14),S13,&HA679438E
md5_FFb,c,d,a,x(k+15),S14,&H49B40821

md5_GGa,b,c,d,x(k+1),S21,&HF61E2562
md5_GGd,a,b,c,x(k+6),S22,&HC040B340
md5_GGc,d,a,b,x(k+11),S23,&H265E5A51
md5_GGb,c,d,a,x(k+0),S24,&HE9B6C7AA
md5_GGa,b,c,d,x(k+5),S21,&HD62F105D
md5_GGd,a,b,c,x(k+10),S22,&H2441453
md5_GGc,d,a,b,x(k+15),S23,&HD8A1E681
md5_GGb,c,d,a,x(k+4),S24,&HE7D3FBC8
md5_GGa,b,c,d,x(k+9),S21,&H21E1CDE6
md5_GGd,a,b,c,x(k+14),S22,&HC33707D6
md5_GGc,d,a,b,x(k+3),S23,&HF4D50D87
md5_GGb,c,d,a,x(k+8),S24,&H455A14ED
md5_GGa,b,c,d,x(k+13),S21,&HA9E3E905
md5_GGd,a,b,c,x(k+2),S22,&HFCEFA3F8
md5_GGc,d,a,b,x(k+7),S23,&H676F02D9
md5_GGb,c,d,a,x(k+12),S24,&H8D2A4C8A

md5_HHa,b,c,d,x(k+5),S31,&HFFFA3942
md5_HHd,a,b,c,x(k+8),S32,&H8771F681
md5_HHc,d,a,b,x(k+11),S33,&H6D9D6122
md5_HHb,c,d,a,x(k+14),S34,&HFDE5380C
md5_HHa,b,c,d,x(k+1),S31,&HA4BEEA44
md5_HHd,a,b,c,x(k+4),S32,&H4BDECFA9
md5_HHc,d,a,b,x(k+7),S33,&HF6BB4B60
md5_HHb,c,d,a,x(k+10),S34,&HBEBFBC70
md5_HHa,b,c,d,x(k+13),S31,&H289B7EC6
md5_HHd,a,b,c,x(k+0),S32,&HEAA127FA
md5_HHc,d,a,b,x(k+3),S33,&HD4EF3085
md5_HHb,c,d,a,x(k+6),S34,&H4881D05
md5_HHa,b,c,d,x(k+9),S31,&HD9D4D039
md5_HHd,a,b,c,x(k+12),S32,&HE6DB99E5
md5_HHc,d,a,b,x(k+15),S33,&H1FA27CF8
md5_HHb,c,d,a,x(k+2),S34,&HC4AC5665

md5_IIa,b,c,d,x(k+0),S41,&HF4292244
md5_IId,a,b,c,x(k+7),S42,&H432AFF97
md5_IIc,d,a,b,x(k+14),S43,&HAB9423A7
md5_IIb,c,d,a,x(k+5),S44,&HFC93A039
md5_IIa,b,c,d,x(k+12),S41,&H655B59C3
md5_IId,a,b,c,x(k+3),S42,&H8F0CCC92
md5_IIc,d,a,b,x(k+10),S43,&HFFEFF47D
md5_IIb,c,d,a,x(k+1),S44,&H85845DD1
md5_IIa,b,c,d,x(k+8),S41,&H6FA87E4F
md5_IId,a,b,c,x(k+15),S42,&HFE2CE6E0
md5_IIc,d,a,b,x(k+6),S43,&HA3014314
md5_IIb,c,d,a,x(k+13),S44,&H4E0811A1
md5_IIa,b,c,d,x(k+4),S41,&HF7537E82
md5_IId,a,b,c,x(k+11),S42,&HBD3AF235
md5_IIc,d,a,b,x(k+2),S43,&H2AD7D2BB
md5_IIb,c,d,a,x(k+9),S44,&HEB86D391

a=AddUnsigned(a,AA)
b=AddUnsigned(b,BB)
c=AddUnsigned(c,CC)
d=AddUnsigned(d,DD)
Next

Ifstype=32Then
MD5=LCase(WordToHex(a)&WordToHex(b)&WordToHex(c)&WordToHex(d))
Else
MD5=LCase(WordToHex(b)&WordToHex(c))
EndIf
EndFunction

'下面是测试代码
Subtest()
MsgBoxMD5("a",16)'16位加密
MsgBoxMD5("a",32)'32位加密
EndSub

2. VB 加密与解密的程序代码

加密:

PrivateFunction JiaMi(ByVal varPass As String) As String '参数varPass是需要加密的文本内容

Dim varJiaMi As String * 20

Dim varTmp As Double

Dim strJiaMi As String

Dim I

For I = 1 To Len(varPass)

varTmp = AscW(Mid$(varPass, I, 1))

varJiaMi = Str$(((((varTmp * 1.5) / 5.6) * 2.7) * I))

strJiaMi = strJiaMi & varJiaMi

NextI

JiaMi = strJiaMi

EndFunction

解密函数:

PrivateFunction JieMi(ByVal varPass As String) As String '参数varPass是需要解密的密文内容

Dim varReturn As String * 20

Dim varConvert As Double

Dim varFinalPass As String

Dim varKey As Integer

Dim varPasslenth As Long

varPasslenth = Len(varPass)

For I = 1 To varPasslenth / 20

varReturn = Mid(varPass, (I - 1) * 20 + 1, 20)

varConvert = Val(Trim(varReturn))

varConvert = ((((varConvert / 1.5) * 5.6) / 2.7) / I)

varFinalPass = varFinalPass & ChrW(Val(varConvert))

NextI

JieMi = varFinalPass

EndFunction

(2)vb本机加密扩展阅读:

注意事项

编写加密程序,将用户输入的一个英文句子加密为加密字符串,然后输出加密字符串。假设句子长度不超过100个字符。

根据给定的句子加密函数原型SentenceEncoding,编写函数SentenceEncoding调用给定的字符加密函数CharEncoding完成句子加密。

然后,编写主程序提示用户输入英文句子,然后调用函数SentenceEncoding对句子加密,最后输出加密后的句子。

字符加密规则为大写字母和小写字母均加密为其补码, 我们定义ASCII码值相加为’A’+’Z’即155的两个大写字母互为补码,ASCII码值相加为’a’+’z’即219的两个小写字母互为补码。

空格用@代替,句号以#代替,其它字符用句点代替。

函数原型:

void SentenceEncoding(char *soure,char *code);

功能:对待加密字符串source加密后保存加密字符串到code.

参数:char *soure,指向待加密句子的字符串指针;

char *code 指向加密字符串的字符串指针;

字符加密函数代码。

3. 如何对自己的VB程序加密

这样的话你就放心吧,别人不会得到你的源代码的。得到的是汇编语言。这样哪怕你再会加密也不可能连汇编码都不生成。所以说不用加密的。
而你如果真的用vb来编写卖钱的软件么,可以用注册码加密的方法。这样的话就看你的加密算法怎么样了。一般用md5就可以了

4. 怎样用VB给文件夹进行密码加密

文件或文件夹的加密、解密

'此方法对 WinXP 系统有效,Win98 没试验过。小心:不能用于系统文件或文件夹,否则会使系统瘫痪。
'加密:利用 API 函数在文件或文件夹名称末尾添上字符“..\”。比如,将文件夹“MyPath”更名为“MyPath..\”,在我的电脑中显示的名称就是“MyPath.”。系统会无法识别,此文件或文件夹就无法打开和修改,也无法删除。着名的病毒 Autorun 就是玩的这个小把戏。
'解密:去掉文件或文件夹名称末尾的字符“..\”

'将以下代码复制到 VB 的窗体代码窗口即可
'例子需控件:Command1、Command2、Text1,均采用默认属性设置
Private Const MAX_PATH = 260
Private Type FileTime ' 8 Bytes
LTime As Long
HTime As Long
End Type
Private Type Win32_Find_Data
dwFileAttributes As Long
ftCreationTime As FileTime
ftLastAccessTime As FileTime
ftLastWriteTime As FileTime
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cNameFile As String * MAX_PATH
cAlternate As String * 14
End Type
Private Declare Function MoveFileEx Lib "kernel32" Alias "MoveFileExA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal dwFlags As Long) As Long
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpNameFile As String, lpFindFileData As Win32_Find_Data) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As Win32_Find_Data) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Private Sub Form_Load()
Text1.Text = "C:\MyPath"
Command1.Caption = "解密": Command2.Caption = "加密"
Me.Caption = "目录或文件的加解密"
End Sub
Private Sub Command1_Click()
Call SetPathName(False) '解密
End Sub
Private Sub Command2_Click()
Call SetPathName(True) '加密
End Sub
Private Sub SetPathName(SetMi As Boolean)
Dim nName As String, NewName As String, nSort As String, nCap As String, dl As Long
nName = Trim(Text1.Text)
If Right(nName, 3) = "..\" Then nName = Left(nName, Len(nName) - 3)
If Right(nName, 1) = "\" Then nName = Left(nName, Len(nName) - 1)
If SetMi Then
NewName = nName & "..\"
Else
NewName = nName
nName = nName & "..\"
End If
If SetMi Then nCap = "加密" Else nCap = "解密"
nSort = GetShortName(nName) '转变其中的 ..\
If nSort = "" Then
MsgBox "文件没有找到:" & vbCrLf & nName, vbCritical, nCap
Exit Sub
End If
If MoveFileEx(nSort, NewName, 0) = 0 Then Exit Sub '文件更名:非零表示成功,支持只读文件
MsgBox nCap & "成功:" & vbCrLf & nName, vbInformation, nCap
End Sub
Public Function GetShortName(F As String, Optional ShortAll As Boolean) As String
'转变为短文件名,如果目录或文件不存在就返回空。可用于判断某目录或文件是否存在
'不能直接用 API 函数 GetShortPathName, 因它不支持 ..\
'ShortAll=T 表示全部转变为短名称,否则只转变其中的点点杠“..\”
Dim FondID As Long, ID1 As Long, S As Long, nPath As String
Dim nF As String, InfoF As Win32_Find_Data, qF As String, hF As String
Dim nName As String, nName1 As String

nF = F
Do
S = InStr(nF, "..\")
If S = 0 Then Exit Do
qF = Left(nF, S + 2): hF = Mid(nF, S + 3) '分为前后两部分
CutPathName qF, nPath, nName
nName = LCase(nName)
qF = nPath & "\" & "*."
FondID = FindFirstFile(qF, InfoF) '-1表示失败。查找所有文件(夹)
ID1 = FondID
Do
If FondID = Find_Err Or ID1 = 0 Then GoTo Exit1 '没有找到符合条件的条目

nName1 = LCase(CutChr0(InfoF.cNameFile)) '文件(夹)名称
If nName1 & ".\" = nName Then
nName1 = CutChr0(InfoF.cAlternate) '用短文件名代替
If hF = "" Then nF = nPath & "\" & nName1 Else nF = nPath & "\" & nName1 & "\" & hF
Exit Do
End If
ID1 = FindNextFile(FondID, InfoF) '查找下一个,0表示失败
Loop
FindClose FondID
Loop

Exit1:
FindClose FondID

S = MAX_PATH: nName = String(S, vbNullChar)
ID1 = GetShortPathName(nF, nName, S) '返回实际字节数,0表示失败
If ID1 = 0 Then Exit Function

If ShortAll Then
If ID1 > S Then
S = ID1: nName = String(S, vbNullChar)
ID1 = GetShortPathName(nF, nName, S) '返回实际字节数
End If
GetShortName = CutChr0(nName)
Else
GetShortName = nF
End If
End Function
Public Sub CutPathName(ByVal F As String, nPath As String, nName As String)
Dim I As Long, LenS As Long

LenS = Len(F)
For I = LenS - 1 To 2 Step -1
If Mid(F, I, 1) = "\" Then
nPath = Left(F, I - 1): nName = Mid(F, I + 1)
GoTo Exit1
End If
Next
nPath = F: nName = ""

Exit1:

If Right(nPath, 2) = ".." Then
nPath = nPath & "\"
Else
If Right(nPath, 1) = "\" Then nPath = Left(nPath, Len(nPath) - 1)
End If

If Right(nName, 1) = "\" And Right(nName, 3) <> "..\" Then nName = Left(nName, Len(nName) - 1)
End Sub
Private Function CutChr0(xx As String) As String
Dim S As Long
S = InStr(xx, vbNullChar)
If S > 0 Then CutChr0 = Left(xx, S - 1) Else CutChr0 = xx
End Function
'参考资料见下

5. 如何加密VB

用一些EXE压缩软件压缩一下就行了,文件被压缩的同时也自然而然地起到了加密的作用,一举两得,而且压缩后的exe文件可以直接运行。
我推荐用ASPack

6. VB调用JS实现RSA本地加密

使用部件Microsoft script control 1.0 (msscript.ocx)应该可以实现。

你参考下VB调用JS计算MD5的方法。

PrivateSubCommand1_Click()
SetScriptControl=ScriptControl1
ScriptControl1.Language="Jscript"
ScriptControl1.Timeout=-1
ScriptControl1.AddCodetxtVarHexcase.Text
Text2.Text=ScriptControl1.Run("md5",Text1.Text)
EndSub

7. VB中如何编写一个加密程序

编写一个加密软件,要求将源文件按字节逐位倒排序加密法加密。

字节逐位倒排序加密法是以比特为单位的换位加密方法,用vb实现的具体算法是:

(1) 以二进制模式打开源文件;

(2) 从源文件第i位读取一个字节,假设为字母“a”,得到“a”的ascii值为65;

(3) 将65转换成八位二进制串为“01000001”;

(4) 将“01000001”按字节逐位倒排序得另一个八位二进制串“10000010”;

(5) 将“10000010”转换成十进制再写回源文件第i位置,完成一个字节的加密;

(6) 重复(2)、(3)、(4)和(5),直到所有字节加密结束。

为了使程序模块化,我们用函数过程bytetobin完成将字节型数据转换成二进制串(其实质就是将十进制数转换成八位二进制串);用函数过程bintobyte将二进制串转换成字节型数据(实质是将八位二进制串转换成十进制数):用函数过程reverse将八位二进制串逐位倒排序。具体程序如下:

function bytetobin(m as byte) as string ' 将字节型数据转换成八位二进制字符串
dim c$
c$ = ""
do while m <> 0
r = m mod 2
m = m \ 2
c$ = r & c$
loop
c$ = right("00000000" & c$, 8)
bytetobin = c$
end function
function reverse(m as string) as string ' 将八位二进制字符串颠倒顺序
dim i%, x$
x = ""
for i = 1 to 8
x = mid(m, i, 1) & x
next i
reverse = x
end function
function bintobyte(m as string) as byte ' 将八位二进制串转换成十进制
dim x as string * 1, y%, z%
z = 0
for i = 1 to 8
x = mid(m, i, 1)
y = x * 2 ^ (8 - i)
z = z + y
next i
bintobyte = z
end function
private sub command1_click()
dim x as byte, i%, fname$
fname = inputbox("请输入要加密的文件名!注意加上路径名:")
if dir(fname) = "" then
msgbox "文件不存在!"
exit sub
end if
open fname for binary as #1 ' 以二进制访问模式打开待加密文件
for i = 1 to lof(1) ' lof函数是求文件长度的内部函数
get #1, i, x ' 取出第i个字节
x = bintobyte(reverse(bytetobin(x))) ' 这里调用了三个自定义函数
put #1, i, x ' 将加密后的这个字节写回到文件原位置
next i
close
msgbox "任务完成!"
end sub

8. 使用VB作出加密,解密并显示密钥

'这是我以前回答别人提问时写的,添加三个文本框,一个按钮。text3文本框中输入要加密的
'文本,在text2中是加密的文本,在text1中是对加密的文本解密。加密和解密用同一个
'过程'Private Function JiaMi(a As String) As String

Private Sub Command1_Click()
Dim a As String
Dim b As String
a = Text3
For i = 1 To Len(a)
b = b & JiaMi(Mid(a, i, 1))
Next i
Text2 = b
a = Text2
b = ""
For i = 1 To Len(a)
b = b & JiaMi(Mid(a, i, 1))
Next i
Text1 = b
End Sub
Private Function JiaMi(a As String) As String
Dim IntAsc As Integer
IntAsc = Asc(a)
If IntAsc Mod 2 Then
IntAsc = IntAsc + 47
If IntAsc > 126 Then IntAsc = IntAsc - 47
Else
IntAsc = IntAsc - 47
If IntAsc < 33 Then IntAsc = IntAsc + 47
End If
JiaMi = Chr(IntAsc)
End Function

9. 用VB编写程序怎样给文件夹加密码

加密原理:循环使用密码中每个字符的ASCII码值与文件的每个字节进行异或运算,然后写入文件即可。这种加密方法是可逆的,即对明文进行加密得到密文,用相同的密码对密文进行加密就得到明文。
界面设计:在窗体From1上放置驱动器列表框(Driver1)、目录列表框(Dir1)、文件列表框(File1)各一个,这三个控件相互配合,用来确定要加密文件的位置。其中File1的Pattern属性设为“*.TXT”,即仅显示文本文件;再放置一个Check控件,用来控制显示文件的类型,其Caption属性设为“显示全部文件”;接着放置两个文本框,Text1显示文件内容,Text2用来输入密码,其Passchar属性设为“*”,一个Label控件,其Caption属性设为“密码”;最后,放置两个命令按钮,其Caption属性分别设为“加密/解密”和“退出”。
程序代码:
Option Explicit
Dim i As Long
Dim databuff() As Byte ’定义数组用于存放文件内容
Dim addbuff() As Byte ’定义数组用于存放加密后的文件内容
Dim password() As Byte ’定义数组用于存放密码的ASCII值
Dim filename As String
Private Sub Check1_Click()�
If Check1.Value Then ’控制是否显示全部文件
File1.Pattern = "*.*"
Else
File1.Pattern = ".txt"
End If
End Sub
Private Sub Command1_Click()�
Dim j As Integer
Dim password_len As Integer
password_len = Len(Text2.Text)
ReDim password(password_len) As Byte
For i = 0 To password_len - 1 ’把密码转化为ASCII码
password(i)= Asc(Mid(Text2.Text,i + 1,1))
Next
If filename = "" Then Exit Sub
Open filename For Binary As #1 ’读取要加密的文件内容
ReDim databuff(LOF(1))
Get #1,, databuff
Close #1
ReDim addbuff(UBound(databuff))As Byte
For i = 0 To UBound(databuff)
If j >= password_len Then ’循环使用密码
j = 0
Else
j = j + 1
End If
addbuff(i)= databuff(i)Xor password(j)’进行异或运算
Next
Open filename For Binary As #1 ’把加密后的内容写入文件
Put #1,,addbuff
Close #1
Text1 = StrConv(addbuff vbUnicode)’显示加密后的文件内容
Text2.Text = ""
End Sub
Private Sub Command2_Click()�
.End
End Sub
Private Sub Dir1_Change()�
File1.Path = Dir1.Path ’与文件列表框相关联
End Sub
Private Sub Drive1_Change()�
On Error GoTo a0
Dir1.Path = Drive1.Drive ’与目录列表框相关联
a0:If Err Then MsgBox(Error(Err))’发生错误,提示错误内容
End Sub
Private Sub File1_Click()’单击文件时,显示文件内容
filename = Dir1.Path + File1.filename
If filename = "" Then Exit Sub
Open filename For Binary As #1
ReDim databuff(LOF(1))
Get #1,,databuff
Close #1
Text1 = StrConv(databuff,vbUnicode)
End Sub

10. VB 保存文件并加密!

简单说就是做个加密‘解密的两个函数,
在保存之前把要保存的记录先通过加密函数加密一下,再存到ini文件里,在读取的时候先读取ini里的信息再通过解密函数把他解密,然后在显示在文本框上,
加密的方式有很多,异或加密法是比较快的一种

热点内容
不支持的压缩算法 发布:2024-11-30 00:44:54 浏览:901
机加工的编程 发布:2024-11-30 00:31:19 浏览:727
坦克世界电脑什么配置 发布:2024-11-30 00:30:41 浏览:317
如何在手机设置上找到网络的密码 发布:2024-11-30 00:18:29 浏览:414
和鹰加密锁 发布:2024-11-30 00:17:17 浏览:556
C语言舞会 发布:2024-11-30 00:15:44 浏览:377
java下载我的世界 发布:2024-11-30 00:11:08 浏览:12
华三配置器升级失败怎么回事 发布:2024-11-30 00:09:23 浏览:842
汽车空调压缩机寿命 发布:2024-11-30 00:06:04 浏览:567
电脑网络波动异常与服务器失去连接 发布:2024-11-29 23:43:19 浏览:247