2012-02-01 238 views
2

下面是一些代碼,絃樂效果很好:AES加密/解密

Public Function AESEncrypt(ByVal PlainText As String, ByVal Password As String, ByVal salt As String) 
    Dim HashAlgorithm As String = "SHA1" 'Can be SHA1 or MD5 
    Dim PasswordIterations As String = 2 
    Dim InitialVector As String = "CanEncryption123" 'This should be a string of 16 ASCII characters. 
    Dim KeySize As Integer = 256 'Can be 128, 192, or 256. 

    If (String.IsNullOrEmpty(PlainText)) Then 
     Return "" 
     Exit Function 
    End If 
    Dim InitialVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitialVector) 
    Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(salt) 
    Dim PlainTextBytes As Byte() = Encoding.UTF8.GetBytes(PlainText) 
    Dim DerivedPassword As PasswordDeriveBytes = New PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations) 
    Dim KeyBytes As Byte() = DerivedPassword.GetBytes(KeySize/8) 
    Dim SymmetricKey As RijndaelManaged = New RijndaelManaged() 
    SymmetricKey.Mode = CipherMode.CBC 

    Dim CipherTextBytes As Byte() = Nothing 
    Using Encryptor As ICryptoTransform = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes) 
     Using MemStream As New MemoryStream() 
      Using CryptoStream As New CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write) 
       CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length) 
       CryptoStream.FlushFinalBlock() 
       CipherTextBytes = MemStream.ToArray() 
       MemStream.Close() 
       CryptoStream.Close() 
      End Using 
     End Using 
    End Using 
    SymmetricKey.Clear() 
    Return Convert.ToBase64String(CipherTextBytes) 
End Function 
Public Function AESDecrypt(ByVal CipherText As String, ByVal password As String, ByVal salt As String) As String 
    Dim HashAlgorithm As String = "SHA1" 
    Dim PasswordIterations As String = 2 
    Dim InitialVector As String = "CanEncryption123" 
    Dim KeySize As Integer = 256 

    If (String.IsNullOrEmpty(CipherText)) Then 
     Return "" 
    End If 
    Dim InitialVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitialVector) 
    Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(salt) 
    Dim CipherTextBytes As Byte() = Convert.FromBase64String(CipherText) 
    Dim DerivedPassword As PasswordDeriveBytes = New PasswordDeriveBytes(password, SaltValueBytes, HashAlgorithm, PasswordIterations) 
    Dim KeyBytes As Byte() = DerivedPassword.GetBytes(KeySize/8) 
    Dim SymmetricKey As RijndaelManaged = New RijndaelManaged() 
    SymmetricKey.Mode = CipherMode.CBC 
    Dim PlainTextBytes As Byte() = New Byte(CipherTextBytes.Length - 1) {} 

    Dim ByteCount As Integer = 0 

    Using Decryptor As ICryptoTransform = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes) 
     Using MemStream As MemoryStream = New MemoryStream(CipherTextBytes) 
      Using CryptoStream As CryptoStream = New CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read) 
       ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length) 
       MemStream.Close() 
       CryptoStream.Close() 
      End Using 
     End Using 
    End Using 
    SymmetricKey.Clear() 
    Return Encoding.UTF8.GetString(PlainTextBytes, 0, ByteCount) 
End Function 

我可以在修改這些功能的加密/解密的字節數組,而不是字符串一些幫助。此外,要使函數返回加密/解密的字節數組,而不是字符串。

感謝

回答

0

最簡單的方法是使用,只需將字節數組轉換爲字符串,用你的AESEncrypt函數加密它的包裝功能和字符串轉換回爲一個字節數組。你可以找到VB.net here的轉換函數。

編輯補充:我覺得我得到了這個錯誤。看來,一個VB String是Unicode格式,而這些翻譯功能將其轉換爲/從UTF8字節數組。這不是什麼要求...

+0

請問這種做法是一樣高效/有效的修改函數接受字節數組,而不是字符串? – 2012-02-01 22:05:49

+0

效率如何?不,但我不認爲頭頂會很重。效果如何?是。 – TonyK 2012-02-01 22:42:33

+0

我已經使用了轉換函數的字節數組轉換爲字符串,並VICA亦可。出於某種原因,如果我嘗試解密加密的字節數組(來自文件),它與原始文件不同。使用這些轉換例程有什麼限制嗎? – 2012-02-01 23:03:24

1

只需複製一個新的功能從Dim ByteCount As Integer = 0SymmetricKey.Clear()擺脫所有的字符串?之後,您只需要定義函數的參數。

+0

字節數組的加密函數如何? :) – 2012-02-18 02:30:03

1

我使用這個(在谷歌找到)的字符串AES加密/解密:

Imports System.Security.Cryptography 

