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
加密:
Private Function 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
Next I
JiaMi = strJiaMi
End Function
解密函数:
Private Function 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))
Next I
JieMi = varFinalPass
End Function
扩展资料:注意事项
编写加密程序,将用户输入的一个英文句子加密为加密字符串,然后输出加密字符串。假设句子长度不超过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 指向加密字符串的字符串指针;
字符加密函数代码。
'图上的控件,你就照着摆上去,然后再把以下代码拷进去,就OK了Dim lg As Integer
'加密
Private Sub Command1_Click()
Text2 = ""
Dim a(), b() As String
lg = Len(Text1)
ReDim a(lg), b(lg)
For i = 1 To lg
a(i) = Mid(Text1, i, 1)
b(i) = AscW(a(i)) Xor 4
Text2 = Text2 &ChrW(b(i))
Next
End Sub
'解密
Private Sub Command2_Click()
Text3 = ""
Dim a(), b() As String
lg = Len(Text2)
ReDim a(lg), b(lg)
For i = 1 To lg
a(i) = Mid(Text2, i, 1)
b(i) = AscW(a(i)) Xor 4
Text3 = Text3 &ChrW(b(i))
Next
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)