Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java(Android)aes-128-cbc解密为明文不一致_Java_Php_Encryption - Fatal编程技术网

Java(Android)aes-128-cbc解密为明文不一致

Java(Android)aes-128-cbc解密为明文不一致,java,php,encryption,Java,Php,Encryption,我有一个Java(Android)代码,可以使用aes-128-cbc将明文加密为密文。我能够用PHP将这个密文解密为相应的明文,但不能用Java本身 加密的Java代码如下: String iv = "0000000000000000"; //Ignore security concerns IvParameterSpec ivspec; ivspec = new IvParameterSpec(iv.getBytes()); String plaintext = "Top Secret D

我有一个Java(Android)代码,可以使用aes-128-cbc将明文加密为密文。我能够用PHP将这个密文解密为相应的明文,但不能用Java本身

加密的Java代码如下:

String iv = "0000000000000000"; //Ignore security concerns
IvParameterSpec ivspec;
ivspec = new IvParameterSpec(iv.getBytes());

String plaintext = "Top Secret Data";
byte[] encrypted = null;

Cipher cipher;
cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, seckey, ivspec);
encrypted = cipher.doFinal(padString(plaintext).getBytes());

ciphertext = Base64.encodeToString(encrypted, Base64.DEFAULT);

Log.i("Encrypted Base64 Data", ciphertext);
public function decrypt($data)
{
    $key = $_SESSION['sessionkey'];
    $iv = "0000000000000000";
    $data = base64_decode($data);

    $encobj = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);
    mcrypt_generic_init($encobj, $key, $iv);
    $decrypted = mdecrypt_generic($encobj, $data);

    mcrypt_generic_deinit($encobj);
    mcrypt_module_close($encobj);

    return utf8_encode(trim($decrypted));
}
能够成功解密的相应PHP代码如下:

String iv = "0000000000000000"; //Ignore security concerns
IvParameterSpec ivspec;
ivspec = new IvParameterSpec(iv.getBytes());

String plaintext = "Top Secret Data";
byte[] encrypted = null;

Cipher cipher;
cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, seckey, ivspec);
encrypted = cipher.doFinal(padString(plaintext).getBytes());

ciphertext = Base64.encodeToString(encrypted, Base64.DEFAULT);

Log.i("Encrypted Base64 Data", ciphertext);
public function decrypt($data)
{
    $key = $_SESSION['sessionkey'];
    $iv = "0000000000000000";
    $data = base64_decode($data);

    $encobj = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);
    mcrypt_generic_init($encobj, $key, $iv);
    $decrypted = mdecrypt_generic($encobj, $data);

    mcrypt_generic_deinit($encobj);
    mcrypt_module_close($encobj);

    return utf8_encode(trim($decrypted));
}
用于解密的有问题的Java代码反向为错误的明文,如下所示

String iv = "0000000000000000";
IvParameterSpec ivspec;
ivspec = new IvParameterSpec(iv.getBytes());

byte[] decrypted = null;

Cipher cipher;
cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, seckey, ivspec);
decrypted = cipher.doFinal(Base64.decode(ciphertext, Base64.DEFAULT));
Log.i("Decrypted Data",decrypted.toString());
如果有人能指出Java解密代码的错误,我将不胜感激。

试试这个

  Cipher cipher;
  cipher = Cipher.getInstance("AES/CBC/NoPadding");
  cipher.init(Cipher.DECRYPT_MODE, seckey, ivspec);
  decrypted = cipher.doFinal(Base64.decode(ciphertext, Base64.DEFAULT)); //byte[]
  String result = new String(decrypted);
  Log.i("Decrypted Data",result);

我猜您的加密和java中的解密代码之间有一个相似之处:那么解密的不是字节数组(byte[])吗?在这种情况下,您可能需要如下所示的字符串转换:string resp=new string(已解密)。这确实有效。道,非常感谢你。我应该早点发布此消息:)