當前位置:首頁 » 密碼管理 » 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: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
個人電腦與伺服器架構類似 發布:2024-11-29 23:40:59 瀏覽:705