php mcrypt_加密与android AES-128加密不匹配
我需要在所有的api请求和响应加密我使用以下代码。但php代码的加密值和android生成的不匹配php mcrypt_加密与android AES-128加密不匹配,php,android,encryption,cryptography,Php,Android,Encryption,Cryptography,我需要在所有的api请求和响应加密我使用以下代码。但php代码的加密值和android生成的不匹配 function encrypt($input,$key) { $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); $input = $this->pkcs5_pad($input, $size); $td = mcrypt_module_open(MCRYPT_RIJND
function encrypt($input,$key) {
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$input = $this->pkcs5_pad($input, $size);
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key);
$data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = base64_encode($data);
return $data;
}
function aesdecrypt($sStr,$sKey){
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
$decrypted= mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
$sKey,
base64_decode($sStr),
MCRYPT_MODE_CBC
);
$dec_s = strlen($decrypted);
$padding = ord($decrypted[$dec_s-1]);
$decrypted = substr($decrypted, 0, -$padding);
return $decrypted;
}
android代码(用于加密和解密):
密文被认为是无法与随机文本区分的。这是唯一或(在CBC模式下)随机IV的主要原因。如果您使用静态IV(和相同的密钥),您将获得相同(第一块)明文的相同(第一块)密文。换句话说,您将向攻击者泄漏信息
这就是为什么你应该使用随机IV,你可以安全地和密文一起存储。加密最好通过解密来测试。签名生成同样可以通过签名验证进行最佳测试。如果您成功生成两次相同的CBC密文,则表明有问题,而不是有问题。为什么不使用安全连接(https)?您使用的是随机IV,这是可取的。如果你想检查兼容性,你需要在一个加密,在另一个解密,并检查你是否得到了原始明文回来。请注意,使用PHP和Android可能意味着传输安全。仅仅CBC还不足以提供传输安全性——由于oracle攻击,它甚至可能无法提供机密性。不过,使用随机IV、AES和PKCS#7兼容的填充值得称赞。谢谢,我解决了这个问题,因为IV值不匹配。
public static String encode(String password, String text)
throws NoPassGivenException, NoTextGivenException {
/*if (password.length() == 0 || password == null) {
throw new NoPassGivenException("Please give Password");
}
if (text.length() == 0 || text == null) {
throw new NoTextGivenException("Please give text");
}*/
try {
SecretKeySpec skeySpec = getKey(password);
byte[] clearText = text.getBytes("UTF8");
//IMPORTANT TO GET SAME RESULTS ON iOS and ANDROID
final byte[] iv = new byte[16];
Arrays.fill(iv, (byte) 0x00);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
//System.out.println(iv);
// Cipher is not thread safe
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
String encrypedValue = new Base64().encodeAsString(
cipher.doFinal(clearText));
//Log.d(TAG, "Encrypted: " + text + " -> " + encrypedValue);
return encrypedValue;
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return "";
}
public static String decode(String password, String text)
throws NoPassGivenException, NoTextGivenException {
/*if (password.length() == 0 || password == null) {
throw new NoPassGivenException("Please give Password");
}
if (text.length() == 0 || text == null) {
throw new NoTextGivenException("Please give text");
}*/
try {
SecretKey key = getKey(password);
//IMPORTANT TO GET SAME RESULTS ON iOS and ANDROID
final byte[] iv = new byte[16];
Arrays.fill(iv, (byte) 0x00);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
//System.out.println(iv);
byte[] encrypedPwdBytes = new Base64().decodeBase64(text);
// cipher is not thread safe
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
byte[] decrypedValueBytes = (cipher.doFinal(encrypedPwdBytes));
String decrypedValue = new String(decrypedValueBytes);
// BigDecimal bd = new BigDecimal(decrypedValue);
//Log.d(TAG, "Decrypted: " + text + " -> " + decrypedValue);
// String data = Long.toString(bd.longValue());
return decrypedValue;
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return "";
}