如何在iOS上使用AES GCM进行加密?

Gab*_*uff 1 cryptography ios commoncrypto swift

我需要在GCM模式下使用AES加密来加密/解密某些数据,但显然使用CommonCrypto API无法做到这一点.这里之前已经问过这个问题,但是接受的答案并不是我想要的,因为我需要使用这个特定的算法.

有任何想法吗?我应该使用OpenSSL吗?因为我听说在iOS中使用它时会有一些错误.

我正在寻找Swift的答案,但Objective-C也没问题.

bla*_*acx 9

从 iOS 13 开始,我们有了 CryptoKit,它非常强大,而且当您掌握要点时相对简单。今天我创建了一个演示 AES-GCM 256 加密和解密的游乐场。我不是密码学专家,但游乐场演示了使用该算法的两种可能的方法。请随意克隆我的游乐场存储库并使用 CryptoKit 游乐场:

\n
import Foundation\nimport CryptoKit\n\nlet key = SymmetricKey(size: .bits256)\nlet plain = """\n    {"data":{"id":"7fab123e96","created_at":"2020-01-21T14:16:41Z","name":"John","age":18,"sex":"male"}}\n    """\n\n/// Encrypt: Using plain text only \xe2\x80\xa2 nonce & tag are randomly created\n/// Decrypt: Specify all 3 parameters: nonce + cipher text + tag\nfunc cryptoDemoCipherText() {\n\n    // Encrypt\n    let sealedBox = try! AES.GCM.seal(plain.data(using: .utf8)!, using: key)\n\n    // Decrypt\n    let sealedBoxRestored = try! AES.GCM.SealedBox(nonce: sealedBox.nonce, ciphertext: sealedBox.ciphertext, tag: sealedBox.tag)\n    let decrypted = try! AES.GCM.open(sealedBoxRestored, using: key)\n\n    print("Crypto Demo I\\n\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\\n")\n    print("Combined:\\n\\(sealedBox.combined!.base64EncodedString())\\n")\n    print("Cipher:\\n\\(sealedBox.ciphertext.base64EncodedString())\\n")\n    print("Nonce:\\n\\(sealedBox.nonce.withUnsafeBytes { Data(Array($0)).base64EncodedString() })\\n")\n    print("Tag:\\n\\(sealedBox.tag.base64EncodedString())\\n")\n    print("Decrypted:\\n\\(String(data: decrypted, encoding: .utf8)!)\\n")\n}\n\n/// Encrypt: Specify all 3 parameters yourself: nonce + cipher text + tag\n/// Decrypt: Using combined data (nonce + cipher text + tag) and tag to open\nfunc cryptoDemoCombinedData() {\n\n    let nonce = try! AES.GCM.Nonce(data: Data(base64Encoded: "fv1nixTVoYpSvpdA")!)\n    let tag = Data(base64Encoded: "e1eIgoB4+lA/j3KDHhY4BQ==")!\n\n    // Encrypt\n    let sealedBox = try! AES.GCM.seal(plain.data(using: .utf8)!, using: key, nonce: nonce, authenticating: tag)\n\n    // Decrypt\n    let sealedBoxRestored = try! AES.GCM.SealedBox(combined: sealedBox.combined!)\n    let decrypted = try! AES.GCM.open(sealedBoxRestored, using: key, authenticating: tag)\n\n    print("Crypto Demo II\\n\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\\n")\n    print("Combined:\\n\\(sealedBox.combined!.base64EncodedString())\\n")\n    print("Cipher:\\n\\(sealedBox.ciphertext.base64EncodedString())\\n")\n    print("Nonce:\\n\\(nonce.withUnsafeBytes { Data(Array($0)).base64EncodedString() })\\n")\n    print("Tag:\\n\\(tag.base64EncodedString())\\n")\n    print("Decrypted:\\n\\(String(data: decrypted, encoding: .utf8)!)\\n")\n}\n\n\nprint("Key32:\\n\\(key.withUnsafeBytes { Data(Array($0)).base64EncodedString() })\\n")\n\ncryptoDemoCombinedData()\ncryptoDemoCipherText()\n
Run Code Online (Sandbox Code Playgroud)\n

https://github.com/Blackjacx/Playgrounds/blob/master/playgrounds/CryptoKit.playground/Contents.swift

\n

希望它对你们有些人有帮助:-)

\n


soy*_*yer 6

CommonCryptorSPI.h中有一些GCM crypt函数,它们尚未公开.但是如果将它们添加到桥接头中,则可以使用它们.

#include <CommonCrypto/CommonCryptor.h>
CCCryptorStatus CCCryptorGCM(
CCOperation     op,             /* kCCEncrypt, kCCDecrypt */
CCAlgorithm     alg,
const void      *key,           /* raw key material */
size_t          keyLength,  
const void      *iv,
size_t          ivLen,
const void      *aData,
size_t          aDataLen,
const void      *dataIn,
size_t          dataInLength,
void            *dataOut,
const void      *tag,
size_t          *tagLength);
Run Code Online (Sandbox Code Playgroud)

或者您可以尝试使用SwCrypt库.

  • 这会通过应用审查 WRT 来使用私有 API 吗? (2认同)
  • 全部:请向Apple提交错误报告:https://bugreport.apple.com,要求对iOS提供GCM支持.现在是Apple更新Common Crypto的时候了,即使这意味着另一个FIPS 140-2评论. (2认同)