#import
@interface NSData (3DES)
/**
* 利用3DES加密数据
*/
- (NSData*)encryptedWith3DESUsingKey:(NSString*)key andIV:(NSData*)iv {
NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
size_t dataMoved;
NSMutableData *encryptedData = [NSMutableData dataWithLength:self.length + kCCBlockSize3DES];
CCCryptorStatus result = CCCrypt(kCCEncrypt,kCCAlgorithm3DES,kCCOptionPKCS7Padding,keyData.bytes,keyData.length,iv.bytes,self.bytes,self.length,encryptedData.mutableBytes,encryptedData.length,&dataMoved);
if(result == kCCSuccess) {
encryptedData.length = dataMoved;
returnencryptedData;
}
return nil;
}
/**
* @brief 利用3DES解密数据
*/
- (NSData*)decryptedWith3DESUsingKey:(NSString*)key andIV:(NSData*)iv {
NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
size_t dataMoved;
NSMutableData *decryptedData = [NSMutableData dataWithLength:self.length + kCCBlockSize3DES];
CCCryptorStatus result = CCCrypt(kCCDecrypt,kCCAlgorithm3DES,kCCOptionPKCS7Padding,keyData.bytes,keyData.length,iv.bytes,self.bytes,self.length,decryptedData.mutableBytes,decryptedData.length,&dataMoved);
if(result == kCCSuccess) {
decryptedData.length = dataMoved;
returndecryptedData;
}
return nil;
}