微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Nodejs 加密到 Swift commonCrypto

如何解决Nodejs 加密到 Swift commonCrypto

想要将以下 Node Crypto 示例转换为等效的 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',iv);
    let decrypted = decipher.update(encryptedText);
    let final = decipher.final();
    decrypted = Buffer.concat([decrypted,final]);


    console.log(decrypted.toString());     // <-- final decrypted string
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。