2014-01-15 120 views
0

我必须做以下操作:垫座损坏

  1. 加密VB - >解密VB
  2. 加密的Android - 解密的Android
  3. 加密VB - >解密的Android
  4. 加密的Android - >解密VB

到目前为止,我成功地在Android上进行了加密和解密。 当我在VB加密和尝试解密在Android上,我得到以下异常:

E /例外:垫块损坏

我还不得不提到的是,当我在VB加密短字符串并将其解密也在VB中,一切运作良好。但是,当我加密一个更大的字节数组时,解密工作,但结果不是预期的结果。

有人能给我提示如何解决这个问题吗? 谢谢!

这里是我的代码:

.NET函数

Public Function AES_Encrypt2(ByVal byteArray() As Byte, ByVal key As String, Optional ByVal ShortKey As Boolean = False) As String 
    Try 

     Dim FirstKeyBytes() As Byte = Encoding.UTF8.GetBytes(key) 

     If Not FirstKeyBytes Is Nothing Then 
      If FirstKeyBytes.Length < 32 Then 
       Array.Resize(FirstKeyBytes, 32) 
      End If 
     End If 

     Dim KeyBytes() As Byte 
     If ShortKey Then 
      KeyBytes = New Byte(15) {} 
      Array.Copy(FirstKeyBytes, KeyBytes, 16) 

     Else 
      KeyBytes = New Byte(31) {} 
      Array.Copy(FirstKeyBytes, KeyBytes, 32) 

     End If 

     Dim InitialVectorBytes() As Byte = New Byte() {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 'Encoding.UTF8.GetBytes("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0") 
     Dim SymmetricKey As New RijndaelManaged() 

     SymmetricKey.Mode = CipherMode.CBC 
     SymmetricKey.Padding = PaddingMode.PKCS7 

     Dim Encryptor As ICryptoTransform = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes) 
     Dim MemStream As New MemoryStream() 
     Dim CryptoStream As New CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write) 

     CryptoStream.Write(byteArray, 0, byteArray.Length) 
     CryptoStream.FlushFinalBlock() 
     MemStream.Close() 
     CryptoStream.Close() 

     Dim CipherTextBytes As Byte() = MemStream.ToArray() 
     Dim encryptedString As String = Convert.ToBase64String(CipherTextBytes) 

     Return encryptedString 
    Catch ex As Exception 
     Return String.Empty 
    End Try 
End Function 



Public Function AES_Decrypt2(ByVal encryptedString As String, ByVal key As String, Optional ByVal ShortKey As Boolean = False) As String 
    Try 

     Dim PlainTextBytes1 As Byte() = Convert.FromBase64String(encryptedString) 
     Dim FirstKeyBytes() As Byte = Encoding.UTF8.GetBytes(key) 

     If Not FirstKeyBytes Is Nothing Then 
      If FirstKeyBytes.Length < 32 Then 
       Array.Resize(FirstKeyBytes, 32) 
      End If 
     End If 

     Dim KeyBytes() As Byte 
     If ShortKey Then 
      KeyBytes = New Byte(15) {} 
      Array.Copy(FirstKeyBytes, KeyBytes, 16) 

     Else 
      KeyBytes = New Byte(31) {} 
      Array.Copy(FirstKeyBytes, KeyBytes, 32) 

     End If 


     Dim SymmetricKey As New RijndaelManaged() 
     Dim InitialVectorBytes As Byte() = New Byte() {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 'Encoding.UTF8.GetBytes("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0") 

     SymmetricKey.Mode = CipherMode.CBC 
     SymmetricKey.Padding = PaddingMode.PKCS7 

     Dim Decryptor As ICryptoTransform = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes) 
     Dim MemStream1 As New MemoryStream(PlainTextBytes1) 
     Dim CryptoStream As New CryptoStream(MemStream1, Decryptor, CryptoStreamMode.Read) 
     Dim pltxt As Byte() = New Byte(PlainTextBytes1.Length - 1) {} 
     Dim d As Integer = CryptoStream.Read(pltxt, 0, pltxt.Length) 

     MemStream1.Close() 
     CryptoStream.Close() 

     Dim textConverter As New ASCIIEncoding() 
     Dim round As String = textConverter.GetString(pltxt, 0, d) 

     Return round 

    Catch ex As Exception 
     Return String.Empty 
    End Try 
End Function 

和Android方法:

public static String encrypt(byte[] input, String key) { 
    try { 
     byte[] iv = new byte[16]; 

     AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv); 

     String newKey = ""; 
     if (key.length() >= 32) { 
      newKey = key.substring(0, 32); 
     } else { 
      for (int i = key.length(); i < 32; i++) { 
       key += "0"; 
      } 
      newKey = key.substring(0, 32); 
     } 

     SecretKeySpec skeySpec = new SecretKeySpec(newKey.getBytes(), "AES"); 
     //skeySpec = new SecretKeySpec(newKey.getBytes(), 0, newKey.length(), "AES"); 
     Cipher fileCipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); 

     fileCipher.init(1, skeySpec, paramSpec); 

     byte[] decrypted = fileCipher.doFinal(input); 

     byte[] base64enc = Base64.encode(decrypted, 0); 

     return new String(base64enc); 
    } catch (Exception e) { 
     Log.e("Exception", e.getMessage()); 
    } 
    return null; 
} 

public static byte[] decrypt(String input, String key) { 
    try { 
     byte[] iv = new byte[16]; 

     AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv); 

     byte[] base64enc = Base64.decode(input.getBytes(), 0); 

     String newKey = ""; 
     if (key.length() >= 32) { 
      newKey = key.substring(0, 32); 
     } else { 
      for (int i = key.length(); i < 32; i++) { 
       key += "0"; 
      } 
      newKey = key.substring(0, 32);; 
     } 

     SecretKeySpec skeySpec = new SecretKeySpec(newKey.getBytes(), "AES"); 
     Cipher fileCipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); 

     fileCipher.init(2, skeySpec, paramSpec); 

     int x = base64enc.length; 

     return fileCipher.doFinal(base64enc); 

    } catch (Exception e) { 
     Log.e("Exception: ", e.getMessage()); 
    } 
    return null; 
} 

回答

0

我想主要的问题是,密钥生成是在两件不同码。密码不是密钥,您应该使用二进制,随机生成的密钥或像PBKDF2这样的密钥派生机制。

试图找到一个经过严格审查的库。在.NET和Java(/ Android)中使用相同的协议进行加密也是一个好主意。

一般来说,对密码算法的输入必须是二进制的。在执行算法之前,始终使用十六进制编码比较算法的所有输入。