当前位置:   article > 正文

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

commoncrypto 解密

《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
  • 2

1.Security.framework中的基本数据类型

SecCertificateRef-X.509证书 .cer或者.der文件 
SecIdentityRef-同时包含私钥与公钥证书信息 .p12文件或者.pfx文件 
SecKeyRef-代表一个加密密钥,私钥或者公钥 
SecTrustRef-X.509证书策略 
SecPolicyRef-X.509证书信任服务 
SecAccessControlRef-某个对象的访问控制权限

2.Security.framework中方法执行后的状态结果信息

  1. CF_ENUM(OSStatus)
  2. {
  3. errSecSuccess = 0,
  4. /* No error. */
  5. errSecUnimplemented = -4,
  6. /* Function or operation not implemented. */
  7. errSecIO = -36,
  8. /* I/O error (bummers) */
  9. errSecOpWr = -49,
  10. /* file already open with with write permission */
  11. errSecParam = -50,
  12. /* One or more parameters passed to a function where not valid. */
  13. errSecAllocate = -108,
  14. /* Failed to allocate memory. */
  15. errSecUserCanceled = -128,
  16. /* User canceled the operation. */
  17. errSecBadReq = -909,
  18. /* Bad parameter or invalid state for operation. */
  19. errSecInternalComponent = -2070,
  20. errSecNotAvailable = -25291,
  21. /* No keychain is available. You may need to restart your computer. */
  22. errSecDuplicateItem = -25299,
  23. /* The specified item already exists in the keychain. */
  24. errSecItemNotFound = -25300,
  25. /* The specified item could not be found in the keychain. */
  26. errSecInteractionNotAllowed = -25308,
  27. /* User interaction is not allowed. */
  28. errSecDecode = -26275,
  29. /* Unable to decode the provided data. */
  30. errSecAuthFailed = -25293,
  31. /* The user name or passphrase you entered is not correct. */
  32. };
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

3.读取证书文件,从数据流到证书类型SecCertificateRef的转换 

  1. <Security/SecCertificate.h>
  2. SecCertificateCreateWithData
  • 1
  • 2
  • 3
  • 4

4.从证书中获取公钥SecKeyRef

  1. <Security/SecPolicy.h>
  2. SecPolicyCreateBasicX509 //创建SecPolicyRef
  3. <Security/SecTrust.h>
  4. SecTrustCreateWithCertificates //创建SecTrustRef
  5. SecTrustCopyPublicKey //从SecTrustRef中拷贝公钥
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

5.读取私钥文件,从数据流到SecIdentityRef的转换

SecPKCS12Import    //私钥数据流文件导入keychain
  • 1
  • 2

6.从私钥文件中获取私钥

SecIdentityCopyPrivateKey //从SecIdentityRef拷贝私钥
  • 1
  • 2

7.加密与解密主要使用了以下四个函数

  1. <Security/SecKey.h>
  2. SecKeyEncrypt //加密
  3. SecKeyDecrypt //解密
  4. SecKeyRawSign //签名
  5. SecKeyRawVerify //验签
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

8.生成公私钥对,存储或者不存储到钥匙串

  1. <Security/SecKey.h>
  2. SecKeyGeneratePair
  • 1
  • 2
  • 3
  • 4
其次,为对称加密算法。

在开发过程中需要引用以下头文件

<CommonCrypto/CommonCrypto.h>
  • 1
  • 2

主要有如下两个思路 

顺序调用以下函数

  1. CCCryptorCreateWithMode //创建CCCryptorRef对象
  2. CCCryptorUpdate
  3. CCCryptorFinal //并已定需要调用该方法
  • 1
  • 2
  • 3
  • 4

另外两个函数的说明

  1. CCCryptorGetOutputLength //获取密文输出缓冲区长度
  2. CCCryptorRelease //释放CCCryptorRef对象
  • 1
  • 2
  • 3

或者直接调用以下函数完成加密过程

CCCrypt 
  • 1
  • 2
最后,为哈希算法。

哈希算法的计算流程大致相同。方法声明在这里

<CommonCrypto/CommonDigest.h>
  • 1
  • 2

以SHA1为例,有两个思路

顺序调用以下函数

  1. CC_SHA1_Init
  2. CC_SHA1_Update
  3. CC_SHA1_Final
  • 1
  • 2
  • 3
  • 4

或者直接调用以下函数

CC_SHA1
  • 1
  • 2

HMAC是密钥相关的哈希运算消息认证码(Hash-based Message Authentication Code),HMAC运算利用哈希算法,以一个密钥和一个消息为输入,生成一个消息摘要作为输出。

其支持SHA1和MD5两种哈希算法

方法声明在这里

<CommonCrypto/CommonHMAC.h>
  • 1
  • 2

顺序调用以下函数

  1. CCHmacInit
  2. CCHmacUpdate
  3. CCHmacFinal
  • 1
  • 2
  • 3
  • 4

或者直接调用以下函数

CCHmac
  • 1
  • 2
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/332183
推荐阅读
相关标签
  

闽ICP备14008679号