I am doing string encryption like below using the ...
# suitescript
s
I am doing string encryption like below using the guid and storing the cipherout.ciphertext value but not storing the cipherout.iv value. But when decrypting , why cant i just pass the key , instead of passing both key and cipherout.iv values. Because it is asking for both values while decrypting it. Do i need to store both key and cipherout.iv values for decrypting? Please advice FIRSTFILE.js :
Copy code
var guid = "4ab23a136dc347d";
var inputString = "sometext";

// Create the key
var key = crypto.createSecretKey({guid:guid, encoding:encode.Encoding.UTF_8});

// Encrypt
var cipher = crypto.createCipher({algorithm: crypto.EncryptionAlg.AES, key: key});
cipher.update({input: inputString, inputEncoding: encode.Encoding.UTF_8});
var cipherout = cipher.final({outputEncoding: encode.Encoding.HEX});
SECONDFILE.JS
Copy code
// Decrypt
var decipher = crypto.createDecipher({algorithm: crypto.EncryptionAlg.AES, key: key, iv:cipherout.iv}); //asking here 
decipher.update({input: cipherout.ciphertext, inputEncoding: encode.Encoding.HEX});
var decipherout = decipher.final({outputEncoding: encode.Encoding.UTF_8});
hi @Vaid . any idea on this ?
v
@samantha not much. By the looks of docs, your iv value should be in your cipherout variable, cipherout.iv is your iv for cypher
s
thanks @Vaid . I am thinking the same . As of now all my code is working fine . just wanted to clarify if i am in right path or not .
v
@samantha: Did you run into a similar error : AN_ERROR_OCCURRED_WHILE_DECRYPT_PASSWORDGUID
If yes, please keep in mind that GUIDs in Netsuite must be created using Form.addCredentialField
s
yes @Vaid , i got that error initially . later i resolved it 🙂👍
Actually i am using addSecretKeyField ,
Copy code
form.addSecretKeyField({id: 'guidkey', label: 'password', restrictToScriptIds :[runtime.getCurrentScript().id,'customscript_setup_token'] , restrictToCurrentUser: false}).maxLength = 200;
. So for all values which need encryption we can generate a single guid correct ? , with a restriction that guid should only be used by scripts mentioned in attribute restrictToScriptIds
i read in some conversations above , where they mentioned about using multiple GUID's , why do we even need multiple , we can simply use a single guid right ? please throw your 2 cents @Vaid
v
@samantha A guid can be used multiple times, thereby you can encrypt multiple strings using same key, but I remember reading somewhere in the docs regarding GUID usability dependent upon script re-execution. Personally I'm not a fan of NetSuite's limited documentation on their implementation of modules like CryptoJS . It came handy in some scenarios but for other situations like calling Restlets from server side scripts - I directly used external crypto-js rather than trying to figure out native Unexpected Errors 🙂
👍 1
s
@Vaid ya valid point . Makes sense