banner
李大仁博客

李大仁博客

天地虽大,但有一念向善,心存良知,虽凡夫俗子,皆可为圣贤。

[iOS]objective-c AES/DES/3DES等加密算法实现

objective-c 对 AES/DES/3DES 等加密提供了统一的加密方法 CCCrypt 进行实现,目前主要支持的算法有

@constant kCCAlgorithmAES128 Advanced Encryption Standard @constant kCCAlgorithmAES Advanced Encryption Standard, 128-bit block @constant kCCAlgorithmDES Data Encryption Standard @constant kCCAlgorithm3DES Triple-DES, three key, EDE configuration @constant kCCAlgorithmCAST CAST @constant kCCAlgorithmRC4 RC4 stream cipher @constant kCCAlgorithmBlowfish Blowfish block cipher

/*!
@enum CCAlgorithm
@abstract Encryption algorithms implemented by this module.

@constant   kCCAlgorithmAES128  Advanced Encryption Standard, 128-bit block
                                This is kept for historical reasons.  It's
                                preferred now to use kCCAlgorithmAES since
                                128-bit blocks are part of the standard.
@constant   kCCAlgorithmAES     Advanced Encryption Standard, 128-bit block
@constant   kCCAlgorithmDES     Data Encryption Standard
@constant   kCCAlgorithm3DES    Triple-DES, three key, EDE configuration
@constant   kCCAlgorithmCAST    CAST

@constant kCCAlgorithmRC4 RC4 stream cipher
@constant kCCAlgorithmBlowfish Blowfish block cipher
*/
enum {
kCCAlgorithmAES128 = 0,
kCCAlgorithmAES = 0,
kCCAlgorithmDES,
kCCAlgorithm3DES,
kCCAlgorithmCAST,
kCCAlgorithmRC4,
kCCAlgorithmRC2,
kCCAlgorithmBlowfish
};

以下是 DES 的代码实现,可供使用的加密算法有 des,3des,aes 等,根据需要替换相应的参数即可。

- (NSString*) des:(NSString*) key
{
const char *cstr = [self cStringUsingEncoding];
NSData *data = [NSData dataWithBytes length.length];

CCCryptorStatus ccStatus;
uint8\_t \*dataOut = NULL;
size\_t dataOutAvailable = 0; //size\_t  是操作符sizeof返回的结果类型
size\_t dataOutMoved = 0;

dataOutAvailable = (data.length + kCCBlockSizeDES) & ~(kCCBlockSizeDES - 1);
dataOut = malloc( dataOutAvailable \* sizeof(uint8\_t));
memset((void \*)dataOut, 0x0, dataOutAvailable);//将已开辟内存空间buffer的首 1 个字节的值设为值 0

NSString \*initIv = key;
const void \*vkey = (const void \*) \[key UTF8String\];
const void \*iv = (const void \*) \[initIv UTF8String\];

//CCCrypt函数 加密/解密
ccStatus = CCCrypt(kCCEncrypt,               //  加密/解密
                   kCCAlgorithmDES,          //  加密根据哪个标准(des,3des,aes。。。。)
                   kCCOptionPKCS7Padding,    //  选项分组密码算法(des:对每块分组加一次密  3DES:对每块分组加三个不同的密)
                   vkey,                     //  密钥
                   kCCKeySizeDES,            //  DES 密钥的大小(kCCKeySizeDES=8)
                   iv,                       //  可选的初始矢量
                   \[data bytes\],             //  数据的存储单元
                   data.length,              // 数据的大小
                   (void \*)dataOut,          // 用于返回数据
                   dataOutAvailable,
                   &dataOutMoved);

NSString \*result = \[\[\[NSString alloc\] initWithData:\[NSData dataWithBytes:(const void \*)dataOut length:(NSUInteger)dataOutMoved\] encoding:NSUTF8StringEncoding\] autorelease\];

return result;

}

配合 Base64 方式使用

- (NSString*) des_base64:(NSString*) key
{
const char *cstr = [self cStringUsingEncoding];
NSData *data = [NSData dataWithBytes length.length];

CCCryptorStatus ccStatus;
uint8\_t \*dataOut = NULL;
size\_t dataOutAvailable = 0; //size\_t  是操作符sizeof返回的结果类型
size\_t dataOutMoved = 0;

dataOutAvailable = (data.length + kCCBlockSizeDES) & ~(kCCBlockSizeDES - 1);
dataOut = malloc( dataOutAvailable \* sizeof(uint8\_t));
memset((void \*)dataOut, 0x0, dataOutAvailable);//将已开辟内存空间buffer的首 1 个字节的值设为值 0

NSString \*initIv = key;
const void \*vkey = (const void \*) \[key UTF8String\];
const void \*iv = (const void \*) \[initIv UTF8String\];

//CCCrypt函数 加密/解密
ccStatus = CCCrypt(kCCEncrypt,               //  加密/解密
                   kCCAlgorithmDES,          //  加密根据哪个标准(des,3des,aes。。。。)
                   kCCOptionPKCS7Padding,    //  选项分组密码算法(des:对每块分组加一次密  3DES:对每块分组加三个不同的密)
                   vkey,                     //  密钥
                   kCCKeySizeDES,            //  DES 密钥的大小(kCCKeySizeDES=8)
                   iv,                       //  可选的初始矢量
                   \[data bytes\],             //  数据的存储单元
                   data.length,              // 数据的大小
                   (void \*)dataOut,          // 用于返回数据
                   dataOutAvailable,
                   &dataOutMoved);

NSData \*dataFor = \[NSData dataWithBytes:(const void \*)dataOut length:(NSUInteger)dataOutMoved\];
NSString \*result = \[GTMBase64 stringByEncodingData:dataFor\];

return result;

}

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.