我正在开发一个android应用程序,并且我需要为它的一个方面使用加密。我真的对我使用的算法(AES,DES,RSA等)漠不关心。我知道Java有一个加密包,但我根本不熟悉它。有人可以发表一个关于如何执行加密/解密功能的例子吗?Android加密
回答
java AES
库有一个缺陷,它允许在正确的情况下侦听器解密发送的数据包。请参阅Padding Oracle Exploit Tool vs Apache MyFaces。
这就是说检查这个SO问题Java 256bit AES Encryption。 http://www.java2s.com/Code/Java/Security/EncryptionanddecryptionwithAESECBPKCS7Padding.htm
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class MainClass {
public static void main(String[] args) throws Exception {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
byte[] input = "www.java2s.com".getBytes();
byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
0x15, 0x16, 0x17 };
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
System.out.println(new String(input));
// 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);
System.out.println(new String(cipherText));
System.out.println(ctLength);
// decryption pass
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);
System.out.println(new String(plainText));
System.out.println(ptLength);
}
}
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()) 这是干什么用的? – 2010-06-30 17:49:22
安全。addProvider让java知道你正在计划使用特定的安全提供者(它基本上将安全提供者安装到安全管理器中,类似于加载库)。 – 2010-06-30 19:54:33
我刚才假设的是最奇怪的巧合:我观看了链接的YouTube视频,并且在顶行中的每个字节解密后(从右到左),我的电脑重新启动。当我检查系统日志时,这是由于CPU温度高于阈值。当我打开电脑开关热敏腻子5时,我看到显卡上的散热风扇被烧焦(字面上是棕色,起泡和翘曲)。我忍不住要妄想有一些错误的闪存软件利用.. – snapfractalpop 2012-02-27 02:04:09
看看这里Android database encryption我的回答:
充气城堡AES实例被盗。它包含2个文件,您可以将其包含在任何需要加密数据存储的应用程序中。
明天我要去测试它。这看起来很有趣。多谢兄弟。 – EliaszKubala 2014-12-22 20:36:06
我也会看看隐藏,看看它是否适合您的账单。它有一个易于使用的API,它抽象出加密的细节:https://github.com/facebook/conceal/
考虑到在Android中对数据进行加密和解密的开销,我设计了一个仅依靠Android和Java本机库的库来尽可能简化过程。
要安装,请使用jcenter分配中心。 在gradle这个:
compile 'com.tinmegali.android:mcipher:0.4'
使用
String ALIAS = "alias"
MEncryptor encryptor = new MEncryptorBuilder(ALIAS).build();
MDecryptor decryptor = new MDecryptorBuilder(ALIAS).build();
String toEncrypt = "encrypt this string";
// encrypting
String encrypted = encryptor.encryptString(toEncrypt, this);
// decrypting
String decrypted = decryptor.decryptString(encrypted, this);
MCipher是从SDK兼容19+,并自动适应更小的数据和大块。默认情况下,它对于SDK 23+使用AES/GCM/NoPadding
,对于较早版本使用RSA/ECB/PKCS1Padding
。
- 1. Android RC2加密
- 2. 的Android - 加密
- 3. Android rc4加密
- 4. Android加密BadPaddingException
- 5. android加密
- 6. 密码加密Android,PHP
- 7. Android/PHP RSA加密/解密?
- 8. 加密/解密public/private android
- 9. Android - 加密和解密
- 10. Android AES加密和解密
- 11. Android AES加密密钥
- 12. 加密蟒蛇/解密android
- 13. C#加密Android解密
- 14. 在android中加密解密
- 15. Android - 加密和解密
- 16. android AES加密/解密
- 17. Android的密码加密/解密
- 18. 在android中加密
- 19. android的sqlite加密
- 20. Android上的加密
- 21. 加密Android设置
- 22. Android AES 128加密
- 23. 加密Android代码?
- 24. Android的Sqlite加密
- 25. Android文件加密
- 26. android中的加密
- 27. Android加密3DES ECB
- 28. AES加密 - 在Android上存储密码
- 29. Android AES加密/解密 - inputstream&bytearrayoutputstream
- 30. android:使用openSSL的AES加密/解密
该算法在很大程度上取决于使用场景。你在保护什么,从谁,在哪里,为什么以及如何计划做到这一点? AES(对称密码)和RSA(不对称)功能完全不同。 – 2010-06-30 18:21:12
我只是在设备上本地存储用户名和密码。我有一个RSA实现,并将其用于我的目的。 – 2010-06-30 19:25:51
@Steven - 我不确定RSA是最好的系统。 – 2010-07-01 03:56:24