當前位置:首頁 » 密碼管理 » vb代碼加密

vb代碼加密

發布時間: 2023-06-08 08:22:38

1. VB 字元串加密解密[高分]

就這樣了,大概還行
Private Function Encrypt(ByVal StrSource As String) As String '加密
Dim BLowData As Byte
Dim BHigData As Byte
Dim i As Long
Dim k As Integer
Dim StrEncrypt As String
Dim StrChar As String
Dim KeyTemp As String
Dim Key1 As Byte
For k = 1 To 30
KeyTemp = KeyTemp & CStr(Int(Rnd * (9) + 1))
Next
Key1 = CByte(Mid(KeyTemp, 11, 1) & Mid(KeyTemp, 27, 1))
For i = 1 To Len(StrSource)
StrChar = Mid(StrSource, i, 1) '從待加密字元串中取出一個字元
BLowData = AscB(MidB(StrChar, 1, 1)) Xor Key1 '取字元的低位元組和Key1進行異或運算
SHigData = AscB(MidB(StrChar, 2, 1)) '取字元的高位元組
StrEncrypt = StrEncrypt & ChrB(BLowData) & ChrB(BHigData) '將運算後的數據合成新的字元
Next i
Encrypt = KeyTemp & StrEncrypt
End FunctionPrivate Function Decrypt(ByVal StrSource As String) As String '解密
Dim BLowData As Byte
Dim BHigData As Byte
Dim i As Long
Dim k As Integer
Dim StrDecrypt As String
Dim StrChar As String
Dim KeyTemp As String
Dim Key1 As Byte
KeyTemp = Mid(StrSource, 1, 30)
Key1 = CByte(Mid(KeyTemp, 11, 1) & Mid(KeyTemp, 27, 1))
For i = 31 To Len(StrSource)
StrChar = Mid(StrSource, i, 1) '從待解密字元串中取出一個字元
BLowData = AscB(MidB(StrChar, 1, 1)) Xor Key1 '取字元的低位元組和Key1進行異或運算
BHigData = AscB(MidB(StrChar, 2, 1)) '取字元的高位元組
StrDecrypt = StrDecrypt & ChrB(BLowData) & ChrB(BHigData) '將運算後的數據合成新的字元
Next i
Decrypt = StrDecryptEnd Function
Private Sub Command2_Click()
MsgBox Decrypt(InputBox(""))
End SubPrivate Sub Command1_Click()
Text1.Text = Encrypt(InputBox(""))
End Sub

2. 怎麼用VB給文件夾加密

1、由於採用二進制讀取文件的方式,因此加密時一般可以不考慮文件類型。
2、這里只進行一次異或運算,如有需要可以進行多次異或運算。
3、此加密演算法速度快,當然加密強度也低 ;
參考代碼如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

'-----------------------------------------------------------------------
'函數說明: 使用異或運算加密文件(可加密大部分文件)
'參數說明: key - 密鑰
' fileName - 普通文件名,
' encryptFileName - 加密後的文件名
'返回值: true - 成功,false - 失敗
'-----------------------------------------------------------------------
Private Function XOR_Encrypt(key As Integer, fileName As String, encryptFileName As String) As Boolean
On Error GoTo errHandler
Dim inputFileNo As Integer
Dim fileBytes() As Byte
Dim length As Long
XOR_Encrypt = False
'打開文件並保存在二進制數組中
inputFileNo = FreeFile
Open fileName For Binary As #inputFileNo
length = LOF(inputFileNo)
If length = 0 Then
MsgBox "退出加密:文件內容為空!", vbInformation, "提示"
Exit Function
End If
ReDim fileBytes(length - 1) As Byte
Get inputFileNo, , fileBytes()
Close #inputFileNo
'將該二進制數組進行異或加密
Dim i As Long
For i = LBound(fileBytes) To UBound(fileBytes)
fileBytes(i) = fileBytes(i) Xor key
Next
'將異或加密後的二進制數組保存在新的文件中
Dim outputFileNo As Integer
outputFileNo = FreeFile
Open encryptFileName For Binary As #outputFileNo
Put outputFileNo, , fileBytes
Close #outputFileNo
XOR_Encrypt = True

