如何在 iOS 上使用 AES GCM 进行加密? [英] How to encrypt using AES GCM on iOS?

查看:176
本文介绍了如何在 iOS 上使用 AES GCM 进行加密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 GCM 模式下使用 AES 加密对一些数据进行加密/解密,但显然这无法通过 CommonCrypto API 完成.之前有人问过这个问题这里,但接受的答案不是我想要的,因为我需要使用这个特定的算法.

I need to encrypt/decrypt some data using AES encryption on GCM mode, but apparently this can't be done with the CommonCrypto API. This has been asked previously here, but the accepted answer is not what I'm looking for, since I need to use this specific algorithm.

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

Any ideas? Should I use OpenSSL? Because I've heard that there are some bugs when using it in iOS.

我正在 Swift 中寻找答案,但 Objective-C 也可以.

I'm looking for an answer in Swift, but Objective-C would be fine as well.

推荐答案

CommonCryptorSPI.h,它们还没有公开.但是,如果将它们添加到桥接头中,则可以使用它们.

There is some GCM crypt functions in the CommonCryptorSPI.h, they are not public yet. But you can use them if you add them to the bridging header.

#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);

或者您可以尝试 SwCrypt 库.

Or you can try the SwCrypt library.

这篇关于如何在 iOS 上使用 AES GCM 进行加密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