ios des加密

查了两天资料,把des加密弄好了,总结一下。感觉网上的资料代码很多,思路很少,看得很郁闷。
des加密主要用CCCrypt函数(只有ios5中有的),这个函数的头文件:
#import CommonCrypto/CommonCryptor.h
主要思路是:
加密:
CCCrypt函数加密一下,然后用base64编码下,传过去。
解密:
先把收到的数据根据base64,decode一下,然后再解密一下,就得到了。

关于base64编码,网上有很多写好的,直接拿来调用下就好了。

 

CCCryptorStatus CCCrypt(

    CCOperation op,        

    CCAlgorithm alg,       

    CCOptions options,     

    const void *key,

    size_t keyLength,

    const void *iv,        

    const void *dataIn,    

    size_t dataInLength,

    void *dataOut,         

    size_t dataOutAvailable,

    size_t *dataOutMoved)

    __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_4_0);

网上找来的一个加密解密的文件(稍微改了一下):

.h文件:

 

+ (NSString *)encryptWithText:(NSString *)sText theKey:(NSString *)aKey;

+ (NSString *)decryptWithText:(NSString *)sText theKey:(NSString *)aKey;

.m文件:

 

 

+ (NSString *)encrypt:(NSString *)sText encryptOrDecrypt:(CCOperation)encryptOperation key:(NSString *)key

{

    const void *vplainText;

    size_t plainTextBufferSize;

    

    if (encryptOperation == kCCDecrypt)

    {

        NSData *decryptData = [GTMBase64 decodeData:[sTextdataUsingEncoding:NSUTF8StringEncoding]];

        //NSData *decryptData = [GTMBase64 decodeString:sText ];

        plainTextBufferSize = [decryptData length];

        vplainText = [decryptData bytes];

    }

    else

    {

        NSData* encryptData = [sText dataUsingEncoding:NSUTF8StringEncoding];

        plainTextBufferSize = [encryptData length];

        vplainText = (const void *)[encryptData bytes];

    }

    

    uint8_t *bufferPtr = NULL;

    size_t bufferPtrSize = 0;

    size_t movedBytes = 0;

    

    bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);

    bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));

    memset((void *)bufferPtr, 0x0, bufferPtrSize);

    

    NSString *initVec = @"12345678";

    const void *vkey = (const void *) [key UTF8String];

    const void *vinitVec = (const void *) [initVec UTF8String];

    

    CCCryptorStatus ccStatus = CCCrypt(encryptOperation,

                       kCCAlgorithm3DES,

                       kCCOptionPKCS7Padding,

                       vkey,

                       kCCKeySize3DES,

                       vinitVec, 

                       vplainText,

                       plainTextBufferSize,

                       (void *)bufferPtr,

                       bufferPtrSize,

                       &movedBytes);

    NSLog(@"%d",ccStatus);

    NSString *result = nil;

    if (ccStatus == kCCSuccess)

    {

        if (encryptOperation == kCCDecrypt)

        {

            result = [[NSString alloc] initWithData:[NSData dataWithBytes:(const void *)bufferPtrlength:(NSUInteger)movedBytes] encoding:NSUTF8StringEncoding] ;

        }

        else

        {

            NSData *data = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];

            result = [GTMBase64 stringByEncodingData:data];

           // NSLog(@"result:%@",result);

           // NSData *temp = [GTMBase64 decodeString:result];

        }

    }

    return result;

}


+ (NSString *)encryptWithText:(NSString *)sText theKey:(NSString *)aKey

{

    return [self encrypt:sText encryptOrDecrypt:kCCEncrypt key:aKey];

}


+ (NSString *)decryptWithText:(NSString *)sText theKey:(NSString *)aKey

{

    return [self encrypt:sText encryptOrDecrypt:kCCDecrypt key:aKey];

}

你可能感兴趣的:(ios des加密)