Java和PHP之间的加密不匹配 [英] Encryption mismatch between Java and PHP

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

问题描述

我正在使用将数据传递给第三方应用程序的加密系统。加密是用Java完成的,解密是用PHP完成的。尽管有几次尝试,我无法获得PHP应用程序打开的加密字符串。



为了测试目的,我创建了一个还加密数据的PHP脚本,所以我可以比较Java和PHP加密的字符串。结果匹配到第21个字符,然后它们不同。这是我所拥有的:

  // Java  - 加密
private String EncryptAES(String text,String key)throws异常
{
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(),AES);

//实例化密码
密码密码= Cipher.getInstance(AES);

cipher.init(Cipher.ENCRYPT_MODE,skeySpec);
byte [] encrypted = cipher.doFinal(text.getBytes());

String encrypttext = new BASE64Encoder()。encode(encrypted);

返回encrypttext;
}

结果:TeUZAFxoFoQy / roPm5tXyPzJP / TLAwR1aIGn2xHbZpsbY1qrKwXfO + F / DAqmeTwB0b8e6dsSM + Yy0zrQt22E2Q ==

  // PHP  - 加密
<?php

$ encrypt = $ crypt = openssl_encrypt($ toCrypt,AES256,key-32-char-long);
echo $ encrypt;

?>

结果:TeUZAFxoFoQy / roPm5tXyC05wta1Z5YOXcq4OtgFoSbfVi / bHAuD6B5tDthT8EcGXQir08UAx0QvcqRJ2fJmbQ ==

显然,做的正确,因为部分字符串匹配,但显然不是一切都是正确的,因为其余的不匹配。此外,如果我尝试解密PHP中的Java字符串,没有任何反应:

  // PHP  - 解密
< ;?php
$ toDecrypt =TeUZAFxoFoQy / roPm5tXyPzJP / TLAwR1aIGn2xHbZpsbY1qrKwXfO + F / DAqmeTwB0b8e6dsSM + Yy0zrQt22E2Q ==;
$ decrypt = openssl_decrypt($ toDecrypt,AES256,< key-32-char-long>);
echo $ decrypt;

?>

结果:< nothing>有没有人有任何想法可能发生什么?

解决方案

由于两个加密的字符串都以相同的字符开始,所以看起来您在一个使用ECB,另一个使用CBC。


I'm working on an encryption system that passes the data to a 3rd party application. The encryption is done in Java and the decryption is done in PHP. Despite several attempts I can't get the encrypted string to be opened by the PHP application.

For testing purposes I created a PHP script that also encrypts the data, so I could compare the Java and PHP encrypted strings. The results match up to the 21st character, and then they differ. This is what I have:

// Java - Encrypt
private String EncryptAES(String text,String key) throws Exception
    {
      SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");

      // Instantiate the cipher
      Cipher cipher = Cipher.getInstance("AES");

      cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
      byte[] encrypted = cipher.doFinal(text.getBytes());

      String encrypttext = new BASE64Encoder().encode(encrypted);

      return encrypttext;
    }

RESULT: TeUZAFxoFoQy/roPm5tXyPzJP/TLAwR1aIGn2xHbZpsbY1qrKwXfO+F/DAqmeTwB0b8e6dsSM+Yy0zrQt22E2Q== 

and

// PHP - Encrypt
<?php

$encrypt =  $crypt = openssl_encrypt($toCrypt,"AES256","key-32-char-long");
echo $encrypt; 

?>

RESULT: TeUZAFxoFoQy/roPm5tXyC05wta1Z5YOXcq4OtgFoSbfVi/bHAuD6B5tDthT8EcGXQir08UAx0QvcqRJ2fJmbQ==

Obviously something is being done right because part of the strings match, but obviously not everything is correct because the rest does not match. Also, if I try to decrypt the Java string in PHP, nothing happens:

// PHP - Decrypt
<?php
$toDecrypt = "TeUZAFxoFoQy/roPm5tXyPzJP/TLAwR1aIGn2xHbZpsbY1qrKwXfO+F/DAqmeTwB0b8e6dsSM+Yy0zrQt22E2Q==";
$decrypt = openssl_decrypt($toDecrypt,"AES256","<key-32-char-long>");
echo $decrypt;

?>

RESULT: <nothing>

Does anyone have any ideas what might be happening?

解决方案

Since both encrypted strings start with the same characters, it looks like you're using ECB in one and CBC in the other.

这篇关于Java和PHP之间的加密不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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