oc des解密

// des和3des不同
+ (NSString *)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,
                                     kCCOptionECBMode,
                                     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 )
    {
        NSString *decodedString = [[NSString alloc] initWithData:output encoding:NSUTF8StringEncoding];
        MTLog(@"decoded str %@",decodedString);
        return decodedString;
    } else {
        MTLog(@"Failed DES decrypt ...");
        return @"";
    }
}

发表评论

电子邮件地址不会被公开。