我想在c#中加密消息,然后在我的android应用程序中再次解密它们。

我想在c#中加密消息,然后在我的android应用程序中再次解密它们。,c#,android,C#,Android,我知道,这个话题以前在这里处理过,但我不明白我的错误。 Android中的代码: public String encryptMsg(String input) { try { byte[] key_Array = Base64.decode(password, Base64.DEFAULT); Cipher _Cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] iv =

我知道,这个话题以前在这里处理过,但我不明白我的错误。 Android中的代码:

public String encryptMsg(String input) {
    try {
        byte[] key_Array = Base64.decode(password, Base64.DEFAULT);

        Cipher _Cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        byte[] iv = { 1, 2, 3, 4, 4, 6, 6, 5, 4, 3, 2, 1, 7, 7, 7, 7 };
        IvParameterSpec ivspec = new IvParameterSpec(iv);

        Key SecretKey = new SecretKeySpec(key_Array, "AES");
        _Cipher.init(Cipher.ENCRYPT_MODE, SecretKey, ivspec);

        return Base64.encodeToString(_Cipher.doFinal(input.getBytes()), Base64.DEFAULT);
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    return null;
}

public String decryptMsg(String input) {
    try {
        byte[] key_Array = Base64.decode(password, Base64.DEFAULT);

        Cipher _Cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        byte[] iv = { 1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 1, 7, 7, 7, 7 };
        IvParameterSpec ivspec = new IvParameterSpec(iv);

        Key SecretKey = new SecretKeySpec(key_Array, "AES");
        _Cipher.init(Cipher.DECRYPT_MODE, SecretKey, ivspec);

        byte DecodedMessage[] = Base64.decode(input, Base64.DEFAULT);
        return new String(_Cipher.doFinal(DecodedMessage));

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    return null;
}
我的C#代码:

我在c#应用程序中加密后,在android应用程序中解密文本时出错。错误如下: “javax.crypto.BadPaddingException:错误:06065064:数字信封例程:EVP_DecryptFinal_ex:错误解密” 如果我没有完全错的话,两边的填充物是一样的(“PKCS”)

有人知道我做错了什么吗


PS:我用来加密的密钥是32个数字字符

代码显示KeySize=256。还要确保Jave中的块大小128是相同的。请参见@jdwen我没有正确地表述它,me KEYSTRING的长度为32个字符。导致长度为32的字节[]。我将C#中的键大小更改为128(32字节),但这导致了相同的异常。我还尝试将java中的“模式”更改为aes_128(以确保块大小为128),但这导致了“noSuchAlgorithm”——例外Rijndael算法支持128、192或256位的密钥长度;默认为256位。该算法支持128、192或256位的块大小;默认为128位(Aes兼容)。Rijndael和AES之间的差异见:
private string newEncrypt(string input)
    {
        RijndaelManaged aes = new RijndaelManaged();
        aes.BlockSize = 128;
        aes.KeySize = 256;
        aes.Mode = CipherMode.CBC;
        aes.Padding = PaddingMode.PKCS7;

        byte[] KeyArrBytes32Value = new byte[32];
        Array.Copy(Encoding.ASCII.GetBytes(keyString), KeyArrBytes32Value, 32);

        byte[] ivArr = { 1, 2, 3, 4, 4, 6, 6, 5, 4, 3, 2, 1, 7, 7, 7, 7 };
        byte[] IVBytes16Value = new byte[16];
        Array.Copy(ivArr, IVBytes16Value, 16);

        aes.Key = KeyArrBytes32Value;
        aes.IV = IVBytes16Value;

        ICryptoTransform cipher = aes.CreateEncryptor();

        byte[] plainTextByte = ASCIIEncoding.UTF8.GetBytes(input);
        byte[] CipherText = cipher.TransformFinalBlock(plainTextByte, 0, plainTextByte.Length);
        return Convert.ToBase64String(CipherText);
    }

    private string newDecrypt(string input)
    {
        RijndaelManaged aes = new RijndaelManaged();
        aes.BlockSize = 128;
        aes.KeySize = 256;

        aes.Mode = CipherMode.CBC;
        aes.Padding = PaddingMode.PKCS7;

        byte[] KeyArrBytes32Value = new byte[32];
        Array.Copy(Encoding.ASCII.GetBytes(keyString), KeyArrBytes32Value, 32);

        byte[] ivArr = { 1, 2, 3, 4, 4, 6, 6, 5, 4, 3, 2, 1, 7, 7, 7, 7 };
        byte[] IVBytes16Value = new byte[16];
        Array.Copy(ivArr, IVBytes16Value, 16);

        aes.Key = KeyArrBytes32Value;
        aes.IV = IVBytes16Value;

        ICryptoTransform cipher = aes.CreateDecryptor();

        byte[] encryptedBytes = Convert.FromBase64CharArray(input.ToCharArray(), 0, input.Length);
        byte[] decryptedData = cipher.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
        return ASCIIEncoding.UTF8.GetString(decryptedData);
    }