Android 中采用 Base64 密钥加密的 AES
我正在制作一个Android应用程序,我应该在其中加密从用户那里获取的密码并将其发送到我的应用程序引擎。我想使用带有 Base64 密钥的 AES 技术。我是加密/解密新手,所以我使用了这个问题中询问的代码。我换了钥匙并用我的替换了它。这是我的代码:
public String encrypt(String dataToEncrypt)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
// I'm using AES encription
if (!dataToEncrypt.equals("")) {
String key = "rEqrHrhdd9I1sg==";
Cipher c = Cipher.getInstance("AES");
SecretKeySpec k;
try {
k = new SecretKeySpec(key.getBytes(), "AES");
c.init(Cipher.ENCRYPT_MODE, k);
} catch (Exception e) {
e.printStackTrace();
}
return new String(c.doFinal(Base64.decode(dataToEncrypt, 0)));
}
return "";
}
但有时当我尝试加密某些字符串时,我会收到错误“java.lang.IllegalArgumentException:bad base-64”,比如说“asdasdasd”在我加密时会出现此错误。谁能告诉我问题是什么?
-提前致谢
I am making an android application in which I am supposed to encrypt the passwords I take from the user and send it to my app engine. I want to the use the AES technique with a Base64 key. I am new to encryption/decryption so i used the code asked in this question. I changed the key and replaced it with mine. This is my code:
public String encrypt(String dataToEncrypt)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
// I'm using AES encription
if (!dataToEncrypt.equals("")) {
String key = "rEqrHrhdd9I1sg==";
Cipher c = Cipher.getInstance("AES");
SecretKeySpec k;
try {
k = new SecretKeySpec(key.getBytes(), "AES");
c.init(Cipher.ENCRYPT_MODE, k);
} catch (Exception e) {
e.printStackTrace();
}
return new String(c.doFinal(Base64.decode(dataToEncrypt, 0)));
}
return "";
}
But sometimes I get an error "java.lang.IllegalArgumentException: bad base-64" when i try to encrypt certain strings, say "asdasdasd" gives this error when i am encrypting it. Can anyone please tell me what the problem is??
-Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试将此作为有关如何使用字符串作为键和消息的示例。至少它使用正确的(字符)编码,使用 CBC 模式和 PKCS5/7 填充。请注意,将加密的密码发送到服务器存在很多问题。通常,应通过在服务器上使用 SSL 来实现机密性并使用 bcrypt 或 PBKDF2 来实现安全性(但这已在 stackoverflow 上一次又一次地介绍)。
请注意,下面的代码不提供完整性检查或真实性
。
Try this as an example on how to use Strings for keys and messages. At least it uses the right (character) encoding, uses CBC mode and PKCS5/7 padding. Note that there are a lot of issues with sending passwords encrypted to a server. Normally, security should be achieved by using SSL for confidentiallity and bcrypt or PBKDF2 on the server (but this has been covered again and again on stackoverflow).
Note that the code below does not provide integrity checks or authenticity
.
simplecrypto.java
并在活动中请输入以下代码:
abc 是要加密的文本, xyz 是加密的密钥
simplecrypto.java
and in activity please put following code :
abc is the text to be encrypt and xyz is the key for encryption
看看我在这里的答案Android数据库加密。它包含 2 个文件,您可以将它们包含在任何需要加密数据存储的应用程序中。有一个实现的方法可以更轻松地将字节数组数据转换为可打印的 Base64 数据,反之亦然。使用带有密码块链接 (CBC) 加密模式和 PKCS#5 填充的 AES 算法。
Look at my answer here Android database encryption. It contains 2 files that you can include in any of your applications that require data storage to be encrypted. There is implemented method that make it easier to convert the byte array data into printable Base64 data and vice versa. Used the AES algorithm with Cipher Block Chaining (CBC) encryption mode and PKCS#5 padding.
你好,我重写了 owlstead java 方法示例,没有使用 DatatypeConverter,而是使用 apache commons。
你甚至不能在 android 上使用它,因为你可能会遇到 Base64 类的问题。在 android 上,您可以使用以下使用 Base64 android 类的方法:
希望这有帮助!
Hi i rewrite the owlstead java methods example without DatatypeConverter and with apache commons.
You can not even use this on android because you could have some problem with Base64 Class. On android you could use the following methods that use Base64 android class:
Hope this help!