简体   繁体   English

具有AES的CBC:Perl中的加密和Java中的解密

[英]CBC with AES : Encrypting in Perl and Decrypting in Java

I am encrypting a file in perl and want to decrypt in java. 我在perl中加密文件,并想在java中解密。 here is my encryption code: 这是我的加密代码:

== Encryption in Perl == == Perl中的加密==

$key = "1234567890123456";
$plain_text = "this is foo";
open ($fh, ">" . $output_file_path) || die ("open ($output_file_path):$!");
my $cipher = Crypt::CBC->new( -key => $key, -cipher => "Crypt::OpenSSL::AES");
$cipher->start("");
print $fh $cipher->crypt($plain_text);

And this is the decryption code I am using, but it is not working. 这是我正在使用的解密代码,但无法正常工作。

== Decryption in Java == == Java中的解密==

String key = "1234567890123456";
byte[] encrypted_bytes = READ_DATA_FROM_FILE
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivSpec = new IvParameterSpec(key.getBytes());
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
String plain_text = new String(cipher.doFinal(encrypted_bytes));

Can someone help me in this? 有人可以帮我吗?

You don't seem to be specifying the IV for the perl encryption, and you are not passing 'encrypt' to the perl start() method. 您似乎没有为perl加密指定IV,也没有将'encrypt'传递给perl start()方法。 those are the immediate problems that i notice. 这些是我注意到的直接问题。

this probably isn't the current problem, but will be a problem for working with "non-trivial" text: you are not being careful with your byte <-> char conversions in java ( String.getBytes() and new String() ). 这可能不是当前的问题,但是使用“非平凡的”文本将是一个问题:您对Java中的字节<-> char转换( String.getBytes()new String()并不小心new String() )。 you are using methods in java which use the default platform character encoding, which may not be what you want. 您正在使用Java中的方法,这些方法使用默认的平台字符编码,可能不是您想要的。 it's best to use an explicit charset. 最好使用显式字符集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM