我必须编写一个程序来使用 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));
}
}