您的位置:首页 > 移动开发 > IOS开发

iOS开发-加密与解密之CommonCrypto与Security.framework

2016-02-23 18:36 633 查看
本文参考苹果官方的文档

《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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: