AES加密Java – > PHP – > Java

在我的Android应用程序中,我正在与Web服务进行通信,发送和响应的数据使用AES加密进行加密。

所以我做的是以下内容。 我正在向share.php发送base64编码的AES加密JSON字符串

然后Share.php将解密此字符串并将其插入数据库。 之后PHP将加密en编码响应。

然后我的Android应用程序需要解码以解密此消息。

但PHP响应的解密并不是很顺利。

这是我的AES.java

 public class AES { private final String characterEncoding = "UTF-8"; private final String cipherTransformation = "AES/ECB/PKCS5Padding"; private final String aesEncryptionAlgorithm = "AES"; public byte[] decrypt(byte[] cipherText, byte[] key, byte [] initialVector) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { Cipher cipher = Cipher.getInstance(cipherTransformation); SecretKeySpec secretKeySpecy = new SecretKeySpec(key, aesEncryptionAlgorithm); //IvParameterSpec ivParameterSpec = new IvParameterSpec(initialVector); //cipher.init(Cipher.DECRYPT_MODE, secretKeySpecy, ivParameterSpec); cipher.init(Cipher.DECRYPT_MODE, secretKeySpecy); System.out.println("Do final: "+cipherText); cipherText = cipher.doFinal(cipherText); return cipherText; } public byte[] encrypt(byte[] plainText, byte[] key, byte [] initialVector) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { Cipher cipher = Cipher.getInstance(cipherTransformation); SecretKeySpec secretKeySpec = new SecretKeySpec(key, aesEncryptionAlgorithm); //IvParameterSpec ivParameterSpec = new IvParameterSpec(initialVector); //cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); plainText = cipher.doFinal(plainText); return plainText; } private byte[] getKeyBytes(String key) throws UnsupportedEncodingException{ byte[] keyBytes= new byte[16]; byte[] parameterKeyBytes= key.getBytes(characterEncoding); System.arraycopy(parameterKeyBytes, 0, keyBytes, 0, Math.min(parameterKeyBytes.length, keyBytes.length)); return keyBytes; } ///  /// Encrypts plaintext using AES 128bit key and a Chain Block Cipher and returns a base64 encoded string ///  /// Plain text to encrypt /// Secret key /// Base64 encoded string public String encrypt(String plainText, String key) throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException{ byte[] plainTextbytes = plainText.getBytes(characterEncoding); byte[] keyBytes = getKeyBytes(key); //return Base64.encodeToString(encrypt(plainTextbytes,keyBytes, keyBytes), Base64.DEFAULT); return Base64.encodeToString(encrypt(plainTextbytes,keyBytes, new byte[0]), Base64.DEFAULT); } ///  /// Decrypts a base64 encoded string using the given key (AES 128bit key and a Chain Block Cipher) ///  /// Base64 Encoded String /// Secret Key /// Decrypted String public String decrypt(String encryptedText, String key) throws KeyException, GeneralSecurityException, GeneralSecurityException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, IOException{ byte[] cipheredBytes = Base64.decode(encryptedText, Base64.DEFAULT); byte[] keyBytes = getKeyBytes(key); //return new String(decrypt(cipheredBytes, keyBytes, keyBytes), characterEncoding); return new String(decrypt(cipheredBytes, keyBytes, new byte[0]), characterEncoding); } 

}

这是编码的代码加密PHP中的响应:

 function mc_encrypt($encrypt, $mc_key) { $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_RAND); $passcrypt = trim(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $mc_key, trim($encrypt), MCRYPT_MODE_ECB, $iv)); $encode = base64_encode($passcrypt); return $encode; } function mc_decrypt($decrypt, $mc_key) { $decoded = base64_decode($decrypt); $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_RAND); $decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $mc_key, trim($decoded), MCRYPT_MODE_ECB, $iv)); return $decrypted; } 

我猜测PHP加密的设置与Java部分的设置不匹配。 能够

我收到以下错误:

 03-12 13:44:09.661: W/System.err(15717): javax.crypto.BadPaddingException: pad block corrupted 

我建议你看看http://phpaes.com/ 。 它是纯粹用PHP实现的免费AES加密库; 它使用起来快速且非常简单。

至少,它可以让您更接近隔离问题的真正来源。

这可能不是您正在寻找的答案 – 但是您是否有特定原因手动加密此数据而不是使用SSL / HTTPS?

在大多数情况下,HTTPS比手动实现对称密码更容易实现,更安全。