一、DES加密算法介绍
- 要求秘钥必须是8个字节,即64bit长度
- 因为秘钥是byte[8],代表字符串也可以是非可见的字节,可以与Base64编码算法一起使用
- 加密、解密都需要通过字节数组作为数据和密钥进行处理
二、对称加密
DES 加密算法属于对称加密。
即利用制定的密钥,按照密码的长度截取数据,分成数据块,和密钥进行复杂的移位、算数运算或者数据处理等操作,形成只有特定的密码才能够解开的数据。加密与解密用的是同一个密钥
三、代码
1.使用DES加密,示例代码👉
//加密
+(NSString *) encryptUseDES2:(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 = key;
NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];
Byte *iv = (Byte *)[testData bytes];
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
kCCOptionPKCS7Padding,
[key UTF8String], kCCKeySizeDES,
iv,
textBytes, dataLength,
(void *)bufferPtr, bufferPtrSize,
&movedBytes);
if (cryptStatus == kCCSuccess) {
ciphertext= [XuDes parseByte2HexString:bufferPtr :(int)movedBytes];
}
ciphertext=[ciphertext uppercaseString];//字符变大写
return ciphertext ;
}
2.使用DES进行解密计算
//解密
+(NSString *)decryptUseDES:(NSString *)cipherText key:(NSString *)key
{
NSData* cipherData = [XuDes convertHexStrToData:[cipherText lowercaseString]];
NSLog(@"++++++++///%@",cipherData);
unsigned char buffer[1024];
memset(buffer, 0, sizeof(char));
size_t numBytesDecrypted = 0;
NSString *testString = key;
NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];
Byte *iv = (Byte *)[testData bytes];
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
kCCAlgorithmDES,
kCCOptionPKCS7Padding,
[key UTF8String],
kCCKeySizeDES,
iv,
[cipherData bytes],
[cipherData length],
buffer,
1024,
&numBytesDecrypted);
NSString* plainText = nil;
if (cryptStatus == kCCSuccess) {
NSData* data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesDecrypted];
plainText = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
return plainText;
}
加密解密中有两个方法调用,其实是为了16进制与data之间的转换。有些宝宝们的公司并未转换成16进制,而是需要跟base64共同加解密、方法使用,只需要将得出的plainText的值转换成base64即可。
加密时转成16进制
+(NSString *) parseByte2HexString:(Byte *) bytes :(int)len{
NSString *hexStr = @"";
if(bytes)
{
for(int i=0;i<len;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(@"%@",hexStr);
}
}
return hexStr;
}
解密时转回data
+ (NSData *)convertHexStrToData:(NSString *)str {
if (!str || [str length] == 0) {
return nil;
}
NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:8];
NSRange range;
if ([str length] % 2 == 0) {
range = NSMakeRange(0, 2);
} else {
range = NSMakeRange(0, 1);
}
for (NSInteger i = range.location; i < [str length]; i += 2) {
unsigned int anInt;
NSString *hexCharStr = [str substringWithRange:range];
NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr];
[scanner scanHexInt:&anInt];
NSData *entity = [[NSData alloc] initWithBytes:&anInt length:1];
[hexData appendData:entity];
range.location += range.length;
range.length = 2;
}
NSLog(@"hexdata: %@", hexData);
return hexData;
}
网友评论