如何在iPhone中使用DESencryptionNSString值?

我用下面的代码用Key来encryptionNSString的值,

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); 

但是我得到(空)是结果。 有什么问题吗? 如何encryptionNSString值?

这里是一个AES的方式,可能为DES会是类似的,看看这个作品与我…在NSData上创build一个新的类别AES
对于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 

最后我做了下面的代码,现在工作正常。

 - (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; }