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

时间:2018-06-22 10:32:58

标签: 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 = { 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#中的代码:

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);
    }

我在c#应用程序中加密后,在解密android应用程序中的文本时遇到错误。错误如下: “ javax.crypto.BadPaddingException:错误:06065064:数字信封例程:EVP_DecryptFinal_ex:不良解密” 如果我没有完全错的话,则两侧的填充相同(“ PKCS”)。

有人知道我在做什么错吗?

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

0 个答案:

没有答案