使用 PHP 加密并使用 Java 解密
我必须编写一个程序来使用 JAVA 解密消息。该消息使用 PHP 中实现的 Triple DES / ECB 进行加密。我在算法、模式和填充模式上尝试了一些不同的设置。我没有得到正确的结果。缺什么?
这是加密消息的 PHP 程序:
$config_mcrypt_ecb_key = "12345678901234567890";
$data = "hello";
echo "Data Before Encrypt: " . $data . "\n";
$td = mcrypt_module_open('tripledes', '', 'ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_ENCRYPT);
mcrypt_generic_init($td, $config_mcrypt_ecb_key, $iv);
$data_encrypt = bin2hex(mcrypt_generic($td, $data));
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
echo "Data After Encrypt: " . $data_encrypt . "\n";
下面是解密消息的 java 程序:(我正在使用 BouncyCastleProvider)
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.security.*;
public class DecryptionTest {
public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException {
String password = "12345678901234567890";
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
SecretKeySpec key = new SecretKeySpec(password.getBytes(), "ECB");
Cipher m_decrypter = Cipher.getInstance("DESede/ECB/ZeroBytePadding");
m_decrypter.init(Cipher.DECRYPT_MODE, key);
byte[] decryptedText = m_decrypter.doFinal("bdf0baf948bff7e7".getBytes());
System.out.println(new String(decryptedText));
}
}
I have to write a program to decrypt a message using JAVA. The message is encrypted using Triple DES / ECB implemented in PHP. I have tried a few different settings on the algorithm, mode, and padding schema. I do not get the correct result. What is missing?
Here is the PHP program that encrypt the message:
$config_mcrypt_ecb_key = "12345678901234567890";
$data = "hello";
echo "Data Before Encrypt: " . $data . "\n";
$td = mcrypt_module_open('tripledes', '', 'ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_ENCRYPT);
mcrypt_generic_init($td, $config_mcrypt_ecb_key, $iv);
$data_encrypt = bin2hex(mcrypt_generic($td, $data));
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
echo "Data After Encrypt: " . $data_encrypt . "\n";
And below is the java program to decrypt the message: (I'm using the BouncyCastleProvider)
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.security.*;
public class DecryptionTest {
public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException {
String password = "12345678901234567890";
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
SecretKeySpec key = new SecretKeySpec(password.getBytes(), "ECB");
Cipher m_decrypter = Cipher.getInstance("DESede/ECB/ZeroBytePadding");
m_decrypter.init(Cipher.DECRYPT_MODE, key);
byte[] decryptedText = m_decrypter.doFinal("bdf0baf948bff7e7".getBytes());
System.out.println(new String(decryptedText));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
执行解密时您似乎没有使用 IV(初始化向量)。我的理解是,
mcrypt_create_iv()
会生成一个随机IV,因此您需要将其与加密数据一起存储,以便在解密时使用。或者(如果您愿意使用较弱的加密),请从 PHP 端省略 IV。
You don't seem to be using an IV (Initialisation Vector) when performing the decrypt. My understanding is that
mcrypt_create_iv()
will generate a random IV, so you need to store that with the encrypted data so that it can be used when decrypting.Alternatively (if you're happy with using a weaker encryption) omit the IV from your PHP side.