本人在操作数据库时,考虑到该数据库还有可能被其他软件打开,所以想能否有另外一种方式把数据库中数据进行加密呢,也 就是说,即使别人利用其他的软件打开了该数据库,看到的也是一片乱码,根本不知道数据库进而是什么内容。出于这种情况,本人利用VB中自带 RND()函数的功能编写了如下加密解密方法。 当RND()的参数(我们称它为种子)为负值时,同一种子产生同一个随机序列,同时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)如有不妥之处,请与我联系: [email protected]
如何对字符串进行加密解密
本人在操作数据库时,考虑到该数据库还有可能被其他软件打开,所以想能否有另外一种方式把数据库中数据进行加密呢,也 就是说,即使别人利用其他的软件打开了该数据库,看到的也是一片乱码,根本不知道数据库进而是什么内容。出于这种情况,本人利用VB中自带 RND()函数的功能编写了如下加密解密方法。 当RND()的参数(我们称它为种子)为负值时,同一种子产生同一个随机序列,同时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)如有不妥之处,请与我联系: [email protected]
64位加密方法 Option Explicit 'CIPHER.CLSPrivate mstrKey As String Private mstrText As String'~~~.KeyString 'A string (key) used in encryption and decryption Public Property Let KeyString(strKey As String) mstrKey = strKey Initialize End Property'~~~.Text 'Write text to be encrypted or decrypted Public Property Let Text(strText As String) mstrText = strText End Property'Read text that was encrypted or decrypted Public Property Get Text() As String Text = mstrText End Property'~~~.DoXor 'Exclusive-or method to encrypt or decrypt Public Sub DoXor() On Error Resume Next Dim lngC As Long Dim intB As Long Dim lngN As Long For lngN = 1 To Len(mstrText) lngC = Asc(Mid(mstrText, lngN, 1)) intB = Int(Rnd * 256) Mid(mstrText, lngN, 1) = Chr(lngC Xor intB) Next lngNEnd Sub'~~~.Stretch 'Convert any string to a printable, displayable string Public Sub Stretch() On Error Resume Next Dim lngC As Long Dim lngN As Long Dim lngJ As Long Dim lngK As Long Dim lngA As Long Dim strB As String lngA = Len(mstrText) strB = Space(lngA + (lngA + 2) \ 3) For lngN = 1 To lngA lngC = Asc(Mid(mstrText, lngN, 1)) lngJ = lngJ + 1 Mid(strB, lngJ, 1) = Chr((lngC And 63) + 59) Select Case lngN Mod 3 Case 1 lngK = lngK Or ((lngC \ 64) * 16) Case 2 lngK = lngK Or ((lngC \ 64) * 4) Case 0 lngK = lngK Or (lngC \ 64) lngJ = lngJ + 1 Mid(strB, lngJ, 1) = Chr(lngK + 59) lngK = 0 End Select Next lngN If lngA Mod 3 Then lngJ = lngJ + 1 Mid(strB, lngJ, 1) = Chr(lngK + 59) End If mstrText = strB End Sub'~~~.Shrink 'Inverse of the Stretch method; 'result can contain any of the 256-byte values Public Sub Shrink() On Error Resume Next Dim lngC As Long Dim lngD As Long Dim lngE As Long Dim lngA As Long Dim lngB As Long Dim lngN As Long Dim lngJ As Long Dim lngK As Long Dim strB As String lngA = Len(mstrText) lngB = lngA - 1 - (lngA - 1) \ 4 strB = Space(lngB) For lngN = 1 To lngB lngJ = lngJ + 1 lngC = Asc(Mid(mstrText, lngJ, 1)) - 59 Select Case lngN Mod 3 Case 1 lngK = lngK + 4 If lngK > lngA Then lngK = lngA lngE = Asc(Mid(mstrText, lngK, 1)) - 59 lngD = ((lngE \ 16) And 3) * 64 Case 2 lngD = ((lngE \ 4) And 3) * 64 Case 0 lngD = (lngE And 3) * 64 lngJ = lngJ + 1 End Select Mid(strB, lngN, 1) = Chr(lngC Or lngD) Next lngN mstrText = strB End Sub'Initializes random numbers using the key string Private Sub Initialize() Dim lngN As Long Randomize Rnd(-1) For lngN = 1 To Len(mstrKey) Randomize Rnd(-Rnd * Asc(Mid(mstrKey, lngN, 1))) Next lngN End Sub'------------------------------------------------------------------------------- 'Then use this code to encrypt text Public Sub Encrypt()'call encrypt Dim cipherTest As New Cipher 'entire encryption process cipherTest.KeyString = keytoencrypt.text'the key is used to encrypt the text. it can be letters and numbers cipherTest.Text = plaintext.text'plaintext is a textbox containg what you want to encrypt cipherTest.DoXor cipherTest.Stretch encryptedtext.text = cipherTest.Text'encryptedtext.text is where the encrypted text will show up End Sub -------------------------------------------------------------------------------- 'use this to decrypt Public Sub Decrypt()'call decrypt Dim cipherTest As New Cipher 'entire decryption process cipherTest.KeyString = keyusedtodecrypt.text'keyusedtodecrypt.text is what is used to decrypt the text. It can be letters or numbers cipherTest.Text = textyouwantdecrypted.Text'textyouwantdecrypted is a textbox containg the text you want decrypted cipherTest.Shrink cipherTest.DoXor decryptedtext.Text = cipherTest.Text'decryptedtext.text is where the decrypted text will show up End If End Sub --------------------------------------------------------------------------------
本人在操作数据库时,考虑到该数据库还有可能被其他软件打开,所以想能否有另外一种方式把数据库中数据进行加密呢,也
就是说,即使别人利用其他的软件打开了该数据库,看到的也是一片乱码,根本不知道数据库进而是什么内容。出于这种情况,本人利用VB中自带
RND()函数的功能编写了如下加密解密方法。
当RND()的参数(我们称它为种子)为负值时,同一种子产生同一个随机序列,同时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)如有不妥之处,请与我联系:
[email protected]
本人在操作数据库时,考虑到该数据库还有可能被其他软件打开,所以想能否有另外一种方式把数据库中数据进行加密呢,也
就是说,即使别人利用其他的软件打开了该数据库,看到的也是一片乱码,根本不知道数据库进而是什么内容。出于这种情况,本人利用VB中自带
RND()函数的功能编写了如下加密解密方法。
当RND()的参数(我们称它为种子)为负值时,同一种子产生同一个随机序列,同时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)如有不妥之处,请与我联系:
[email protected]
Option Explicit
'CIPHER.CLSPrivate mstrKey As String
Private mstrText As String'~~~.KeyString
'A string (key) used in encryption and decryption
Public Property Let KeyString(strKey As String)
mstrKey = strKey
Initialize
End Property'~~~.Text
'Write text to be encrypted or decrypted
Public Property Let Text(strText As String)
mstrText = strText
End Property'Read text that was encrypted or decrypted
Public Property Get Text() As String
Text = mstrText
End Property'~~~.DoXor
'Exclusive-or method to encrypt or decrypt
Public Sub DoXor()
On Error Resume Next
Dim lngC As Long
Dim intB As Long
Dim lngN As Long
For lngN = 1 To Len(mstrText)
lngC = Asc(Mid(mstrText, lngN, 1))
intB = Int(Rnd * 256)
Mid(mstrText, lngN, 1) = Chr(lngC Xor intB)
Next lngNEnd Sub'~~~.Stretch
'Convert any string to a printable, displayable string
Public Sub Stretch()
On Error Resume Next
Dim lngC As Long
Dim lngN As Long
Dim lngJ As Long
Dim lngK As Long
Dim lngA As Long
Dim strB As String
lngA = Len(mstrText)
strB = Space(lngA + (lngA + 2) \ 3)
For lngN = 1 To lngA
lngC = Asc(Mid(mstrText, lngN, 1))
lngJ = lngJ + 1
Mid(strB, lngJ, 1) = Chr((lngC And 63) + 59)
Select Case lngN Mod 3
Case 1
lngK = lngK Or ((lngC \ 64) * 16)
Case 2
lngK = lngK Or ((lngC \ 64) * 4)
Case 0
lngK = lngK Or (lngC \ 64)
lngJ = lngJ + 1
Mid(strB, lngJ, 1) = Chr(lngK + 59)
lngK = 0
End Select
Next lngN
If lngA Mod 3 Then
lngJ = lngJ + 1
Mid(strB, lngJ, 1) = Chr(lngK + 59)
End If
mstrText = strB
End Sub'~~~.Shrink
'Inverse of the Stretch method;
'result can contain any of the 256-byte values
Public Sub Shrink()
On Error Resume Next
Dim lngC As Long
Dim lngD As Long
Dim lngE As Long
Dim lngA As Long
Dim lngB As Long
Dim lngN As Long
Dim lngJ As Long
Dim lngK As Long
Dim strB As String
lngA = Len(mstrText)
lngB = lngA - 1 - (lngA - 1) \ 4
strB = Space(lngB)
For lngN = 1 To lngB
lngJ = lngJ + 1
lngC = Asc(Mid(mstrText, lngJ, 1)) - 59
Select Case lngN Mod 3
Case 1
lngK = lngK + 4
If lngK > lngA Then lngK = lngA
lngE = Asc(Mid(mstrText, lngK, 1)) - 59
lngD = ((lngE \ 16) And 3) * 64
Case 2
lngD = ((lngE \ 4) And 3) * 64
Case 0
lngD = (lngE And 3) * 64
lngJ = lngJ + 1
End Select
Mid(strB, lngN, 1) = Chr(lngC Or lngD)
Next lngN
mstrText = strB
End Sub'Initializes random numbers using the key string
Private Sub Initialize()
Dim lngN As Long
Randomize Rnd(-1)
For lngN = 1 To Len(mstrKey)
Randomize Rnd(-Rnd * Asc(Mid(mstrKey, lngN, 1)))
Next lngN
End Sub'-------------------------------------------------------------------------------
'Then use this code to encrypt text
Public Sub Encrypt()'call encrypt
Dim cipherTest As New Cipher 'entire encryption process
cipherTest.KeyString = keytoencrypt.text'the key is used to encrypt the text. it can be letters and numbers
cipherTest.Text = plaintext.text'plaintext is a textbox containg what you want to encrypt
cipherTest.DoXor
cipherTest.Stretch
encryptedtext.text = cipherTest.Text'encryptedtext.text is where the encrypted text will show up
End Sub
--------------------------------------------------------------------------------
'use this to decrypt
Public Sub Decrypt()'call decrypt
Dim cipherTest As New Cipher 'entire decryption process
cipherTest.KeyString = keyusedtodecrypt.text'keyusedtodecrypt.text is what is used to decrypt the text. It can be letters or numbers
cipherTest.Text = textyouwantdecrypted.Text'textyouwantdecrypted is a textbox containg the text you want decrypted
cipherTest.Shrink
cipherTest.DoXor
decryptedtext.Text = cipherTest.Text'decryptedtext.text is where the decrypted text will show up
End If
End Sub
--------------------------------------------------------------------------------
一次异或加密
再异或一次解密
比如字符窜A和1异或之后被加密得到字符串B
字符串B再和1异或一次又得到字符串A