Java和PHP之间的加密/解密字符串 [英] Encrypt/Decrypt string between Java and PHP

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

问题描述

我使用AES加密来加密和解密在服务器端和Android的应用程序(如客户端)PHP的字符串。

在PHP中的加密字符串为:

  HaxRKnMxT24kCJWUXaVvqDHahzurJQK + sYA4lIHql / U =
 

和Java中它的:

  HaxRKnMxT24kCJWUXaVvqD / KMEkJTPTXEcCsHIYGX9TGtCNOHQcJyUURPk8qlgf3
 

我利用phpseclib在PHP脚本来执行加密。

我是什么在这里失踪?

相关的Java code此处

  SecretKeySpec skeySpec =新SecretKeySpec(pad16(通),AES);
密码C = Cipher.getInstance(AES);
c.init(Cipher.ENCRYPT_MODE,skeySpec);
byte []的输出= c.doFinal(输入)
 

和PHP的code在这里:

  $ AES =新Crypt_AES();
$ AES-> setkey的('密码');
$ encrypted_en coded_text = base64_en code($ AES->加密($明文));
 

解决方案

有关的加密/解密在不同的语言工作,有几件事情需要是相同的。

  1. 在加密算法(废话!)
  2. 键(废话,又来了!)
  3. 密钥大小
  4. 操作(ECB,CBC,CTR)的模式
  5. 在初始化向量(如CBC,不需要ECB)
  6. 填充方案

    和一些可能更多的因素太多......

您确定所有这些都是通过这两个相同的语言?如果是的话,那么你的加密/解密应工作得很好,除非是在执行中的错误(这是非常非常罕见的,但可能)。

I'm using AES encryption to encrypt and decrypt string between php on the server side and Android app (as client).

The encrypted string in PHP is:

HaxRKnMxT24kCJWUXaVvqDHahzurJQK+sYA4lIHql/U=

and in Java it's:

HaxRKnMxT24kCJWUXaVvqD/KMEkJTPTXEcCsHIYGX9TGtCNOHQcJyUURPk8qlgf3

I'm making use of phpseclib in the PHP script to do the encryption.

What am I missing here?

The relevant Java code here

SecretKeySpec skeySpec = new SecretKeySpec(pad16(pass), "AES");
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] out = c.doFinal( input )

And the PHP code here:

$aes = new Crypt_AES();
$aes->setKey('password');
$encrypted_encoded_text =  base64_encode($aes->encrypt($plaintext));

解决方案

For the encryption/decryption to work across different languages, there are few things that needs to be the same.

  1. Encryption Algorithm (duh!)
  2. Key (duh, again!)
  3. Key Size
  4. Mode of Operation (ECB, CBC, CTR)
  5. Initialization Vector (If CBC, no need for ECB)
  6. Padding scheme

    and probably some more factors too....

Are you sure all those are the same across both the languages? If yes, then your encryption/decryption should work flawlessly unless there is a bug in the implementation (which is very very rare but possible).

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

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