AES256 Encryption / Decryption
Derives an AES-256 key from a passphrase with bin.sha256(), encrypts a JSON
payload, decrypts it and validates the checksum.
while(run()){
let aesKey = bin.sha256("flexscadaFlexsQ5!");
print("KEY: ", bin.toHex(aesKey));
let secretMsg = string( {msg: "I LOVE FLEXSCADA", code: 12.454 } );
let encrypted = bin.enc(secretMsg,aesKey);
let decrypted = bin.dec(encrypted,aesKey);
print("Size (Raw, Encrypted)",secretMsg.length,encrypted.length);
if(decrypted.valid){// decryption successful and checksums valid
let decryptedObj = JSON.parse(decrypted.data);
print(string(decryptedObj));
} else {
print("AES Decryption Failed.");
}
waitMS(1000);
}