2013-03-26 63 views
1

我想用DES算法加密目标c中的数据。这是我的代码。我从此代码获取我的数据:<>和加密的str:。任何人都可以说什么是错的吗?目标c DES加密

NSString *token = @"hello world"; 
    const void *vplainText; 
    size_t plainTextBufferSize; 

    plainTextBufferSize = [token length]; 
    vplainText = (const void *) [token UTF8String]; 

    uint8_t *bufferPtr = NULL; 
    size_t bufferPtrSize = 0; 
    size_t *movedBytes; 

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

    NSString *key = [[NSString alloc] initWithString:@"12345678"]; 
    const void *vkey = (const void *) [key UTF8String]; 
    CCCryptorStatus cryptStatus =CCCrypt(
          kCCEncrypt,   /* kCCEncrypt, etc. */ 
          kCCAlgorithmDES,  /* kCCAlgorithmAES128, etc. */ 
          kCCOptionPKCS7Padding | kCCOptionECBMode, /*kCCOptionPKCS7Padding, etc. */ 
          vkey, 
          kCCKeySizeDES, 
          NULL,   /* optional initialization vector */ 
          vplainText,  /* optional per op and alg */ 
          plainTextBufferSize, 
          (void *)bufferPtr,   /* data RETURNED here */ 
          bufferPtrSize, 
          movedBytes); 

    NSString *result; 

    NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes]; 
    NSLog(@"my data :%@",myData); 


    result = [self base64Encoding:myData]; 
    NSLog(@"enrypted str: %@",result); 
+0

你得到什么错误? – Vignesh 2013-03-26 14:58:25

+0

你看过你的“cryptStatus”了吗? – trumpetlicks 2013-03-26 15:00:41

+0

我如何看待我的“cryptStatus”? – user1765253 2013-03-26 15:08:30

回答

2

示例代码...

- (void) testDES 
{ 
    NSData * data = [OpenSSLProxy getRandomBytes:20]; // generate some random bytes 
    NSData * key = [OpenSSLProxy getRandomBytes:kCCKeySizeDES]; 
    NSData * enc = [self encryptDESByKey:key data:data]; 
    NSData *dec = [self decryptDESByKey:key data:enc]; 
    STAssertEqualObjects(data, dec, @""); 
} 

- (NSData *)encryptDESByKey:(NSData *)key data:(NSData *)data 
{ 
    size_t numBytesEncrypted = 0; 
    size_t bufferSize = data.length + kCCBlockSizeDES; 
    void *buffer = malloc(bufferSize); 

    CCCryptorStatus result = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionPKCS7Padding, 
            key.bytes, kCCKeySizeDES, 
            NULL, 
            data.bytes, data.length, 
            buffer, bufferSize, 
            &numBytesEncrypted); 
    NSData *output = [NSData dataWithBytes:buffer length:numBytesEncrypted]; 
    free(buffer); 
    if(result == kCCSuccess) 
    { 
     return output; 
    } else { 
     NSLog(@"Failed DES encrypt..."); 
     return nil; 
    } 
} 

- (NSData *) decryptDESByKey:(NSData *)key data:(NSData *)data 
{ 
    size_t numBytesEncrypted = 0; 

    size_t bufferSize = data.length + kCCBlockSizeDES; 
    void *buffer_decrypt = malloc(bufferSize); 
    CCCryptorStatus result = CCCrypt(kCCDecrypt , kCCAlgorithmDES, kCCOptionPKCS7Padding, 
            key.bytes, kCCKeySizeDES, 
            NULL, 
            data.bytes, data.length, 
            buffer_decrypt, bufferSize, 
            &numBytesEncrypted); 

    NSData *output = [NSData dataWithBytes:buffer_decrypt length:numBytesEncrypted]; 
    free(buffer_decrypt); 
    if(result == kCCSuccess) 
    { 
     return output; 
    } else { 
     NSLog(@"Failed DES decrypt ..."); 
     return nil; 
    } 
} 
+0

只是想注意到这个代码泄漏了缓冲区!你可以使用一个NSMutableData来代替void *缓冲区。 – casillic 2014-04-23 04:15:06

+0

是的,谢谢。我添加了一个免费的声明。很久以前为测试代码编写的。我想我使用void *可能是因为CCCrypt容易接受。 – karim 2014-04-23 07:38:30