我需要能够从Windows向各种手机发送安全信息。我是iPhone和Android开发的总新手,但需要为每个环境创建一个易于使用的应用程序。与接收到的短信接口也很好。我想要获取适用于iPhone,Android和Windows XP(及更高版本)的AES 256加密的代码。通缉兼容AES代码Iphone,Android,Windows/XP加密/解密
感谢
穆雷
我需要能够从Windows向各种手机发送安全信息。我是iPhone和Android开发的总新手,但需要为每个环境创建一个易于使用的应用程序。与接收到的短信接口也很好。我想要获取适用于iPhone,Android和Windows XP(及更高版本)的AES 256加密的代码。通缉兼容AES代码Iphone,Android,Windows/XP加密/解密
感谢
穆雷
不同的语言有加密类的不同实现。所以我不认为有一个单一的库可以在所有平台上工作。
您尚未指定您在Windows上为您的应用程序使用的语言。 T
这里不是简单的加密和解密方式。所以我建议你至少应该在加密算法如何处理不同的密钥大小,IV,操作模式和填充方面有一个坚实的基础。还有如何生成安全密钥,如何使用非对称密码等将密钥从一个用户传输到另一个用户等等。或者,您是否已经拥有密码学的理论知识?
对于iPhone
我没有在SDK中提供的加密类的想法。不过看看this question。
对于Android的
您有几种选择这里。
Here is a question你会感兴趣的。
对于Windows。
对于类似的问题,也请看my answer。
你应该在你的程序实施之前学习密码学。使用内置函数非常简单,但如果您不知道自己在做什么,那么您就会给自己一种错误的安全感,并可能会将您的客户数据置于风险之中。
对于iPhone我用AESCrypt-ObjC,以及针对Android使用此代码:
public class AESCrypt {
private final Cipher cipher;
private final SecretKeySpec key;
private AlgorithmParameterSpec spec;
public AESCrypt(String password) throws Exception
{
// hash password with SHA-256 and crop the output to 128-bit for key
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.update(password.getBytes("UTF-8"));
byte[] keyBytes = new byte[32];
System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);
cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
key = new SecretKeySpec(keyBytes, "AES");
spec = getIV();
}
public AlgorithmParameterSpec getIV()
{
byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
IvParameterSpec ivParameterSpec;
ivParameterSpec = new IvParameterSpec(iv);
return ivParameterSpec;
}
public String encrypt(String plainText) throws Exception
{
cipher.init(Cipher.ENCRYPT_MODE, key, spec);
byte[] encrypted = cipher.doFinal(plainText.getBytes("UTF-8"));
String encryptedText = new String(Base64.encode(encrypted, Base64.DEFAULT), "UTF-8");
return encryptedText;
}
public String decrypt(String cryptedText) throws Exception
{
cipher.init(Cipher.DECRYPT_MODE, key, spec);
byte[] bytes = Base64.decode(cryptedText, Base64.DEFAULT);
byte[] decrypted = cipher.doFinal(bytes);
String decryptedText = new String(decrypted, "UTF-8");
return decryptedText;
}
}
如果你还在寻找适合这两种设备的实现,iPhone和Android看看this post 。我和朋友一起创建了它。在iPhone的帖子下,你会发现Android的一部分。可以通过将它们插入到项目中来使用它们,如解释。
如果你想使用其他算法,你应该看看它们在iPhone和Android中的调用方式,并在方法内部的任何地方改变它。
实施AES加密时,不需要注意几点:
1.切勿使用纯文本作为加密密钥。始终散列纯文本密钥,然后用于加密。
2.始终使用随机IV(初始化向量)进行加密和解密。真正的随机化很重要。
我最近为C#,iOS和Android编写了跨平台的AES加密和解密库,我已经发布在Github上。你可以在这里看到 - https://github.com/Pakhee/Cross-platform-AES-encryption
工作得很好。谢谢! – gmogames 2015-03-18 21:02:13
太棒了!取我的+1 – 2016-10-07 14:15:59
工作完美无缺:) +1 – Pihu 2018-01-08 07:19:58