iOS開發-加密與解密之CommonCrypto與Security.framework
本文參考蘋果官方的文件
《Cryptographic Services Guide》
《Certificate,Key,and Trust Services Programming Guide》
《Keychain Services Programming Guide》
對iOS平臺下使用CommonCrypto與Security.framework的加密與解密,簽名與簽名的基本技術進行了總結。
主要實現了以下功能
1.非對稱加密演算法
RSA
包含公私鑰的生成、公鑰加密、私鑰解密、私鑰簽名、公鑰驗籤功能。證書資訊的讀取。以及金鑰在KeyChain中儲存,查詢,刪除等功能。
2.對稱加密演算法
DES、3DES、AES
主要實現加密與解密功能。
3.雜湊演算法
SHA1、SHA224、SHA256、SHA384、SHA512
MD2、MD4、MD5
以下是詳細的說明。
首先,從非對稱加密演算法開始,在開發的過程中引用以下標頭檔案就足夠了
<Security/Security.h>
1.Security.framework中的基本資料型別
SecCertificateRef-X.509證書 .cer或者.der檔案
SecIdentityRef-同時包含私鑰與公鑰證書資訊 .p12檔案或者.pfx檔案
SecKeyRef-代表一個加密金鑰,私鑰或者公鑰
SecTrustRef-X.509證書策略
SecPolicyRef-X.509證書信任服務
SecAccessControlRef-某個物件的訪問控制許可權
2.Security.framework中方法執行後的狀態結果資訊
CF_ENUM(OSStatus)
{
errSecSuccess = 0,
/* No error. */
errSecUnimplemented = -4,
/* Function or operation not implemented. */
errSecIO = -36,
/* I/O error (bummers) */
errSecOpWr = -49,
/* file already open with with write permission */
errSecParam = -50,
/* One or more parameters passed to a function where not valid. */
errSecAllocate = -108,
/* Failed to allocate memory. */
errSecUserCanceled = -128,
/* User canceled the operation. */
errSecBadReq = -909,
/* Bad parameter or invalid state for operation. */
errSecInternalComponent = -2070,
errSecNotAvailable = -25291,
/* No keychain is available. You may need to restart your computer. */
errSecDuplicateItem = -25299,
/* The specified item already exists in the keychain. */
errSecItemNotFound = -25300,
/* The specified item could not be found in the keychain. */
errSecInteractionNotAllowed = -25308,
/* User interaction is not allowed. */
errSecDecode = -26275,
/* Unable to decode the provided data. */
errSecAuthFailed = -25293,
/* The user name or passphrase you entered is not correct. */
};
3.讀取證書檔案,從資料流到證書型別SecCertificateRef的轉換
<Security/SecCertificate.h>
SecCertificateCreateWithData
4.從證書中獲取公鑰SecKeyRef
<Security/SecPolicy.h>
SecPolicyCreateBasicX509 //建立SecPolicyRef
<Security/SecTrust.h>
SecTrustCreateWithCertificates //建立SecTrustRef
SecTrustCopyPublicKey //從SecTrustRef中拷貝公鑰
5.讀取私鑰檔案,從資料流到SecIdentityRef的轉換
SecPKCS12Import //私鑰資料流檔案匯入keychain
6.從私鑰檔案中獲取私鑰
SecIdentityCopyPrivateKey //從SecIdentityRef拷貝私鑰
7.加密與解密主要使用了以下四個函式
<Security/SecKey.h>
SecKeyEncrypt //加密
SecKeyDecrypt //解密
SecKeyRawSign //簽名
SecKeyRawVerify //驗籤
8.生成公私鑰對,儲存或者不儲存到鑰匙串
<Security/SecKey.h>
SecKeyGeneratePair
其次,為對稱加密演算法。
在開發過程中需要引用以下標頭檔案
<CommonCrypto/CommonCrypto.h>
主要有如下兩個思路
順序呼叫以下函式
CCCryptorCreateWithMode //建立CCCryptorRef物件
CCCryptorUpdate
CCCryptorFinal //並已定需要呼叫該方法
另外兩個函式的說明
CCCryptorGetOutputLength //獲取密文輸出緩衝區長度
CCCryptorRelease //釋放CCCryptorRef物件
或者直接呼叫以下函式完成加密過程
CCCrypt
最後,為雜湊演算法。
雜湊演算法的計算流程大致相同。方法宣告在這裡
<CommonCrypto/CommonDigest.h>
以SHA1為例,有兩個思路
順序呼叫以下函式
CC_SHA1_Init
CC_SHA1_Update
CC_SHA1_Final
或者直接呼叫以下函式
CC_SHA1
HMAC是金鑰相關的雜湊運算訊息認證碼(Hash-based Message Authentication Code),HMAC運算利用雜湊演算法,以一個金鑰和一個訊息為輸入,生成一個訊息摘要作為輸出。
其支援SHA1和MD5兩種雜湊演算法
方法宣告在這裡
<CommonCrypto/CommonHMAC.h>
順序呼叫以下函式
CCHmacInit
CCHmacUpdate
CCHmacFinal
或者直接呼叫以下函式
CCHmac
相關文章
- iOS開發之AES+Base64資料混合加密與解密iOS加密解密
- Web開發之編碼與解碼、簽名、加密與解密Web加密解密
- RSA加密與解密加密解密
- iOS開發中AES的加密和解密iOS加密解密
- 影像的加密與解密加密解密
- utf8 加密與解密加密解密
- Java中的加密與解密Java加密解密
- iOS中加密、解密iOS加密解密
- 序列密碼的加密與解密密碼加密解密
- python加密與解密,加簽與驗籤Python加密解密
- iOS開發之登入與訪客iOS
- 有關字元的加密與解密 (轉)字元加密解密
- iOS開發之記憶體與快取iOS記憶體快取
- iOS加密解密演算法iOS加密解密演算法
- 使用Java加密與解密實現步驟Java加密解密
- java實現DES資料加密與解密Java加密解密
- iOS 開發加密做法iOS加密
- C/C++ 常用加密與解密演算法C++加密解密演算法
- PhpAES加密、解密與Java互操作的問題PHP加密解密Java
- 軟體狗[Dongles]的加密與解密技術加密解密
- 淺談如何學習加密與解密技術加密解密
- security.js RSA加密與java客戶端解密JS加密Java客戶端解密
- 閱讀《加密與解密》之前,需要什麼基礎?加密解密
- 《Java加密與解密的藝術》讀後總結Java加密解密
- C#開發中常用加密解密方法解析C#加密解密
- C#開發中常用的加密解密方法C#加密解密
- iOS客戶端開發與Web前端開發iOS客戶端Web前端
- iOS開發– ARC與引用計數iOS
- iOS 極光推送整合與開發iOS
- 前端加密解密之Crypto.js前端加密解密JS
- Linux下實現 OpenSSL 簡單加密與解密字串Linux加密解密字串
- Linux基於tar與openssl加密解密壓縮包Linux加密解密
- Flutter與已有iOS工程混合開發與指令碼配置FlutteriOS指令碼
- 加密解密加密解密
- iOS與Flutter混合開發的姿勢iOSFlutter
- iOS 靜態庫詳解與開發iOS
- 原生(iOS)與Flutter混合開發步驟iOSFlutter
- Android與iOS開發統一坑AndroidiOS