我需要使用密钥在android中加密某些文本。在PHP中的加密代码看起来像这样Android中的base64和mcrypt_encrypt
$this->securekey = hash('sha256',$textkey,TRUE);
$this->iv = mcrypt_create_iv(32, MCRYPT_DEV_URANDOM);
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->securekey, $input, MCRYPT_MODE_ECB, $this->iv));
对于Base64的加我从apache.org(公地编解码器1.6.jar)NetBeans中的公共编解码器,我的Android应用程序。代码中没有错误。但是当我运行应用程序并调用使用编解码器的函数时,应用程序会停止并需要关闭。
在logcat的说:
Android Runtime: java.lang.NoSuchMethodError:
org.apache.commons.codec.binary.Base64.decodeBase64
这里是我的代码:
public static String crypt(String input, String key){
byte[] crypted = null;
try{
SecretKeySpec skey = new SecretKeySpec(org.apache.commons.codec.binary.Base64.decodeBase64(key), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skey);
crypted = cipher.doFinal(input.getBytes());
}catch(Exception e){
}
return org.apache.commons.codec.binary.Base64.encodeBase64String(crypted);
}
我不知道如果我的代码做同样的加密作为PHP代码。我发现Android和PHP之间的链接为http://www.androidsnippets.com/encrypt-decrypt-between-android-and-php,但它不使用Base64,仅用于mcrypt_encrypt。任何人都可以帮助我获得与PHP服务器相同的加密。
在此先感谢。
首先,如果我的生活依赖于密码相关的信息,我不会相信“android snippets”。可怕的东西。 –