首页 > 解决方案 > Nodejs Crypto 到 Swift commonCrypto

问题描述

想要将以下 NodeCrypto示例转换为等效的 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 实现代码贴在这里:Swift AES encryption using CommonCrypto

标签: node.jsswiftencryptioncommoncryptonode-crypto

解决方案


推荐阅读