Java 无法解密OpenSSL
我正在使用Java程序对数据进行加密,并将加密后的数据发送到Linux机器,在那里,数据应该使用OpenSSL解密回原始文本。Java 无法解密OpenSSL,java,linux,encryption,cryptography,encryption-symmetric,Java,Linux,Encryption,Cryptography,Encryption Symmetric,我正在使用Java程序对数据进行加密,并将加密后的数据发送到Linux机器,在那里,数据应该使用OpenSSL解密回原始文本。 若我在Java程序中进行加密和解密,它运行良好。但在Linux机器上,数据并没有被解密。 我得到以下例外: 解密错误 140209007396768:错误:0606506D:数字信封例程:EVP_DecryptFinal_ex:错误的最终块长度:EVP_enc.c:597: . 下面是我的Java代码: 公共类加密测试{ 密码c; 静态SecretKey-secKey
若我在Java程序中进行加密和解密,它运行良好。但在Linux机器上,数据并没有被解密。
我得到以下例外: 解密错误 140209007396768:错误:0606506D:数字信封例程:EVP_DecryptFinal_ex:错误的最终块长度:EVP_enc.c:597: .
下面是我的Java代码:
公共类加密测试{
密码c;
静态SecretKey-secKey;
静态iv参数规范iv;
公共静态void main(字符串[]args)引发异常
{
KeyGenerator keygen=KeyGenerator.getInstance(“DES”);
secKey=keygen.generateKey();
byte[]encoded=secKey.getEncoded();
字符串KeyString=bytesToHex(编码);
System.out.println(键串);
SecureRandom rnd=新的SecureRandom();
iv=新的IvParameterSpec(rnd.generateSeed(8));
字节[]IVENCODE=iv.getIV();
字符串ivString=bytesToHex(ivencoded);
System.out.println(ivString);
新建EncryptionTest().start();
}
//将密钥编码为十六进制,以便我可以在Linux机器上使用它来解密数据。
公共静态字符串bytesToHex(字节[]字节){
char[]hexArray=“0123456789ABCDEF.toCharArray();
char[]hexChars=新字符[bytes.length*2];
对于(int j=0;j>>4];
hexChars[j*2+1]=hexArray[v&0x0F];
}
返回新字符串(hexChars);
}
public void start()引发异常
{
System.out.println(“加密/解密应用程序”);
c=Cipher.getInstance(“DES/CBC/pkcs5pAdd”);
字符串输入=“要加密的数据”;
byte[]text=input.getBytes();
System.out.println(“--------------------------------------------------”);
System.out.println(“Text:+新字符串(Text));
字节[]textEncrypted=加密(文本,c);
String encryptedString=bytesToHex(textEncrypted);
System.out.println(“文本加密:“+encryptedString”);
字节[]textDecrypted=解密(textEncrypted,c);
System.out.println(“文本解密:+新字符串(文本解密));
System.out.println(“--------------------------------------------------”);
}
公共字节[]加密(字节[]b,密码c)引发异常
{
c、 init(Cipher.ENCRYPT_模式,secKey,iv);
字节[]encryptedText=null;
试一试{
encryptedText=c.doFinal(b);
}捕获(非法块大小异常e){
System.out.println(“错误-发生错误”);
系统出口(0);
}
返回加密文本;
}
公共字节[]解密(字节[]b,密码c)引发异常
{
c、 init(Cipher.DECRYPT_模式,secKey,iv);
字节[]解密文本=c.doFinal(b);
返回解密文本;
}
}
在Linux机器上:openssl enc-des cbc-d-in test.txt-out received.txt-iv“ivkeycenerated from Java”-K“key Generated from Java” 但解密并没有在Linux机器上发生。
请指出我哪里做错了。解密前是否将密文转换为二进制?尝试解密文件时收到的错误消息是什么?@MaartenBodewes。。不,我在解密之前没有将密文转换成二进制。我会试试看。@JonathanRosene。。我收到错误消息:错误解密140209007396768:错误:0606506D:数字信封例程:EVP_DecryptFinal_ex:错误的最终块长度:EVP_enc.c:597:。@MaartenBodewes。。谢谢你的建议。我试着将密文转换成二进制,但仍然出现同样的错误。
public class EncryptionTest{
Cipher c;
static SecretKey secKey;
static IvParameterSpec iv;
public static void main(String[] args) throws Exception
{
KeyGenerator keygen=KeyGenerator.getInstance("DES");
secKey=keygen.generateKey();
byte[] encoded = secKey.getEncoded();
String KeyString=bytesToHex(encoded);
System.out.println(KeyString);
SecureRandom rnd = new SecureRandom();
iv = new IvParameterSpec(rnd.generateSeed(8));
byte[] ivencoded=iv.getIV();
String ivString=bytesToHex(ivencoded);
System.out.println(ivString);
new EncryptionTest().start();
}
// encoded the key to hex so that i can use at Linux machine to decrypt the data.
public static String bytesToHex(byte[] bytes) {
char[] hexArray = "0123456789ABCDEF".toCharArray();
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
public void start() throws Exception
{
System.out.println("Encryption/Decryption App");
c = Cipher.getInstance("DES/CBC/PKCS5Padding");
String input ="Data to be ecrypted";
byte[] text = input.getBytes();
System.out.println("--------------------------------------------");
System.out.println("Text : " + new String(text));
byte[] textEncrypted = encrypt(text, c);
String encryptedString=bytesToHex(textEncrypted);
System.out.println("Text Encrypted : " + encryptedString);
byte[] textDecrypted = decrypt(textEncrypted, c);
System.out.println("Text Decrypted : " + new String(textDecrypted));
System.out.println("--------------------------------------------");
}
public byte[] encrypt(byte[] b, Cipher c) throws Exception
{
c.init(Cipher.ENCRYPT_MODE, secKey,iv);
byte[] encryptedText = null;
try {
encryptedText = c.doFinal(b);
} catch (IllegalBlockSizeException e) {
System.out.println("ERROR - error occured");
System.exit(0);
}
return encryptedText;
}
public byte[] decrypt(byte[] b, Cipher c) throws Exception
{
c.init(Cipher.DECRYPT_MODE, secKey,iv);
byte[] decryptedText = c.doFinal(b);
return decryptedText;
}
}