如何从android中的AES加密字符串中删除PKCS7填充?

时间:2015-12-18 06:26:19

标签: android aes

我正在使用自己的自定义加密方法处理安全应用程序,并且在解密邮件时遇到问题。

这是我的代码

private static void myCryptography(){

Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
byte[] input = "Hitesh Dhamshaniya".getBytes();
byte[] keyBytes = "ABCD657865BHNKKK".getBytes();
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");

// encryption pass

cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
ctLength += cipher.doFinal(cipherText, ctLength);
Log.e("==> ", " == > Encode " + Base64.encodeToString(cipherText, Base64.DEFAULT));
String encodedStr = Base64.encodeToString(cipherText, Base64.DEFAULT);
// decryption pass

cipherText = Base64.decode(encodedStr, Base64.DEFAULT);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] plainText = new byte[cipher.getOutputSize(ctLength)];
int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
ptLength += cipher.doFinal(plainText, ptLength);
Log.e("==> ", " == > Decoded " + new String(plainText, "UTF-8"));

}

获得低于输出

==>编码TteNmufoa5AWWmEPBmQ3N8fdqRpahvwUR7CSclAcsjM =

==>解码HiteshDhamshaniya

如何从解码字符串中删除不需要的字符,如' '。

1 个答案:

答案 0 :(得分:3)

密码会自动删除填充。你在这里看到的是字节数组var redis = require("redis"); // You can use any module to create redis client app.use(session({ store: new redisStore({ client : redis.createClient(<your setting>) host:'localhost', port:'8543' }), resave: false, saveUninitialized: true, secret: 'somesecrettoken' })); 到字符串的转换。您应该只使用第一个String connectionUrl = "jdbc:derby:myDB;create=true;user="+"\"john.smith\""+";password="+"johnpassword"+";"; 字节而不是整个数组:

plainText