首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法克服javax.crypto.BadPaddingException

无法克服javax.crypto.BadPaddingException
EN

Stack Overflow用户
提问于 2016-05-26 09:48:12
回答 1查看 353关注 0票数 0

我正在编写一个隐写系统,它对图像中的文本进行加密--我决定在将文本嵌入图片之前也要对其进行加密。隐写算法适用于字符串输入/输出。

由于我的想法,我一直试图使用DES和AES算法,但遇到了上述例外。例如,我将展示AES算法的加密/解密方法:

代码语言:javascript
复制
    public static byte[] encryptText(String plainText,SecretKey secKey) throws Exception{
    // AES defaults to AES/ECB/PKCS5Padding in Java 7
    Cipher aesCipher = Cipher.getInstance("AES");
    aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
    byte[] byteCipherText = aesCipher.doFinal(plainText.getBytes());
    return byteCipherText;}


    public static String decryptText(byte[] byteCipherText, SecretKey secKey) throws Exception {
    // AES defaults to AES/ECB/PKCS5Padding in Java 7
    Cipher aesCipher = Cipher.getInstance("AES");
    aesCipher.init(Cipher.DECRYPT_MODE, secKey);
    byte[] bytePlainText = aesCipher.doFinal(byteCipherText);
    return new String(bytePlainText);
}

下面是调用(加密端):

代码语言:javascript
复制
  //sending the text to encrypt using AES algorithm
byte[] cipherText = new AES_Algorithm().encrypt(messageDesc.getText(), key);
  //converting into a String to encrypt using Steganography
newStringtoEncrypt = new String (cipherText);

下面是调用(解密端)-异常

代码语言:javascript
复制
  //AES encrypted String after the Steganography's decryption 
String decMessage = dec.decode(path, name);
  //converting the String to byte []
byte [] byteArray = decMessage.getBytes();
try{
    //HERE IS WHERE I GET THE EXCEPTION
    //sending the byte array to decryption
  String original = AES_Algorithm().decrypt(byteArray, key);

问题出在哪里?

密钥和字节数组是相似的(我通过打印序列来检查它)--但是当我打算使用字符串解密算法时,我被告知不能使用getBytes()来将字符串转换为byteArray

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-26 10:11:12

加密的输出是二进制的,不能像在下面的代码中那样将其转换为String

代码语言:javascript
复制
byte[] cipherText = new AES_Algorithm().encrypt(messageDesc.getText(), key);
//converting into a String to encrypt using Steganography
newStringtoEncrypt = new String (cipherText);

如果您需要加密数据为String,则需要以某种方式对其进行编码,例如base64encodinghexEncoding

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

https://stackoverflow.com/questions/37457236

复制
相关文章

相似问题

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