I'm trying to encrypt and then decrypt string using Rijndael with custom key.
Dim obj,arr,i,r,str,enc,utf
dim bytes,bytesd,s,sc,sd
set obj=WScript.CreateObject("System.Security.Cryptography.RijndaelManaged")
Set utf = CreateObject("System.Text.UTF8Encoding")
s="This is a private message"
bytes=utf.GetBytes_4(s)
obj.GenerateKey()
obj.GenerateIV()
set enc=obj.CreateEncryptor()
set dec=obj.CreateDecryptor()
bytec=enc.TransformFinalBlock((bytes),0,lenb(bytes))
sc=utf.GetString((bytec))
msgbox sc
byted=dec.TransformFinalBlock((bytec),0,lenb(bytec))
sd=utf.GetString((byted))
msgbox sd
I rewrote this vbscirpt code, which works perfectly into jscript.
But in my jscript solution, I'm getting an error: "Padding is invalid and cannot be removed." The error is thrown during decryption at line var result = decryptor.TransformFinalBlock(bytes, 0, string.length);.
I don't know what am I doing wrong.
function CRYPTO(key) {
this.Rijndael = WScript.CreateObject("System.Security.Cryptography.RijndaelManaged");
this.Unicode = WScript.CreateObject("System.Text.UTF8Encoding");
var MD5 = WScript.CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider");
MD5.Initialize();
var bytes = MD5.ComputeHash_2(this.Unicode.GetBytes_4(key));
this.Rijndael.Key = bytes; this.Rijndael.IV = bytes;
}
CRYPTO.prototype.encrypt = function(string) {
var bytes = this.Unicode.GetBytes_4(string);
var encryptor = this.Rijndael.CreateEncryptor();
var result = encryptor.TransformFinalBlock(bytes, 0, string.length);
return this.Unicode.GetString(result);
}
CRYPTO.prototype.decrypt = function(string) {
var bytes = this.Unicode.GetBytes_4(string);
var decryptor = this.Rijndael.CreateDecryptor();
var result = decryptor.TransformFinalBlock(bytes, 0, string.length);
return this.Unicode.GetString(result);
}
var crypto = new CRYPTO(getMotherboardSerialNumber());
var before = "Hello World!";
WScript.Echo(before);
var after = crypto.encrypt(before);
WScript.Echo(after);
var back = crypto.decrypt(after);
WScript.Echo(back);
function getMotherboardSerialNumber() {
var WMI = GetObject("winmgmts:\\\\.\\root\\CIMV2");
var items = new Enumerator(WMI.ExecQuery("Select * from Win32_BaseBoard"));
return items.item().SerialNumber;
}
Thanks in advance, sorry for my english.
var standard = WScript.CreateObject("System.Security.Cryptography.Rijndael");