首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用AES时出现解密错误

使用AES时出现解密错误
EN

Stack Overflow用户
提问于 2016-02-22 17:43:53
回答 1查看 67关注 0票数 0
代码语言:javascript
复制
/**
 * 初始化 AES Cipher
 * @param sKey
 * @param cipherMode
 * @return
 */
public static Cipher initAESCipher (String sKey, int cipherMode) throws Exception {
    //创建Key gen
    KeyGenerator keyGenerator = null;
    Cipher cipher = null;
        keyGenerator = KeyGenerator.getInstance("AES");
        SecureRandom sr = SecureRandom.getInstance( "SHA1PRNG" ,"Crypto");
        sr.setSeed(sKey.getBytes("UTF-8"));
        keyGenerator.init(128,sr);
        SecretKey secretKey = keyGenerator.generateKey();
        byte[] codeFormat = secretKey.getEncoded();
        SecretKeySpec key = new SecretKeySpec(codeFormat, "AES");
        cipher = Cipher.getInstance("AES");
        //初始化
        cipher.init(cipherMode, key);
    return cipher;
}

/**
 * 对文件进行AES加密
 * @param sourceFile
 * @param fileType
 * @param sKey
 * @return
 */
public static File encryptFile(File sourceFile,String fileType, String sKey) throws Exception {
    //新建临时加密文件
    File encrypfile = null;
    InputStream inputStream = null;
    OutputStream outputStream = null;

        inputStream = new FileInputStream(sourceFile);
        encrypfile = new File(fileType);
        outputStream = new FileOutputStream(encrypfile);
        Cipher cipher = initAESCipher(sKey,Cipher.ENCRYPT_MODE);
        //以加密流写入文件
        CipherInputStream cipherInputStream = new CipherInputStream(inputStream, cipher);
        byte[] cache = new byte[1024];
        int nRead = 0;
        while ((nRead = cipherInputStream.read(cache)) != -1) {
            outputStream.write(cache, 0, nRead);
            outputStream.flush();
        }
        cipherInputStream.close();
            inputStream.close();
            outputStream.close();
            sourceFile.delete();

    return encrypfile;
}

我想使用AES加密和解密文件。这是我的代码。

代码语言:javascript
复制
/**
 * AES方式解密文件
 * @param sourceFile
 * @return
 */
public static File decryptFile(File sourceFile,String fileType,String sKey) throws Exception {
    File decryptFile = null;
    InputStream inputStream = null;
    OutputStream outputStream = null;
        decryptFile = new File(fileType);
        Cipher cipher = initAESCipher(sKey,Cipher.DECRYPT_MODE);
        inputStream = new FileInputStream(sourceFile);
        outputStream = new FileOutputStream(decryptFile);
        CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher);
        byte [] buffer = new byte [1024];
        int r;
        while ((r = inputStream.read(buffer)) >= 0) {
            cipherOutputStream.write(buffer, 0, r);
        }
        cipherOutputStream.close();
            inputStream.close();
            outputStream.close();
            sourceFile.delete();
    return decryptFile;
}

我使用它来加密和解密文件,但我得到了以下日志:

代码语言:javascript
复制
02-22 17:36:38.451 11972-11995/com.augur.android.agcom W/System.err: java.io.IOException: pad block corrupted

有人能帮我吗?我已经困惑了两个星期了。

EN

回答 1

Stack Overflow用户

发布于 2016-02-22 22:45:08

您尚未在实例化Cipher.getInstance("AES")时提供填充。最好同时提供填充选项和模式,因为基本文档声明:“特定于提供程序的模式和填充方案的默认值”,这可能会有所不同。

初始化字符串应包含填充和模式。

由于没有提供iv,可能正在使用ECB模式,这是不安全的,但初始化字符串将是:"AES/ECB/PKCS5Padding“。(PKCS#5填充是过时的,但本质上与PKCS#7相同)。

但不要使用ECB模式,这是不安全的,而是使用CBC与随机IV和MAC加密的字符串进行身份验证。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35550306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档