以下JavaScript代码:
'use strict';
const CryptoJS = require('crypto-js');
const plaintext = 's3cret';
const password = 'MyPassword';
//const iv = CryptoJS.lib.WordArray.random(16);
//const salt = CryptoJS.lib.WordArray.random(16);
const iv = CryptoJS.enc.Hex.parse("43c9ccba630fe1cd61fc2bdb90121c6f"); // For testing
const salt = CryptoJS.enc.Hex.parse("5c788a415851e909d9c7951717714204"); // For testing
const key = CryptoJS.PBKDF2(password, salt, {keySize: 128/32, iterations: 1000});
const b64ciphertext = CryptoJS.AES.encrypt(plaintext, key, {iv: iv}).ciphertext.toString(CryptoJS.enc.Base64);
console.log(b64ciphertext);
字符串
1.使用[email protected]
,生成:
'use strict';
const plaintext = 's3cret';
const password = 'MyPassword';
//const iv = CryptoJS.lib.WordArray.random(16);
//const salt = CryptoJS.lib.WordArray.random(16);
const iv = CryptoJS.enc.Hex.parse("43c9ccba630fe1cd61fc2bdb90121c6f"); // For testing
const salt = CryptoJS.enc.Hex.parse("5c788a415851e909d9c7951717714204"); // For testing
const key = CryptoJS.PBKDF2(password, salt, {keySize: 128/32, iterations: 1000});
console.log("key: " + key.toString(CryptoJS.enc.Base64));
const b64ciphertext = CryptoJS.AES.encrypt(plaintext, key, {iv: iv}).ciphertext.toString(CryptoJS.enc.Base64);
console.log("ciphertext: " + b64ciphertext);
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.2.0/crypto-js.min.js" integrity="sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
的数据
key: 8E2dqz889iLAam1MV4wweQ==
ciphertext: eiJkaAmCVjHFKbvY/CnkvQ==
型
1.使用[email protected]
,生成:
'use strict';
const plaintext = 's3cret';
const password = 'MyPassword';
//const iv = CryptoJS.lib.WordArray.random(16);
//const salt = CryptoJS.lib.WordArray.random(16);
const iv = CryptoJS.enc.Hex.parse("43c9ccba630fe1cd61fc2bdb90121c6f"); // For testing
const salt = CryptoJS.enc.Hex.parse("5c788a415851e909d9c7951717714204"); // For testing
const key = CryptoJS.PBKDF2(password, salt, {keySize: 128/32, iterations: 1000});
console.log("key: " + key.toString(CryptoJS.enc.Base64));
const b64ciphertext = CryptoJS.AES.encrypt(plaintext, key, {iv: iv}).ciphertext.toString(CryptoJS.enc.Base64);
console.log("ciphertext: " + b64ciphertext);
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js" integrity="sha512-E8QSvWZ0eCLGk4km3hxSsNmGWbLtSCSUcewDQPQWZF6pEU8GlT8a5fF32wOl1i8ftdMhssTrF/OhyGWwonTcXA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
key: 8NJim/oe3/Q0+wLRq86FGg==
ciphertext: qT1Vtod7ihS2wvtwmlnPow==
型[[email protected]](https://stackoverflow.com/cdn-cgi/l/email-protection)
输出的Perl等价物由this answer提供:
use strict;
use warnings;
use feature qw( say );
use Crypt::CBC qw( );
use Crypt::PBKDF2 qw( );
use Mojo::Util qw(b64_encode);
my $plaintext = 's3cret';
my $password = 'MyPassword';
#my $iv = Crypt::CBC->random_bytes( 16 );
#my $salt = Crypt::CBC->random_bytes( 16 );
my $iv = pack( "H*", "43c9ccba630fe1cd61fc2bdb90121c6f" ); # For testing
my $salt = pack( "H*", "5c788a415851e909d9c7951717714204" ); # For testing
my $pbkdf2 = Crypt::PBKDF2->new(output_len=>128/8, iterations=>1000);
my $key = $pbkdf2->PBKDF2($salt,$password);
say "key: ", b64_encode($key,'');
my $cipher = Crypt::CBC->new(
-cipher => 'Cipher::AES',
-header => 'none',
-pbkdf => 'none',
-keysize => length($key),
-key => $key,
-iv => $iv
);
my $ciphertext = $cipher->encrypt( $plaintext );
my $b64ciphertext = b64_encode($ciphertext,'');
say "ciphertext: ", $b64ciphertext;
型
使用Perl,如何获得类似于[[email protected]](https://stackoverflow.com/cdn-cgi/l/email-protection)
的输出?
1条答案
按热度按时间kulphzqa1#
CryptoJS,v4.1.1默认使用SHA1作为PBKDF2摘要,CryptoJS,v4.2.0默认使用SHA256。为了在Perl代码中使用SHA256,必须添加
hash_class => 'HMACSHA2'
,即:字符串
完整性:在CryptoJS中,也可以使用
hasher
选项显式设置摘要:以下代码也将在v4.1.1下使用SHA256作为PBKDF2摘要:型
请注意,CryptoJS最近已经停止使用,不再维护。