//iOS DES ECB 模式加密
#import <CommonCrypto/CommonCryptor.h> static Byte iv[] = {,,,,,,,};
+(NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key { NSString *ciphertext = nil; const char *textBytes = [plainText UTF8String]; NSUInteger dataLength = [plainText length]; unsigned char buffer[]; memset(buffer, , sizeof(char)); size_t numBytesEncrypted = ; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionECBMode|kCCOptionPKCS7Padding, //kCCOptionECBMode kCCOptionPKCS7Padding [key UTF8String], kCCKeySizeDES, iv, textBytes, dataLength, buffer, , &numBytesEncrypted);
if (cryptStatus == kCCSuccess) { NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
//NSLog(@"ssf:%s",buffer);
ciphertext = [ViewController base64Encoding:data];
// NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
// Byte* bb = (Byte*)[data bytes];
// ciphertext = [self parseByteArray2HexString:bb]; } return ciphertext; } static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +(NSString *)base64Encoding:(NSData*) text
{
if (text.length == )
return @""; char *characters = malloc(text.length*/); if (characters == NULL)
return @""; int end = text.length - ;
int index = ;
int charCount = ;
int n = ; while (index <= end) {
int d = (((int)(((char *)[text bytes])[index]) & 0x0ff) << )
| (((int)(((char *)[text bytes])[index + ]) & 0x0ff) << )
| ((int)(((char *)[text bytes])[index + ]) & 0x0ff); characters[charCount++] = encodingTable[(d >> ) & ];
characters[charCount++] = encodingTable[(d >> ) & ];
characters[charCount++] = encodingTable[(d >> ) & ];
characters[charCount++] = encodingTable[d & ]; index += ; if(n++ >= )
{
n = ;
characters[charCount++] = ' ';
}
} if(index == text.length - )
{
int d = (((int)(((char *)[text bytes])[index]) & 0x0ff) << )
| (((int)(((char *)[text bytes])[index + ]) & ) << );
characters[charCount++] = encodingTable[(d >> ) & ];
characters[charCount++] = encodingTable[(d >> ) & ];
characters[charCount++] = encodingTable[(d >> ) & ];
characters[charCount++] = '=';
}
else if(index == text.length - )
{
int d = ((int)(((char *)[text bytes])[index]) & 0x0ff) << ;
characters[charCount++] = encodingTable[(d >> ) & ];
characters[charCount++] = encodingTable[(d >> ) & ];
characters[charCount++] = '=';
characters[charCount++] = '=';
}
NSString * rtnStr = [[NSString alloc] initWithBytesNoCopy:characters length:charCount encoding:NSUTF8StringEncoding freeWhenDone:YES];
return rtnStr;
}