Android aes 加密垫块损坏

发布于 2024-11-01 07:13:54 字数 1523 浏览 0 评论 0原文

我正在使用下面的方法,如果我输入正确的密钥,一切都会正常工作。 但是,如果我输入错误的密钥,我会收到 BadPaddingException:填充块已损坏... 我做错了什么吗?

public  void initKey(String passwd, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException{

    byte[] localsalt = salt; 
   PBEKeySpec password = new PBEKeySpec(passwd.toCharArray(),localsalt, 1024,128);//, localsalt, 1000, 128);  //128bit enc aes
   SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWithMD5And128BitAES-CBC-OpenSSL","BC");  
   PBEKey key = (PBEKey) factory.generateSecret(password);  
   encKey = new SecretKeySpec(key.getEncoded(), "AES");
}


public   String txt2enc(String etxt) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {

       final Cipher cipher = Cipher.getInstance("AES");//AES       
       cipher.init(Cipher.ENCRYPT_MODE, encKey);      

       byte[] encrypted = cipher.doFinal((etxt).getBytes("UTF-8"));
       return Base64.encodeToString(encrypted, 0);
}

//decryption
public  String txt2dec(String dtxt) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException{

    final Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, encKey);
    byte[] decrypt = cipher.doFinal(Base64.decode(dtxt, 0));
    return new String(decrypt);//return Base64.encodeToString(decrypt, 0);
}

I am using the methods below and if I enter the right key everything works fine.
But if I enter a wrong key I am receiving a BadPaddingException:pad block corrupted...
Am I doing something wrong?

public  void initKey(String passwd, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException{

    byte[] localsalt = salt; 
   PBEKeySpec password = new PBEKeySpec(passwd.toCharArray(),localsalt, 1024,128);//, localsalt, 1000, 128);  //128bit enc aes
   SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWithMD5And128BitAES-CBC-OpenSSL","BC");  
   PBEKey key = (PBEKey) factory.generateSecret(password);  
   encKey = new SecretKeySpec(key.getEncoded(), "AES");
}


public   String txt2enc(String etxt) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {

       final Cipher cipher = Cipher.getInstance("AES");//AES       
       cipher.init(Cipher.ENCRYPT_MODE, encKey);      

       byte[] encrypted = cipher.doFinal((etxt).getBytes("UTF-8"));
       return Base64.encodeToString(encrypted, 0);
}

//decryption
public  String txt2dec(String dtxt) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException{

    final Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, encKey);
    byte[] decrypt = cipher.doFinal(Base64.decode(dtxt, 0));
    return new String(decrypt);//return Base64.encodeToString(decrypt, 0);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谢绝鈎搭 2024-11-08 07:13:54

你没有做错任何事。这是预期的。从技术上讲,这与 Java 加密问题 重复(似乎不容易找到)。请参阅此答案以获得很好的解释。您可以考虑添加 MAC(消息身份验证代码)来安全地完成类似于校验和的操作。

You're not doing anything wrong. It's expected. This is technically a duplicate of Java Encryption issue (which doesn't seem to be easy to find). See this answer for a good explanation. You can consider adding a MAC (Message Authentication Code) to safely accomplish something more like a checksum.

清风不识月 2024-11-08 07:13:54

除了提供错误的密钥之外,我不这么认为:)

堆栈跟踪会有所帮助,这样我们就可以看到哪个函数抛出了 BadPaddingException。

这很可能是因为“坏”密钥与“好”密钥的长度不同 - “坏”密钥是来自 initKey() 还是其他地方?

最好的祝愿,

菲尔·莱洛

Other than supplying the wrong key, I don't think so :)

A stack trace would help, so we can see which function is throwing the BadPaddingException.

This may well be because the 'bad' key is a different length to the 'good' key - does the 'bad' key come out of initKey() or somewhere else?

Best wishes,

Phil Lello

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文