Pad Block已损坏

时间:2014-01-15 09:18:29

标签: android vb.net aes

我必须做以下操作:

  1. 加密VB - >解密VB
  2. 加密Android - 解密Android
  3. 加密VB - >解密Android
  4. 加密Android - >解密VB
  5. 到目前为止,我已成功在Android上进行加密和解密。 当我在VB中加密并尝试在Android上解密时,我得到以下异常:

    E / Exception:pad block corrupted

    我还要提到,当我在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;
    }
    

1 个答案:

答案 0 :(得分:0)

我猜主要的问题是密钥生成在两段代码中都是不同的。密码不是密钥,您应该使用二进制,随机生成的密钥或良好的密钥派生机制,如PBKDF2。

试图找到经过良好审核的lib。在.NET和Java(/ Android)中使用相同的加密协议也是一个好主意。

通常,加密算法的输入必须是二进制的。在执行算法之前,请务必使用十六进制编码比较算法的所有输入。