errHandler:
If Err.Number Then
MsgBox "加密過程中出錯:" & Err.Description, vbCritical, "錯誤"
XOR_Encrypt = False
Resume Next
End If
End Function

3. vb加密演算法

PrivateSubCommand1_Click()
DimtAsString
t=Text1.Text
Text2.Text=Encrypt(t,177,86)
EndSub

PrivateSubCommand2_Click()
DimtAsString
t=Text2.Text
Text4.Text=Encrypt(t,177,86)
End坦蘆滑Sub

親,你這兩個按鈕裡面的代碼都是加密讓臘的啊!
最基本的知識你都沒有理解!哪有加密和解密都用一樣嘩答的代碼!

4. 運用VB對文字進行加密解密

'這是我從網上找到的一段加密解密的代碼,很不錯,應該符合要求。
'文本框的multiline屬性是用來設置是否可以接受多行文本,只能在窗體上手工設置。
'文本框的scrollbars屬性是用來設置是否有垂直和水平滾動條的,也只能在窗體上手工設置。
'keyAscii不清楚是作什麼用的。
'兩個StrConv函數用的太好了,我沒想到能處理的這么簡單。

Option Explicit
Dim key() As Byte

Sub initkey() '這里為密匙,建議定義的復雜些,我這里僅僅是個示例
ReDim key(9)
key(0) = 12
key(1) = 43
key(2) = 53
key(3) = 67
key(4) = 78
key(5) = 82
key(6) = 91
key(7) = 245
key(8) = 218
key(9) = 190
End Sub

Function Pass_Encode(ByVal s As String) As String '加密
On Error GoTo myerr
initkey
Dim buff() As Byte
buff = StrConv(s, vbFromUnicode)
Dim i As Long, j As Long
Dim k As Long
k = UBound(key) + 1
For i = 0 To UBound(buff)
j = i Mod k
buff(i) = buff(i) Xor key(j)
Next
Dim mstr As String
mstr = ""
Dim outstr As String
Dim temps As String
For i = 0 To UBound(buff)
k = buff(i) \ Len(mstr)
j = buff(i) Mod Len(mstr)
temps = Mid(mstr, j + 1, 1) + Mid(mstr, k + 1, 1)
outstr = outstr + temps
Next
Pass_Encode = outstr
Exit Function
myerr:
Pass_Encode = ""
End Function

Function Pass_Decode(ByVal s As String) As String '解密
On Error GoTo myerr
initkey
Dim i As Long, j As Long
Dim k As Long, n As Long
Dim mstr As String
mstr = ""
Dim outstr As String
Dim temps As String
If Len(s) Mod 2 = 1 Then
Pass_Decode = ""
Exit Function
End If
Dim t1 As String
Dim t2 As String
Dim buff() As Byte
Dim m As Long
m = 0
For i = 1 To Len(s) Step 2
t1 = Mid(s, i, 1)
t2 = Mid(s, i + 1, 1)
j = InStr(1, mstr, t1)
k = InStr(1, mstr, t2)
n = j - 1 + (k - 1) * Len(mstr)
ReDim Preserve buff(m)
buff(m) = n
m = m + 1
Next
k = UBound(key) + 1
For i = 0 To UBound(buff)
j = i Mod k
buff(i) = buff(i) Xor key(j)
Next
Pass_Decode = StrConv(buff, vbUnicode)
Exit Function
myerr:
Pass_Decode = ""
End Function

Private Sub Command1_Click()
Text2.Text = Pass_Encode(Text1.Text)
Text3.Text = Pass_Decode(Text2.Text)
End Sub

5. 用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

6. VB加密演算法

