【问题标题】:AES Encryption Java -> PHP -> JavaAES 加密 Java -> PHP -> Java
【发布时间】:2012-03-28 20:46:17
【问题描述】:

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

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

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

然后我的 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;
}

/// <summary>
/// Encrypts plaintext using AES 128bit key and a Chain Block Cipher and returns a base64 encoded string
/// </summary>
/// <param name="plainText">Plain text to encrypt</param>
/// <param name="key">Secret key</param>
/// <returns>Base64 encoded string</returns>
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);
}

/// <summary>
/// Decrypts a base64 encoded string using the given key (AES 128bit key and a Chain Block Cipher)
/// </summary>
/// <param name="encryptedText">Base64 Encoded String</param>
/// <param name="key">Secret Key</param>
/// <returns>Decrypted String</returns>
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

【问题讨论】:

标签: java php android encryption


【解决方案1】:

我建议你看看http://phpaes.com/。它是一个完全用 PHP 实现的免费 AES 加密库;它速度快,使用起来非常简单。

至少,它可以让您离找出问题的真正根源更近一步。

【讨论】:

  • 还有一点需要注意:base64 encoding 有许多不同的形状和大小。在使用 base64 编码二进制数据时,您必须绝对确保您的客户端和服务器端代码都能正常工作。我建议从比加密数据更简单的东西开始,测试你的场所,并确保你已经涵盖了所有更基本的基础。
【解决方案2】:

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

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

【讨论】:

  • SSL/HTTPS 不会取代对称加密,在某些情况下,甚至 SSL/HTTPS 通道都不能被信任。
猜你喜欢
  • 2011-06-05
  • 2013-07-24
  • 1970-01-01
  • 1970-01-01
  • 2012-06-01
  • 2017-05-31
  • 1970-01-01
  • 1970-01-01
  • 2013-04-19
相关资源
最近更新 更多