【问题标题】:AES256 Issue using swift 5使用 swift 5 的 AES256 问题
【发布时间】:2019-07-04 07:07:58
【问题描述】:

我正在使用AES256 算法CBC modepkc7 填充。我在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)
    }

没有填充怎么办?或者应该从后端做什么?

【问题讨论】:

  • 你是在快速加密还是解密? nodejs是怎么做加密的?
  • @JamesKPolk 我正在从 swift 加密并从 node.js 解密
  • 这可能是你的编码。确保在 iOS 上使用相同的编码将文本转换为字节,就像将字节转换为文本一样。您与数据有一些相似之处的事实表明加密完成了它应该做的事情。如果加密失败,它就会爆炸。

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


【解决方案1】:

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

点赞&lt;tag&gt;string&lt;/tag&gt;

所以如果你加密你会得到前 12 个随机字节

所以你需要忽略**&lt;tag&gt;...&lt;/tag&gt;**之间以外的文本

【讨论】:

  • 我们是否需要从后端忽略?
【解决方案2】:

您发布的内容没有任何问题,您可能只是在 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 例程的输入,您就可以开始了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    相关资源
    最近更新 更多