- (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 )
{
NSLog(@"encoded str %@",[output base64EncodedStringWithOptions:0]);
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 )
{
NSString *decodedString = [[NSString alloc] initWithData:output encoding:NSUTF8StringEncoding];
NSLog(@"decoded str %@",decodedString);
return output;
} else {
NSLog(@"Failed DES decrypt ...");
return nil;
}
}
- Затем вы можете вызвать метод, как
[self decryptDESByKey:[@"qwertykey" dataUsingEncoding:NSUTF8StringEncoding] data:[self encryptDESByKey:[@"qwertykey" dataUsingEncoding:NSUTF8StringEncoding] data:["YOUR--INPUT__STRING" dataUsingEncoding:NSUTF8StringEncoding]]];
- "qwertykey" - это секретный ключ, поставляемый