小编jmb*_*b95的帖子

使用相同密钥解密 AES 时出现 BadPaddingException

这是测试人员:

public class CryptographySimpleTests extends ActivityTestCase
{
    public void testsCryptographyClass_encryptAndDecrypt()
    {
        final String orgVal     = "hi world! :D";
        final String key        = "key";

        try
        {
            final byte[] encryptKey       = Cryptography.deriveAES256Key(key);
            final byte[] decryptKey       = Cryptography.deriveAES256Key(key);

            //Deviation method
            Assert.assertTrue(Arrays.equals(encryptKey, decryptKey));

            byte[] encrypted = Cryptography.encryptAES(encryptKey, orgVal.getBytes());

            Assert.assertFalse(Arrays.equals(encrypted, orgVal.getBytes()));

            byte[] decrypted = Cryptography.decryptAES(decryptKey, encrypted);

            Assert.assertTrue(Arrays.equals(orgVal.getBytes(), decrypted));
        }
        catch (Exception e) {
            Assert.fail(e.getMessage());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

由于最后一个断言而失败:

Assert.fail(e.getMessage());
Run Code Online (Sandbox Code Playgroud)

尝试执行时:

byte[] decrypted = Cryptography.decryptAES(decryptKey, encrypted);
Run Code Online (Sandbox Code Playgroud)

给出这个堆栈跟踪:

javax.crypto.BadPaddingException: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
        at com.android.org.conscrypt.NativeCrypto.EVP_CipherFinal_ex(Native Method)
        at …
Run Code Online (Sandbox Code Playgroud)

java android aes badpaddingexception

5
推荐指数
1
解决办法
9043
查看次数

将'final'修饰符与getter和setter一起使用是一个好主意吗?

我想知道为什么最终修饰符不与getter和setter一起使用?

为什么这样:

private int x;

public void setX(int x) 
{ 
  if(x >= 1) throw new IllegalArgumentException("X must be lower than 1");
  this.x = x; 
}
Run Code Online (Sandbox Code Playgroud)

而不是这个:

private int x;

public final void setX(int x) 
{ 
  if(x >= 1) throw new IllegalArgumentException("X must be lower than 1");
  this.x = x; 
}
Run Code Online (Sandbox Code Playgroud)

它没有改善封装?我一直试图用谷歌澄清它,但我没有运气.

谢谢你提前.

java oop encapsulation access-modifiers

3
推荐指数
1
解决办法
3105
查看次数