Home > 零敲碎打 > [iOS]objective-c AES/DES/3DES等加密算法实现

[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:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:self.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:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:self.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;
 
}
Categories: 零敲碎打 Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.