iOS|iOS 笔记之_时间戳 + DES 加密

【iOS|iOS 笔记之_时间戳 + DES 加密】iOS 时间戳 + DES 加密
  • 时间戳
NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0]; NSTimeInterval a=[date timeIntervalSince1970]*1000; NSString *timeString = [NSString stringWithFormat:@"%f", a]; NSString *timeStamp = [timeString substringToIndex:13];

  • DES 加密解密
#import "ViewController.h" @interface ViewController ()@end@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //加密内容 NSString *plainText = @"helloWorld"; NSLog(@"加密内容:%@",plainText); //秘钥 NSString *seckey = @"seckey"; NSString *result = [self encryptUseDES:plainText Andkey:seckey]; NSLog(@"加密结果:%@",result); NSString *decresult = [self decryptUseDES:result Andkey:seckey]; NSLog(@"解密结果:%@",decresult); }//加密 -(NSString *) encryptUseDES:(NSString *)clearText Andkey:(NSString *)key { NSString *ciphertext = nil; NSData *textData = https://www.it610.com/article/[clearText dataUsingEncoding:NSUTF8StringEncoding]; NSUInteger dataLength = [textData length]; size_t bufferSize = dataLength + kCCBlockSizeAES128; void * buffer = malloc(bufferSize); size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionPKCS7Padding | kCCOptionECBMode, [key UTF8String], kCCBlockSizeDES, NULL, [textData bytes], dataLength, buffer, bufferSize, &numBytesEncrypted); if (cryptStatus == kCCSuccess) { NSLog(@"DES加密成功"); NSData *data = https://www.it610.com/article/[NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted]; ciphertext = [self stringWithHexBytes2:data]; }else{ NSLog(@"DES加密失败"); }free(buffer); return ciphertext; }-(NSString *) decryptUseDES:(NSString *)plainText Andkey:(NSString *)key { NSString *cleartext = nil; NSData *textData = https://www.it610.com/article/[self parseHexToByteArray:plainText]; NSUInteger dataLength = [textData length]; size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc(bufferSize); size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmDES, kCCOptionPKCS7Padding | kCCOptionECBMode, [key UTF8String], kCCKeySizeDES, NULL, [textData bytes], dataLength, buffer, bufferSize, &numBytesEncrypted); if (cryptStatus == kCCSuccess) { NSLog(@"DES解密成功"); NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted]; cleartext = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; }else{ NSLog(@"DES解密失败"); }free(buffer); return cleartext; }//nsdata转成16进制字符串 - (NSString*)stringWithHexBytes2:(NSData *)sender { static const char hexdigits[] = "0123456789ABCDEF"; const size_t numBytes = [sender length]; const unsigned char* bytes = [sender bytes]; char *strbuf = (char *)malloc(numBytes * 2 + 1); char *hex = strbuf; NSString *hexBytes = nil; for (int i = 0; i> 4) & 0xF]; *hex++ = hexdigits[(c ) & 0xF]; }*hex = 0; hexBytes = [NSString stringWithUTF8String:strbuf]; free(strbuf); return hexBytes; }/* 将16进制数据转化成NSData 数组 */ -(NSData*) parseHexToByteArray:(NSString*) hexString { int j=0; Byte bytes[hexString.length]; for(int i=0; i<[hexString length]; i++) { int int_ch; /// 两位16进制数转化后的10进制数 unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16) int int_ch1; if(hex_char1 >= '0' && hex_char1 <='9') int_ch1 = (hex_char1-48)*16; //// 0 的Ascll - 48 else if(hex_char1 >= 'A' && hex_char1 <='F') int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65 else int_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97 i++; unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位) int int_ch2; if(hex_char2 >= '0' && hex_char2 <='9') int_ch2 = (hex_char2-48); //// 0 的Ascll - 48 else if(hex_char2 >= 'A' && hex_char1 <='F') int_ch2 = hex_char2-55; //// A 的Ascll - 65 else int_ch2 = hex_char2-87; //// a 的Ascll - 97int_ch = int_ch1+int_ch2; bytes[j] = int_ch; ///将转化后的数放入Byte数组里 j++; }NSData *newData = https://www.it610.com/article/[[NSData alloc] initWithBytes:bytes length:hexString.length/2]; return newData; } @end

就酱...

加密内容:helloWorld
DES加密成功
加密结果:F6F8FDCDA4FD6B837E34C208AB6BA36C
DES解密成功
解密结果:helloWorld

    推荐阅读