Reputation: 1768
Wanted to convert the following Node Crypto
example to equivalent Swift commonCrypto
:
function encrypt(key, text){
let iv = crypto.randomBytes(IV_LENGTH);
let cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
let final_encrypted = iv.toString('hex') + ':' + encrypted.toString('hex');
console.log(final_encrypted); // <-- final encrypted string
}
function decrypt(key, enc_text){
let textParts = enc_text.split(':');
let iv = Buffer.from(textParts.shift(), 'hex');
let encryptedText = Buffer.from(textParts.join(':'), 'hex');
let decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(key), iv);
let decrypted = decipher.update(encryptedText);
let final = decipher.final();
decrypted = Buffer.concat([decrypted, final]);
console.log(decrypted.toString()); // <-- final decrypted string
}
Swift implementation code is posted here: Swift AES encryption using CommonCrypto
Upvotes: 1
Views: 360