7

我使用以下代码用 Key 加密 NSString 值,

NSString *token = @"encryptValue";
NSString *key = @"encryptKey";

NSData * keyData = [key dataUsingEncoding: NSUTF8StringEncoding];
NSData * plainData = [token dataUsingEncoding: NSUTF8StringEncoding];
NSMutableData * encData = [NSMutableData dataWithLength: plainData.length + 256];
size_t bytesWritten = 0;
CCCryptorStatus ccStatus = CCCrypt (kCCEncrypt,
                    kCCAlgorithmDES,
                    kCCOptionPKCS7Padding,
                    [keyData bytes], 
                    kCCKeySizeDES,
                    NULL,
                    [plainData bytes],
                    [plainData length],
                    encData.mutableBytes,
                    encData.length,
                    &bytesWritten);
encData.length = bytesWritten;    
NSString *encrypted = [[NSString alloc]  initWithBytes:[encData bytes] length:[encData length] encoding: NSUTF8StringEncoding];  
NSLog(@"ENCRYPT : %@",encrypted); 

但我得到 (null) 是结果。有什么问题吗?如何加密 NSString 值?

4

2 回答 2

10

这是 AES 的一种方式,可能对于 DES 来说是类似的,看看这对我有用...AESNSData
For NSData+AES.h上创建一个新类别

#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonCryptor.h>

@interface NSData (AES)

- (NSData*) EncryptAES: (NSString *) key;
- (NSData *) DecryptAES: (NSString *) key;

@end

对于NSData+AES.m

    #import "NSData+AES.h"
    @implementation NSData(AES)

    - (NSData*) EncryptAES: (NSString *) key
    {
        char keyPtr[kCCKeySizeAES256+1];
        bzero( keyPtr, sizeof(keyPtr) );

        [key getCString: keyPtr maxLength: sizeof(keyPtr) encoding: NSUTF16StringEncoding];
        size_t numBytesEncrypted = 0;

        NSUInteger dataLength = [self length];

        size_t bufferSize = dataLength + kCCBlockSizeAES128;
        void *buffer = malloc(bufferSize);

        CCCryptorStatus result = CCCrypt( kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                         keyPtr, kCCKeySizeAES256,
                                         NULL,
                                         [self bytes], [self length],
                                         buffer, bufferSize,
                                         &numBytesEncrypted );

        if( result == kCCSuccess )
            return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];

        return nil;
    }

    - (NSData *) DecryptAES: (NSString *) key
    {
        char  keyPtr[kCCKeySizeAES256+1];
        bzero( keyPtr, sizeof(keyPtr) );

        [key getCString: keyPtr maxLength: sizeof(keyPtr) encoding: NSUTF16StringEncoding];

        size_t numBytesEncrypted = 0;

        NSUInteger dataLength = [self length];

        size_t bufferSize = dataLength + kCCBlockSizeAES128;
        void *buffer_decrypt = malloc(bufferSize);    

        CCCryptorStatus result = CCCrypt( kCCDecrypt , kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                         keyPtr, kCCKeySizeAES256,
                                         NULL,
                                         [self bytes], [self length],
                                         buffer_decrypt, bufferSize,
                                         &numBytesEncrypted );

        if( result == kCCSuccess )
        return [NSData dataWithBytesNoCopy:buffer_decrypt length:numBytesEncrypted];

        return nil;
    }

    @end

用法:导入NSData+AES.h

//Encryption
NSString *myString = @"This is the content";
NSString *key = @"my secret key";
NSData *data = [myString dataUsingEncoding:NSUTF8StringEncoding];
NSData *encryptedData = [data EncryptAES:key];

//Decryption
NSString *decryptedString = [[[NSString alloc] initWithData:[encryptedData DecryptAES:key] encoding:NSUTF8StringEncoding] autorelease]; // remove autorelease if using ARC
于 2012-05-17T06:55:15.713 回答
4

最后我完成了以下代码,现在它工作正常。

- (NSString*) doCipher:(NSString*)encryptValue {

    const void *vplainText;
    size_t plainTextBufferSize = [encryptValue length];
    vplainText = (const void *) [encryptValue UTF8String];    
    CCCryptorStatus ccStatus;
    uint8_t *bufferPtr = NULL;
    size_t bufferPtrSize = 0;
    size_t movedBytes = 0;

    bufferPtrSize = (plainTextBufferSize + kCCBlockSizeDES) & ~(kCCBlockSizeDES - 1);
    bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
    memset((void *)bufferPtr, 0x0, bufferPtrSize);

    Byte iv [] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef};

    NSString *key = @"encryptKey";
    const void *vkey = (const void *) [key UTF8String];

    ccStatus = CCCrypt(kCCEncrypt,
                       kCCAlgorithmDES,
                       kCCOptionPKCS7Padding,
                       vkey, 
                       kCCKeySizeDES,
                       iv,
                       vplainText,
                       plainTextBufferSize,
                       (void *)bufferPtr,
                       bufferPtrSize,
                       &movedBytes);

    NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
    NSString *result = [myData base64Encoding]; 

   return result;
}
于 2012-05-17T08:18:06.687 回答