首页 > 代码库 > 3DES 加密 解密
3DES 加密 解密
#import <Foundation/Foundation.h>
//3DES加密
@interface JKEncrypt : NSObject
-(NSString *)doEncrypt:(NSString *)plainText;
-(NSString*)doDecEncrypt:(NSString *)encryptText;
@end
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1d9421 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c91b13 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #822e0e } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #1d9421 } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c32275 } p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo } p.p8 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81 } p.p9 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #703daa } p.p10 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #6122ae } span.s1 { } span.s2 { font: 18.0px "PingFang SC" } span.s3 { color: #822e0e } span.s4 { color: #c91b13 } span.s5 { color: #1d9421 } span.s6 { font: 18.0px "PingFang SC"; color: #1d9421 } span.s7 { font: 18.0px Menlo } span.s8 { color: #000000 } span.s9 { color: #6122ae } span.s10 { color: #703daa } span.s11 { color: #3d1d81 } span.s12 { color: #c32275 } span.s13 { color: #0435ff } span.s14 { color: #78492a }</style>//
// main.m
// 3DES研究
//
// Created by apple on 15/10/22.
// Copyright ? 2015年 apple. All rights reserved.
//
#import "JKEncrypt.h"
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonCryptor.h>
#import <Security/Security.h>
//密匙 key
#define gkey @"123456788765432112345678" //24位的密钥
//偏移量
#define gIv @"jukai"
@implementation JKEncrypt
-(NSString *)doEncrypt:plainText{
//把string 转NSData
NSData* data = http://www.mamicode.com/[plainText dataUsingEncoding:NSUTF8StringEncoding];
//length
size_t plainTextBufferSize = [data length];
const void *vplainText = (const void *)[data bytes];
CCCryptorStatus ccStatus;
uint8_t *bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t movedBytes = 0;
bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
memset((void *)bufferPtr, 0x0, bufferPtrSize);
const void *vkey = (const void *) [gkey UTF8String];
//偏移量
const void *vinitVec = (const void *) [gIv UTF8String];
//配置CCCrypt
ccStatus = CCCrypt(kCCEncrypt,
kCCAlgorithm3DES, //3DES
kCCOptionECBMode|kCCOptionPKCS7Padding, //设置模式
vkey, //key
kCCKeySize3DES,
// vinitVec, //偏移量,这里不用,设置为nil;不用的话,必须为nil,不可以为@“” 解密算法里也应该为nil,也不可以为@“”
nil, //偏移量,这里不用,设置为nil;不用的话,必须为nil,不可以为@“”
vplainText,
plainTextBufferSize,
(void *)bufferPtr,
bufferPtrSize,
&movedBytes);
NSData *myData = http://www.mamicode.com/[NSData dataWithBytes:(const char *)bufferPtr length:(NSUInteger)movedBytes];
NSUInteger len = [myData length];
char * chars = (char *)[myData bytes];
NSMutableString * hexString = [[NSMutableString alloc] init];
for(NSUInteger i = 0; i < len; i++ )
[hexString appendString:[NSString stringWithFormat:@"%0.2hhx", chars[i]]];
return hexString;
}
-(NSString*)doDecEncrypt:(NSString *)hexString{
//十六进制转NSData
long len = [hexString length] / 2;
unsigned char *buf = malloc(len);
unsigned char *whole_byte = buf;
char byte_chars[3] = {‘\0‘,‘\0‘,‘\0‘};
int i;
for (i=0; i < [hexString length] / 2; i++) {
byte_chars[0] = [hexString characterAtIndex:i*2];
byte_chars[1] = [hexString characterAtIndex:i*2+1];
*whole_byte = strtol(byte_chars, NULL, 16);
whole_byte++;
}
NSData *encryptData = http://www.mamicode.com/[NSData dataWithBytes:buf length:len];
size_t plainTextBufferSize = [encryptData length];
const void *vplainText = [encryptData bytes];
CCCryptorStatus ccStatus;
uint8_t *bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t movedBytes = 0;
bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
memset((void *)bufferPtr, 0x0, bufferPtrSize);
const void *vkey = (const void *) [gkey UTF8String];
const void *vinitVec = (const void *) [gIv UTF8String];
ccStatus = CCCrypt(kCCDecrypt,
kCCAlgorithm3DES,
kCCOptionPKCS7Padding|kCCOptionECBMode,
vkey,
kCCKeySize3DES,
nil,
vplainText,
plainTextBufferSize,
(void *)bufferPtr,
bufferPtrSize,
&movedBytes);
NSString *result = [[NSString alloc] initWithData:[NSData dataWithBytes:(const void *)bufferPtr
length:(NSUInteger)movedBytes] encoding:NSUTF8StringEncoding];
return result;
}
@end
3DES 加密 解密