Namespace TextCrypters 

    Public Class AESCrypter 

     Public Shared pass As String = "password" 

     Public Shared Function AES_Encrypt(ByVal input As String) As String 
      Dim AES As New System.Security.Cryptography.RijndaelManaged 
      Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider 
      Dim encrypted As String = "" 
      Try 
       Dim hash(31) As Byte 
       Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass)) 
       Array.Copy(temp, 0, hash, 0, 16) 
       Array.Copy(temp, 0, hash, 15, 16) 
       AES.Key = hash 
       AES.Mode = CipherMode.ECB 
       Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor 
       Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input) 
       encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length)) 
       Return encrypted 
      Catch ex As Exception 
       Return Nothing 
      End Try 

     End Function 

     Public Shared Function AES_Decrypt(ByVal input As String) As String 
      Dim AES As New System.Security.Cryptography.RijndaelManaged 
      Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider 
      Dim decrypted As String = "" 
      Try 
       Dim hash(31) As Byte 
       Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass)) 
       Array.Copy(temp, 0, hash, 0, 16) 
       Array.Copy(temp, 0, hash, 15, 16) 
       AES.Key = hash 
       AES.Mode = CipherMode.ECB 
       Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor 
       Dim Buffer As Byte() = Convert.FromBase64String(input) 
       decrypted = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length)) 
       Return decrypted 
      Catch ex As Exception 
       Return Nothing 
      End Try 

     End Function 

    End Class 

End Namespace 

要使用它只是這樣做:

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    TextBox2.Text = AESCrypter.AES_Encrypt(TextBox1.Text) 
End Sub 

Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 
    TextBox4.Text = AESCrypter.AES_Decrypt(TextBox3.Text) 
End Sub 
+0

該代碼可以「工作」,但它肯定不是安全(ECB模式),它破壞包含非ASCII字符的任意輸入。 – CodesInChaos 2012-12-15 14:27:11

0
Public Function AESEncrypt(ByVal PlainBytes As Byte, ByVal Password As String, ByVal salt As String) 
Dim HashAlgorithm As String = "SHA1" 'Can be SHA1 or MD5 
Dim PasswordIterations As String = 2 
Dim InitialVector As String = "CanEncryption123" 'This should be a string of 16 ASCII characters. 
Dim KeySize As Integer = 256 'Can be 128, 192, or 256. 

Dim InitialVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitialVector) 
Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(salt) 
Dim DerivedPassword As PasswordDeriveBytes = New PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations) 
Dim KeyBytes As Byte() = DerivedPassword.GetBytes(KeySize/8) 
Dim SymmetricKey As RijndaelManaged = New RijndaelManaged() 
SymmetricKey.Mode = CipherMode.CBC 

Dim CipherTextBytes As Byte() = Nothing 
Using Encryptor As ICryptoTransform = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes) 
    Using MemStream As New MemoryStream() 
     Using CryptoStream As New CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write) 
      CryptoStream.Write(PlainBytes, 0, PlainBytes.Length) 
      CryptoStream.FlushFinalBlock() 
      CipherTextBytes = MemStream.ToArray() 
      MemStream.Close() 
      CryptoStream.Close() 
     End Using 
    End Using 
End Using 
SymmetricKey.Clear() 
Return Convert.ToBase64String(CipherTextBytes) 
End Function 
Public Function AESDecrypt(ByVal CipherBytes As Byte, ByVal password As String, ByVal salt As String) As String 
Dim HashAlgorithm As String = "SHA1" 
Dim PasswordIterations As String = 2 
Dim InitialVector As String = "CanEncryption123" 
Dim KeySize As Integer = 256 

If (String.IsNullOrEmpty(CipherText)) Then 
    Return "" 
End If 
Dim InitialVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitialVector) 
Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(salt) 
Dim DerivedPassword As PasswordDeriveBytes = New PasswordDeriveBytes(password, SaltValueBytes, HashAlgorithm, PasswordIterations) 
Dim KeyBytes As Byte() = DerivedPassword.GetBytes(KeySize/8) 
Dim SymmetricKey As RijndaelManaged = New RijndaelManaged() 
SymmetricKey.Mode = CipherMode.CBC 
Dim PlainTextBytes As Byte() = New Byte(CipherBytes.Length - 1) {} 

Dim ByteCount As Integer = 0 

Using Decryptor As ICryptoTransform = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes) 
    Using MemStream As MemoryStream = New MemoryStream(CipherBytes) 
     Using CryptoStream As CryptoStream = New CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read) 
      ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length) 
      MemStream.Close() 
      CryptoStream.Close() 
     End Using 
    End Using 
End Using 
SymmetricKey.Clear() 
Return Encoding.UTF8.GetString(PlainTextBytes, 0, ByteCount) 
End Function 

你實際上使用的代碼將輸入字符串轉換爲字節數組。該代碼接受字節數組。