AES加密的Java - > PHP - > Java的 [英] AES Encryption Java -> PHP -> Java

查看:118
本文介绍了AES加密的Java - > PHP - > Java的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用我与Web服务通信的发送和回复进行加密,AES加密数据。

所以,我做的是以下几点。我送一个base64连接codeD AES加密JSON字符串share.php

Share.php然后解密这个字符串,并将其插入到数据库中。在那之后,PHP将加密嗯嗯code的响应。

我的Andr​​oid应用程序则需要去code连接解密此消息。

但PHP响应的解密不会很好。

这是我的 AES.java

 公共类AES {
私人最终字符串的characterEncoding =UTF-8;
私人最终字符串cipherTransformation =AES / ECB / PKCS5Padding;
私人最终字符串aesEncryptionAlgorithm =AES;

公共字节[]解密(byte []的密文,字节[]键,byte []的initialVector)抛出抛出:NoSuchAlgorithmException,NoSuchPaddingException,InvalidKeyException将,InvalidAlgorithmParameterException使用,IllegalBlockSizeException,BadPaddingException
{
    密码加密= Cipher.getInstance(cipherTransformation);
    SecretKeySpec secretKeySpecy =新SecretKeySpec(键,aesEncryptionAlgorithm);
    // IvParameterSpec ivParameterSpec =新IvParameterSpec(initialVector);
    //cipher.init(Cipher.DECRYPT_MODE,secretKeySpecy,ivParameterSpec);
    cipher.init(Cipher.DECRYPT_MODE,secretKeySpecy);
    的System.out.println(做最后的:+密文);

    密文= cipher.doFinal(密文);
    返回密文;
}

公共byte []的加密(byte []的明文字节[]键,byte []的initialVector)抛出抛出:NoSuchAlgorithmException,NoSuchPaddingException,InvalidKeyException将,InvalidAlgorithmParameterException使用,IllegalBlockSizeException,BadPaddingException
{
    密码加密= Cipher.getInstance(cipherTransformation);
    SecretKeySpec secretKeySpec =新SecretKeySpec(键,aesEncryptionAlgorithm);
    // IvParameterSpec ivParameterSpec =新IvParameterSpec(initialVector);
    //cipher.init(Cipher.ENCRYPT_MODE,secretKeySpec,ivParameterSpec);
    cipher.init(Cipher.ENCRYPT_MODE,secretKeySpec);
    明文= cipher.doFinal(明文);
    返回明文;
}

私人字节[] getKeyBytes(字符串键)抛出UnsupportedEncodingException {
    byte []的keyBytes =新的字节[16];
    byte []的parameterKeyBytes = key.getBytes(的characterEncoding);
    System.arraycopy(parameterKeyBytes,0,keyBytes,0,Math.min(parameterKeyBytes.length,keyBytes.length));
    返回keyBytes;
}

///<总结>
///加密使用AES 128位密钥和一个连锁分组密码的明文,并返回一个base64连接codeD字符串
///< /总结>
///< PARAM NAME =明文>纯文本加密< /参数>
///< PARAM NAME =键>秘密密钥LT; /参数>
///<返回>的Base64 EN codeD字符串< /回报>
公共字符串加密(明文字符串,字符串键)抛出UnsupportedEncodingException,InvalidKeyException将,抛出:NoSuchAlgorithmException,NoSuchPaddingException,InvalidAlgorithmParameterException使用,IllegalBlockSizeException,BadPaddingException {
    byte []的plainTextbytes = plainText.getBytes(的characterEncoding);
    byte []的keyBytes = getKeyBytes(密钥);
    //返回Base64.en codeToString(加密(plainTextbytes,keyBytes,keyBytes),Base64.DEFAULT);
    返回Base64.en codeToString(加密(plainTextbytes,keyBytes,新的字节[0]),Base64.DEFAULT);
}

///<总结>
///解密一个base64连接codeD字符串中使用给定的密钥(AES 128位密钥和手拉葫芦密码)
///< /总结>
///< PARAM NAME =encryptedText>的Base64恩codeD字符串< /参数>
///< PARAM NAME =键>秘密钥匙< /参数>
///<返回>解密的字符串< /回报>
公共字符串解密(字符串encryptedText,字符串键)抛出KeyException,GeneralSecurityException,GeneralSecurityException,InvalidAlgorithmParameterException使用,IllegalBlockSizeException,BadPaddingException,IOException异常{
    byte []的cipheredBytes = Base64.de code(encryptedText,Base64.DEFAULT);
    byte []的keyBytes = getKeyBytes(密钥);
    //返回新的String(解密(cipheredBytes,keyBytes,keyBytes)的characterEncoding);
    返回新的String(解密(cipheredBytes,keyBytes,新的字节[0])的characterEncoding);
}
 

}

这是code到EN code连接加密在PHP中的响应:

 函数mc_encrypt($加密,$ mc_key){
    $ IV = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB),MCRYPT_RAND);
    $ passcrypt =修剪(mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$ mc_key,修剪($加密),MCRYPT_MODE_ECB,$ IV));
    $ EN code = base64_en code($ passcrypt);
    返回$ EN code;
}

功能mc_decrypt($解密,$ mc_key){
    $日codeD = base64_de code($解密);
    $ IV = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB),MCRYPT_RAND);
    $解密=修剪(mcrypt_decrypt(MCRYPT_RIJNDAEL_128,$ mc_key,修剪($日codeD),MCRYPT_MODE_ECB,$ IV));
    返回$解密;
}
 

我猜的PHP加密设置不匹配的设置Java的一部分。能否

我收到以下错误:

  03-12 13:44:09.661:W / System.err的(15717):javax.crypto.BadPaddingException:垫块损坏
 

解决方案

我建议你看看的http:// phpaes。 COM / 。这是纯粹在PHP实现一个自由​​的AES加密库;它的速度快,使用起来非常简单。

最起码,它可以让你更近了一步隔离问题的真正原因。

In my Android app I am communicating with a web service the data sent and responded are encrypted with AES encryption.

So what I do is the following. I'm sending a base64 encoded AES encrypted JSON String to share.php

Share.php will then decrypt this string and insert it into the database. After that the PHP will encrypt en encode the response.

My Android application then needs to decode en decrypt this message.

But the decryption of the PHP response is not going very well.

This is my 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);
}

}

And this is the code to encode en encrypt the response in 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;
}

I'm guessing that the settings of the PHP encryption do not match the settings for the Java part. Can

I'm getting the following error:

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

解决方案

I suggest you take a look at http://phpaes.com/. It's a free AES encryption library implemented purely in PHP; it's fast and very very simple to use.

At the very least, it allows you get one step closer to isolating the true source of the issue.

这篇关于AES加密的Java - &GT; PHP - &GT; Java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