能够加密和解密的代码
能够加密和解密的代码
要求如下:
1、能够给定的 字符串A (仅含数字和26个英文字母) 和 加密凭据字符串KEY 生成 密文B(仅含数字和26个英文字母)
2、能够给定的 密文串B (仅含数字和26个英文字母) 和 加密凭据字符串KEY 解密后得 原字符串A(仅含数字和26个英文字母)
[解决办法]
可逆加解密用AES可以实现,具体可以网上搜索一下
[解决办法]
有很多加密算法,你也可以自己定义一个算法。这个其实不难,比如去ASCII编码,异或等等
[解决办法]
aes
或者 des 都可以满足你的要求
[解决办法]
这个是msdn上的具体例子,你可以直接使用。首先你要自己建立Key,IV。具体你自己找一下,很容易的。
Public Function EncryptStringToBytes(ByVal plainText As String, ByVal Key() As Byte, ByVal IV() As Byte) As Byte() If plainText Is Nothing OrElse plainText.Length <= 0 Then Throw New ArgumentNullException("plainText") End If If Key Is Nothing OrElse Key.Length <= 0 Then Throw New ArgumentNullException("Key") End If If IV Is Nothing OrElse IV.Length <= 0 Then Throw New ArgumentNullException("Key") End If Dim encrypted() As Byte Using rijAlg As New RijndaelManaged() rijAlg.Key = Key rijAlg.IV = IV Dim encryptor As ICryptoTransform = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV) Using msEncrypt As New MemoryStream() Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write) Using swEncrypt As New StreamWriter(csEncrypt) swEncrypt.Write(plainText) End Using encrypted = msEncrypt.ToArray() End Using End Using End Using Return encrypted End Function Public Function DecryptStringFromBytes(ByVal cipherText() As Byte, ByVal Key() As Byte, ByVal IV() As Byte) As String If cipherText Is Nothing OrElse cipherText.Length <= 0 Then Throw New ArgumentNullException("cipherText") End If If Key Is Nothing OrElse Key.Length <= 0 Then Throw New ArgumentNullException("Key") End If If IV Is Nothing OrElse IV.Length <= 0 Then Throw New ArgumentNullException("Key") End If Dim plaintext As String = Nothing Using rijAlg As New RijndaelManaged rijAlg.Key = Key rijAlg.IV = IV Dim decryptor As ICryptoTransform = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV) Using msDecrypt As New MemoryStream(cipherText) Using csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read) Using srDecrypt As New StreamReader(csDecrypt) plaintext = srDecrypt.ReadToEnd() End Using End Using End Using End Using Return plaintext End Function
[解决办法]
加解密算法多多咯.网络多多.
Private Const base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"''====================================================================== ''##### 加密成可见字符 ##### Private Function Base64_Encode(DecryptedText As String) As String Dim c1, c2, c3 As Integer Dim w1 As Integer Dim w2 As Integer Dim w3 As Integer Dim w4 As Integer Dim N As Long Dim retry As String For N = 1 To Len(DecryptedText) Step 3 c1 = Asc(Mid$(DecryptedText, N, 1)) c2 = Asc(Mid$(DecryptedText, N + 1, 1) + Chr$(0)) c3 = Asc(Mid$(DecryptedText, N + 2, 1) + Chr$(0)) w1 = Int(c1 / 4) w2 = (c1 And 3) * 16 + Int(c2 / 16) If Len(DecryptedText) >= N + 1 Then w3 = (c2 And 15) * 4 + Int(c3 / 64) Else w3 = -1 If Len(DecryptedText) >= N + 2 Then w4 = c3 And 63 Else w4 = -1 retry = retry + mimeencode(w1) + mimeencode(w2) + mimeencode(w3) + mimeencode(w4) Next Base64_Encode = retry End Function ''##### 解密为原来的数据 ##### Private Function Base64_Decode(A As String) As String Dim w1 As Integer Dim w2 As Integer Dim w3 As Integer Dim w4 As Integer Dim N As Long Dim retry As String For N = 1 To Len(A) Step 4 w1 = mimedecode(Mid$(A, N, 1)) w2 = mimedecode(Mid$(A, N + 1, 1)) w3 = mimedecode(Mid$(A, N + 2, 1)) w4 = mimedecode(Mid$(A, N + 3, 1)) If w2 >= 0 Then retry = retry + Chr$(((w1 * 4 + Int(w2 / 16)) And 255)) If w3 >= 0 Then retry = retry + Chr$(((w2 * 16 + Int(w3 / 4)) And 255)) If w4 >= 0 Then retry = retry + Chr$(((w3 * 64 + w4) And 255)) Next Base64_Decode = retry End Function Private Function mimedecode(A As String) As Integer If Len(A) = 0 Then mimedecode = -1: Exit Function mimedecode = InStr(base64, A) - 1 End Function Private Function mimeencode(w As Integer) As String If w >= 0 Then mimeencode = Mid$(base64, w + 1, 1) Else mimeencode = "" End Function
[解决办法]
http://www.m5home.com/bbs/thread-1448-1-1.html
这里有个DES的.
[解决办法]
无论是 DES 还是 AES 都无法做到密文仍然是字母数字字符集。
话说回来了,要求密文是字母数字字符集,必然极大地降低加密算法的安全性。再说,要求密文是字母数字字符集有意义吗?是怕攻击者看不懂乱码吗?
最原始的密码,的确有过楼主所说的,密文仍然是明文所用字符集组成的。比如古罗马的凯撒密码、清代银号的银票密押。主要的方法就是替换和移位。这种方式延续到二战,只是替换和移位的组合更加复杂而已,比如德国的恩戈玛加密机(机械方式)等。大家知道,基于这种密码机的德国和日本密码体系,都被英美破译了,而且后果非常严重。
现代密码是基于数学的,且替换和移位都是基于比特的,密钥 Key 的参与也更加复杂,任何一个比特的改变都将扩展到整个分组。DES 的分组是 8 字节,AES 的分组是 16字节。因此,得到的密文必然是“乱码”。
如果楼主的加密算法不仅仅是游戏,就不要采用那样的限制。