Problem Statement
I need to encrypt details in Laravel using RSA algorithm and then decrypt in Javascript.
Efforts Done
In laravel I am using phpseclib
library to encrypt.
Laravel code to encrypt string is given below...
$publicKey= file_get_contents(storage_path('app/' . $disk .'/'. $filename));
$rsa = new RSA();
$rsa->loadKey($publicKey); // public key
$plaintext = 'hello world';
$ciphertext = $rsa->encrypt($plaintext);
$ciphertext = base64_encode($ciphertext);
echo '<br>base64_encode = '. $ciphertext;
Given below is the output of this script.
base64_encode = FDtZ1XAzW6yH7LIwIBb9VjL64KQb/UPclpV4Stc2xSoiAdIQc2r1dYFgrsJflkQfLtA/7ZJ5IHWQUWjNUuUTWmMYKk2xRmCjShTEhSkI9JEJC70ltjhxB7BXPRLIofpFdc8HVq8koEZOFTvtZ5YmJt86/iqLby0t41VhlDqkN2U=
Private Key
-----BEGIN RSA PRIVATE KEY----- MIICXgIBAAKBgQDNIdcnHmDD+XzO+nBTt2VOjcCfJQs2kefZbVWzSgczLeYBfPsg MNX5r/q5oB65pfs07iMO9fBi5mpIG9fQ8sEbGfEupDQjBdPE6v2TCn/FR0JmH6OO tbKZVmn2mC2KxmB2PDluUM3dN5YJtezZnJn/vGYCTse4W8AYL9ljYTnRbQIDAQAB AoGAOHWV+EheLdJfTcCUZhZWJdbSK3tQqMZj3+495kj7zlRVuBckL2oBmuWaxriR fFt41sqsj5OVhEMTAaHYNsR57XMaZa+Frv49vv8TO3YK41oI4fIhdI9hxbc5zcYR GJMVnRrzxKcAQ7gxZzxDvjqnS3Qv6gAuX+ohBNeYNJoFdSkCQQD4Wutn852uRDIW DJiIizp1zW4wyRYtXUk684z1GpXYnnyFStvtnVMwNxx+fS19up89Ur13uq275Qo1 DQDrA8lHAkEA03JRD0zGkhuMZ74pouBXUMlbH179BQWQsj95cvMvdH8SCDZ5tdxX KECmASawUN1NqAsI4/0sVo24rOy9fNopqwJBALglKeDE2IiLFhwVMNAlc64/KNHR d7khLxgf18cw9CROtwNaEyHnAfSBGIaNKDrOMh4VCFkLOKBV3jChuKmJACcCQQDR YOHjxKLdC2p9UFDqeDv+Q2V4KxdhYaFm4HPi3+yd7zZr64WaEBY+RcYcXbM6X+oF /OdQ2+KL0sqMceExQNcBAkEAwtV3+J7fIJ5TJIlCN6WZRbfLo3g8DzzzEkyA3CcS nSO4ZQoCclINsZWJBTk31eEVA4+Sna5MpOtGSFK2+5h8ew== -----END RSA PRIVATE KEY-----
I am using jsencrypt library to decrypt RSA string.
var crypt = new JSEncrypt();
crypt.setKey('-----BEGIN RSA PRIVATE KEY----- MIICXgIBAAKBgQDNIdcnHmDD+XzO+nBTt2VOjcCfJQs2kefZbVWzSgczLeYBfPsg MNX5r/q5oB65pfs07iMO9fBi5mpIG9fQ8sEbGfEupDQjBdPE6v2TCn/FR0JmH6OO tbKZVmn2mC2KxmB2PDluUM3dN5YJtezZnJn/vGYCTse4W8AYL9ljYTnRbQIDAQAB AoGAOHWV+EheLdJfTcCUZhZWJdbSK3tQqMZj3+495kj7zlRVuBckL2oBmuWaxriR fFt41sqsj5OVhEMTAaHYNsR57XMaZa+Frv49vv8TO3YK41oI4fIhdI9hxbc5zcYR GJMVnRrzxKcAQ7gxZzxDvjqnS3Qv6gAuX+ohBNeYNJoFdSkCQQD4Wutn852uRDIW DJiIizp1zW4wyRYtXUk684z1GpXYnnyFStvtnVMwNxx+fS19up89Ur13uq275Qo1 DQDrA8lHAkEA03JRD0zGkhuMZ74pouBXUMlbH179BQWQsj95cvMvdH8SCDZ5tdxX KECmASawUN1NqAsI4/0sVo24rOy9fNopqwJBALglKeDE2IiLFhwVMNAlc64/KNHR d7khLxgf18cw9CROtwNaEyHnAfSBGIaNKDrOMh4VCFkLOKBV3jChuKmJACcCQQDR YOHjxKLdC2p9UFDqeDv+Q2V4KxdhYaFm4HPi3+yd7zZr64WaEBY+RcYcXbM6X+oF /OdQ2+KL0sqMceExQNcBAkEAwtV3+J7fIJ5TJIlCN6WZRbfLo3g8DzzzEkyA3CcS nSO4ZQoCclINsZWJBTk31eEVA4+Sna5MpOtGSFK2+5h8ew== -----END RSA PRIVATE KEY-----');
var text = 'FDtZ1XAzW6yH7LIwIBb9VjL64KQb/UPclpV4Stc2xSoiAdIQc2r1dYFgrsJflkQfLtA/7ZJ5IHWQUWjNUuUTWmMYKk2xRmCjShTEhSkI9JEJC70ltjhxB7BXPRLIofpFdc8HVq8koEZOFTvtZ5YmJt86/iqLby0t41VhlDqkN2U=';
text = atob(text);
var dec = crypt.decrypt(text);
console.log(dec);
Output of js code in console is given below...
null
Requirement
This is required to decrypt string in separate offline system. I am not going to decrypt with javascript on live website. Rather decryption with javascript will be done on offline system.