javax.crypto.BadPaddingException:错误:1e06b065:密码函数:EVP_DecryptFinal_ex:BAD_DECRYPT异常

时间:2017-02-23 05:21:54

标签: java c# android encryption

我有这段代码:

 public static void EncryptFile_Aes(string inputFile, string outputFile, byte[] key)
    {
        using (Aes aesAlg = Aes.Create())
        {
            aesAlg.Key = key;
            aesAlg.Mode = CipherMode.CBC;

            var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

            using (FileStream outputStream = new FileStream(outputFile, FileMode.Create))
            {
                using (var cryptoStream = new CryptoStream(outputStream, encryptor, CryptoStreamMode.Write))
                {
                    FileStream inputStream = new FileStream(inputFile, FileMode.Open);

                    int data;
                    while ((data = inputStream.ReadByte()) != -1)
                    {
                        cryptoStream.WriteByte((byte)data);
                    }
                }
            }
        }

使用密钥“MySecret”。我需要将此代码解密为java或android。但我得到了这个例外,

  

javax.crypto.BadPaddingException:错误:1e06b065:密码函数:EVP_DecryptFinal_ex:BAD_DECRYPT

java中的代码,

 private  byte[] getRawKey(byte[] seed) throws Exception {

    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG","Crypto");
    sr.setSeed(seed);
    kgen.init(128, sr); // 192 and 256 bits may not be available
    SecretKey skey = kgen.generateKey();
    byte[] raw = skey.getEncoded();
    return raw;
}
  private  byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {

    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  // Cipher cipher = Cipher.getInstance("AES");
    IvParameterSpec ivSpec = new IvParameterSpec(raw);

    Cipher cipher = Cipher.getInstance("CBC");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec,ivSpec);
    byte[] decrypted = cipher.doFinal(encrypted);
    return decrypted;
}

请帮我消除那个例外。

0 个答案:

没有答案