/// 工程中只需把下面的2个方法拖进去,然后就可以调用,且需要导入头文件#import"GTMBase64.h"
#include<CommonCrypto/CommonCryptor.h>
///加密算法
staticByteiv[] = {0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF};
-(NSString*) encryptUseDESS:(NSString*)plainText key:(NSString*)key
{
NSString*ciphertext =nil;
constchar*textBytes = [plainTextUTF8String];
NSUIntegerdataLength =strlen(textBytes);
unsignedcharbuffer[1024];
memset(buffer,0,sizeof(char));
size_tnumBytesEncrypted =0;
CCCryptorStatuscryptStatus =CCCrypt(kCCEncrypt,kCCAlgorithmDES,
kCCOptionPKCS7Padding,
[keyUTF8String],kCCKeySizeDES,
iv,
textBytes, dataLength,
buffer,1024,
&numBytesEncrypted);
if(cryptStatus ==kCCSuccess) {
NSData*data = [NSDatadataWithBytes:bufferlength:(NSUInteger)numBytesEncrypted];
ciphertext = [database64Encoding];
}
returnciphertext;
}
///解密
-(NSString*) decryptUseDES:(NSString*)cipherText key:(NSString*)key {
NSData* cipherData = [GTMBase64decodeString:cipherText];
///如果需要解密的字符串太长,可以把下面的buffer数字设大一点
unsigned char buffer[1024*100];
memset(buffer,0,sizeof(char));
size_tnumBytesDecrypted =0;
Byteiv[] = {0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF};
CCCryptorStatuscryptStatus =CCCrypt(kCCDecrypt,
kCCAlgorithmDES,
kCCOptionPKCS7Padding,
[keyUTF8String],
kCCKeySizeDES,
iv,
[cipherDatabytes],
[cipherDatalength],
buffer,
1024*100,
&numBytesDecrypted);
NSString* plainText =nil;
if(cryptStatus ==kCCSuccess) {
NSData* data = [NSDatadataWithBytes:buffer length:(NSUInteger)numBytesDecrypted];
plainText = [[NSStringalloc]initWithData:data encoding:NSUTF8StringEncoding];
}
returnplainText;
}
网友评论