java - AES 加密 Java -> PHP -> Java

标签 java php android encryption

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

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

然后 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

最佳答案

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

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

关于java - AES 加密 Java -> PHP -> Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9667288/

相关文章:

java - 无法在 GlobalMethodSecurityConfiguration 中 Autowiring UserDetailsS​​ervice

java - JMockit - 模拟 System.getProperties() 时为 "Missing invocation to mocked type"

javascript - Twitter-Typeahead 无法使用 Bloodhound 自动完成

php - Wordpress Redux 框架媒体查询

java - 如何设置带角的表面 View 相机背景

android - 如何正确设置 com.google.android.material.textfield.TextInputLayout 的样式?

java - 从 Angular 调用 Java EE Rest 服务

php - 在将数据发送到操作链接或人工 POST 请求之前解析数据

android - 在 android 中每次点击时始终在页面顶部显示滚动条

java - 正则表达式在 Java 类的单行/多行中查找注释