首先程序定義兩個字元串k1和k2;
這里他給出了一個Text1變數,應該是一個全局變數,在程序之前已經定義了。
首先定義code為一個字元串,將Text1裡面的文字用LCase()函數將大寫字母轉換成小寫字母。
加密的過程開始,為那一個循環過程。
在這個循環里 設定i的區間是1到code的長度
每個循環開始時候,將code中每一個字母取出來,賦值到s變數裡面。
然後判斷如果s不是空字元時候,將n賦值為s中的字母與字母a的一個"距離"(這里說不知道你明白不,你好好理解一下吧)。
假如i,即s裡面的那個字母在code中是第幾位的位數,不是2的倍數,那麼就將在decode裡面加入k1字元串中的第n個字母。假如i是2的倍數的話,就在decode加入k2字元串裡面的第n個字母。

在剛才判斷s是否為空字元時候,假如得到的s是為空字元,即跳入以下過程、。
假如i不是2的倍數,空格那個字母變為k1的第27個字母即p,就加入k2的第27個字母即z。

最後將decode賦值給text2。

其實這個程序很簡單,定義兩個字元串,分別是27個字母,等於是26個字母加空格,再把順序掉亂。然後根據需要加密的字元串來分別去每一個字母,根據字母的位置來確定取k1或者k2對應的那個掩碼字母。

7. 怎樣用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
'參考資料見下

8. vb 字母 加密字元串

PrivateSubCommand1_Click()'加密
DimpAsString,sAsString,tAsString
DimiAsInteger,kAsInteger
p=""
s=Text1.Text
Fori=1ToLen(s)
k=InStr(p,Mid(s,i,1))
Ifk=0Then
MsgBox"數據有誤"
ExitSub
Else
t=t&Mid(p,((k+4)Mod52)+1,1)
EndIf
Next
Text2.Text=t
Text1.Text=""
EndSub

PrivateSubCommand2_Click()'解密
DimpAsString,sAsString,tAsString
DimiAsInteger,kAsInteger
p=""
s=Text2.Text
Fori=1ToLen(s)
k=InStr(p,Mid(s,i,1))
Ifk=0Then
MsgBox"數據有誤"
ExitSub
Else
t=t&Mid(p,((k+46)Mod52)+1,1)
EndIf
Next
Text1.Text=t
Text2.Text=""
EndSub

以上代碼用到四個控制項:Text1放加密前的數據,Text2放加密後的數據,Command1點擊加密,Command2點擊解密

9. 怎樣用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
本例可以完成對任意文件的加密與解密,對同一文件作第一次處理為加密,第二次處理為解族搭密。要調試本程序,碧昌可用記事本在C盤根目錄下任意建立一個文本文件(假設為文件名為aaa.txt),其中的內容任意(可以包括字母、漢字、數字、回車符、換行符等)。運行本程序後,在輸入文件名的對話框中輸入文件名(如:「C:\aaa.txt」)後回車,即可完成對文件的加密。文件加密後,可以在記事本中打開該文件查看加密效果。如果想解密,可再次運行該程序並輸入相同文件名。

熱點內容
怎麼把電腦鎖上密碼 發布:2024-05-20 05:19:09 瀏覽:982
安卓為什麼連上wifi後沒有網路 發布:2024-05-20 05:17:50 瀏覽:417
安卓usb在設置哪裡 發布:2024-05-20 05:03:03 瀏覽:185
綏化編程 發布:2024-05-20 04:59:44 瀏覽:989
基本原理和從頭計演算法 發布:2024-05-20 04:50:32 瀏覽:28
配置情況指的是什麼 發布:2024-05-20 04:48:14 瀏覽:495
那個程序用來編譯源文件 發布:2024-05-20 04:46:45 瀏覽:549
小程序需要資料庫嗎 發布:2024-05-20 04:35:14 瀏覽:337
鏈接sqlserver 發布:2024-05-20 04:27:53 瀏覽:209
ftp文件伺服器擴容 發布:2024-05-20 04:22:21 瀏覽:646