使用Swift 5的AES256问题

时间:2019-07-04 07:07:58

标签: node.js swift encryption aes node-crypto

我正在使用AES256填充的CBC mode算法pkc7。我在Node.js中有后端。但是获得前12个随机字符。

这是我的快速代码:

    func encrypt(data: Data, key: Data, iv: Data) throws -> Data? {

        // Output buffer (with padding)
        let outputLength = data.count + kCCBlockSizeAES128

        var outputBuffer = Array<UInt8>(repeating: 0,
                                        count: outputLength)
        //var outputBuffer: [UInt8] = []
        var numBytesEncrypted = 0
        let status = CCCrypt(CCOperation(kCCEncrypt),
                             CCAlgorithm(kCCAlgorithmAES),
                             CCOptions(kCCOptionPKCS7Padding),
                             Array(key),
                             kCCKeySizeAES256,
                             Array(iv),
                             Array(data),
                             data.count,
                             &outputBuffer,
                             outputLength,
                             &numBytesEncrypted)

        guard status == kCCSuccess else { return nil }

        let outputBytes = iv + outputBuffer.prefix(numBytesEncrypted)

        return Data(bytes: outputBytes)
    }

如何不填充?还是应该从后端做什么?

enter image description here

2 个答案:

答案 0 :(得分:1)

您发布的内容没有错,您可能只是在Swift和node.js之间的某个地方输入了一些错误的参数。

首先,最好检查一下是否可以使用相同的语言在本地解密,无论加密了什么。使用您发布的屏幕快照中的信息(将来的注意事项:还要以文本形式发布所有内容,输入屏幕快照中以base 64编码的数据,比复制和粘贴要麻烦得多)。

在您的情况下,在Swift中,它看起来像这样:

import UIKit
import CommonCrypto

var key = "zewQjVQMGdoEJK0yHtLcbP3ZlHOKjERG"

// This is the ciphertext with the initialization vector prepended.
let base64String = "w93bonVuqtW22Drj4HtZ3zNtNSt+5OBMapGGHekLCFA="
var data = Data(base64Encoded: base64String)!
// Split out the initialization vector and ciphertext
var iv = data[0..<kCCBlockSizeAES128]
var ciphertext = data[kCCBlockSizeAES128..<data.count]


var outputLength = data.count
var outputBuffer = Array<UInt8>(repeating:0, count: outputLength)
var bytesDecrypted = 0

let status = CCCrypt(CCOperation(kCCDecrypt),
    CCAlgorithm(kCCAlgorithmAES),
    CCOptions(kCCOptionPKCS7Padding),
    Array(key.utf8),
    kCCKeySizeAES256,
    Array(iv),
    Array(ciphertext),
    ciphertext.count,
    &outputBuffer,
    outputLength,
    &bytesDecrypted
    )

print(String(bytes: outputBuffer.prefix(bytesDecrypted), encoding: .utf8))
// Optional("gmail.com")

一旦您知道所有语言都运行良好,请尝试另一种语言。现在我对node.js的了解不多,但是逐行浏览Swift会导致以下情况:

const crypto = require('crypto')

let keyString = 'zewQjVQMGdoEJK0yHtLcbP3ZlHOKjERG'
let key = Buffer.from(keyString, 'utf8')

let base64String = 'w93bonVuqtW22Drj4HtZ3zNtNSt+5OBMapGGHekLCFA='
let ivPlusCiphertextBuffer = Buffer.from(base64String, 'base64')

// Split out the initialization vector and the ciphertext
let blockSize = 16 // Don't know how to get this in Node.js so hard-code it
let iv = ivPlusCiphertextBuffer.subarray(0, blockSize)
let ciphertext = ivPlusCiphertextBuffer.subarray(blockSize, ivPlusCiphertextBuffer.length)

let decryptor = crypto.createDecipheriv('aes-256-cbc', key, iv)
var plaintext = decryptor.update(ciphertext, 'binary', 'utf8')
plaintext += decryptor.final('utf8')

console.log(plaintext)
// gmail.com

只要您将Swift例程的输出输入到看起来像上面我的示例的node.js例程的输入,您就应该很好!

答案 1 :(得分:1)

您可以在要共享的有效载荷之前和之后使用标签。该标签将成为您的标题。

喜欢 <tag>string</tag>

因此,如果加密,您将获得前12个随机字节

因此您需要忽略**<tag>...</tag>**之间的文本