Two different programs, one in Javascript and the other in Perl, were designed to accomplish the same task with identical input data. Nevertheless, the output generated by these programs varied. The issue stemmed from using JavaScript on the client side to send a POST request to a Perl app on the server side, causing failure in decrypting the data without any clear explanation. Upon closer inspection, I determined that the libraries used in the two distinct programming languages exhibited different behaviors. To validate this observation, I rewrote the client side code in Perl. The outcomes are outlined below.
const CryptoJS = require('crypto-js');
function logBytes(description, wordArray) {
const bytes = CryptoJS.enc.Hex.stringify(wordArray);
console.log(description, bytes);
}
function encrypt(key32, iv16, data) {
// Remaining JavaScript encryption logic...
}
const data = "myemail@myserver.com";
// key32 and iv16 declarations...
encrypt(key32, iv16, data);
output:
% node test.js
Data: myemail@myserver.com
Key Bytes: 9d066ab6dc74593bbcef0876b4f7c00bada3acce6134fc64fa31a2cf995a39dd
IV Bytes: 9b2b9bdb4af5357cd78a8a2257c51a7f
Data Bytes: 6d79656d61696c406d797365727665722e636f6d
Encrypted (Base64): iit+mjBnWsMrMkJp63hpRmsCZgIxZ4FPZQId35qv12s=
#!/usr/bin/perl
use strict;
use warnings;
sub logMessage {
# Perl logMessage function implementation...
}
sub logBytes {
# Implementation for logging bytes in Perl...
}
sub encrypt {
# Remaining Perl encryption logic...
}
sub main {
# Main method definition for Perl program...
}
main();
exit (0);
output
% ./test.pm
Data: myemail@myserver.com
Key Bytes: 9d066ab6dc74593bbcef0876b4f7c00bada3acce6134fc64fa31a2cf995a39dd
IV Bytes: 9b2b9bdb4af5357cd78a8a2257c51a7f
Data Bytes: 6d79656d61696c406d797365727665722e636f6d
Encrypted (Base64): rk7JgOwsb7atyvEIXVNQkexbx5SYzufE05LZAoqtZGk=
Versions:
Perl:
Crypt::OpenSSL::AES version: 0.19
MIME::Base64 version: 3.16
JS:
JavaScript library versions listed here