+ (NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key
{
NSString *ciphertext = nil;
const char *textBytes = [plainText UTF8String];
size_t dataLength = [plainText length];
//==================
uint8_t *bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t movedBytes = 0;
bufferPtrSize = (dataLength + kCCBlockSizeDES) & ~(kCCBlockSizeDES - 1);
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
memset((void *)bufferPtr, 0x0, bufferPtrSize);
NSString *testString = @"f7afc629";
NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];
Byte *iv = (Byte *)[testData bytes];
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
kCCOptionPKCS7Padding,
iv, kCCKeySizeDES,
iv,
textBytes, dataLength,
(void *)bufferPtr, bufferPtrSize,
&movedBytes);
if (cryptStatus == kCCSuccess) {
// NSData *data = [NSData dataWithBytes:(void *)bufferPtr length:(NSUInteger)movedBytes];
/*
//============================test=========
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
NSString *new=[[NSString alloc]initWithData:data encoding:enc];
//===================================================
*/
// char *testbyte=(char *)[data bytes];
//NSData *dd = [NSData dataWithBytes:textBytes length:(NSInteger)movedBytes];
// NSLog(@"%x",[data bytes]);
// int t = sizeof(testbyte);
ciphertext= [des parseByte2HexString:bufferPtr :movedBytes];
NSLog(@"ciphertext==%@",ciphertext);
}
ciphertext=[ciphertext uppercaseString];
return ciphertext ;
}
+(NSString *) parseByte2HexString:(Byte *) bytes :(int)len{
NSString *hexStr = @"";
if(bytes)
{
for(int i=0;i { NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff]; ///16进制数 if([newHexStr length]==1) hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr]; else hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr]; } } NSLog(@"bytes 的16进制数为:%@",hexStr); return hexStr; }