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

vb加密中文

發布時間: 2025-05-29 06:17:51

『壹』 vb MD5 加密漢字和標準的不一樣.

很可能是你的原字元串的編碼不和標準的一樣

『貳』 運用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

『叄』 怎麼用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

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

『伍』 vb中如何對字元串進行加密和解密(有漢字的)

源程序如下:

Public Function StringEnDeCodecn(strSource As String, MA) As String
'該函數只對中西文起到加密作用
'參數為:源文件,密碼
On Error GoTo ErrEnDeCode
Dim X As Single
Dim CHARNUM As Long, RANDOMINTEGER As Integer
Dim SINGLECHAR As String * 1
Dim strTmp As String
If MA < 0 Then
MA = MA * (-1)
End If
X = Rnd(-MA)
For i = 1 To Len(strSource) Step 1 '取單位元組內容
SINGLECHAR = Mid(strSource, i, 1)
CHARNUM = Asc(SINGLECHAR)
g: RANDOMINTEGER = Int(127 * Rnd)
If RANDOMINTEGER < 30 Or RANDOMINTEGER > 100 Then GoTo g
CHARNUM = CHARNUM Xor RANDOMINTEGER
strTmp = strTmp & Chr(CHARNUM)
Next i
StringEnDeCodecn = strTmp
Exit Function
ErrEnDeCode:
StringEnDeCodecn = ""
MsgBox Err.Number & "\" & Err.Description
End Function

使用方法:
tmp=stringEnDecn("中華人民共和國",75)
如果要解密的話,只須鍵入以下語句:
tmp1=stringendecn(tmp,75)

『陸』 vb編程,設計一個文本加密程序。輸入一個字元串,按照以下規律加密「

'你這個需求很有意思啊,編寫難度不是很大,但要考慮全面。
'運行效果如上圖,代碼如下所示:
'判斷中我將大小寫字母均按規則進行了轉換。
'原創,親測可用,轉發請說明出處。

PrivateSubCommand1_Click()
Dima,bAsString
a=Text1.Text
DimiAsInteger
i=Len(a)
Dimarr()AsString
ReDimarr(1Toi)
DimjAsInteger
Forj=1Toi
b=Mid(a,j,1)
IfAsc(b)>=97AndAsc(b)<=118Then
arr(j)=Chr(Asc(b)+4)
EndIf
IfAsc(b)=119Then
arr(j)="a"
EndIf
IfAsc(b)=120Then
arr(j)="b"
EndIf
IfAsc(b)=121Then
arr(j)="c"
EndIf
IfAsc(b)=122Then
arr(j)="d"
EndIf
IfAsc(b)>=65AndAsc(b)<=86Then
arr(j)=Chr(Asc(b)+4)
EndIf
IfAsc(b)=87Then
arr(j)="A"
EndIf
IfAsc(b)=88Then
arr(j)="B"
EndIf
IfAsc(b)=89Then
arr(j)="C"
EndIf
IfAsc(b)=90Then
arr(j)="D"
EndIf
IfAsc(b)<65OrAsc(b)>122Or(Asc(b)>90AndAsc(b)<97)Then
arr(j)=b
EndIf
Nextj
Text2.Text=Join(arr,"")
EndSub

『柒』 用VB如何給文件加密

這個比較麻煩的。

給文件加密,我還是建議您選擇專業的文件加密軟體。

超級加密3000擁有高強度加密演算法,有效保障數據安全!

建議您再加密文件的時候可以試試。

熱點內容
華為liteos編譯 發布:2025-05-30 13:25:52 瀏覽:842
北森編程題 發布:2025-05-30 12:53:49 瀏覽:785
無法打開ftp 發布:2025-05-30 12:47:11 瀏覽:662
文件夾的資料 發布:2025-05-30 12:06:07 瀏覽:72
蘋果手機伺服器地址哪裡獲取 發布:2025-05-30 11:46:25 瀏覽:750
安卓系統開源谷歌怎麼控制的 發布:2025-05-30 11:43:30 瀏覽:367
5m上傳速度 發布:2025-05-30 11:43:25 瀏覽:239
c語言集合的並運算 發布:2025-05-30 11:41:11 瀏覽:824
雲學生伺服器 發布:2025-05-30 11:15:20 瀏覽:154
瑞恩源碼 發布:2025-05-30 11:02:33 瀏覽:352