3DES (or Triple DES) is a generic term for the triple Data encryption algorithm (Tdea,triple encryption algorithm) block cipher. It is equivalent to applying three DES encryption algorithms to each block of data. Because of the enhancement of computing ability, the key length of the original des password becomes easy to be cracked; 3DES is designed to provide a relatively simple way to avoid similar attacks by increasing the key length of DES, rather than designing a new block cipher algorithm.
3DES, also known as Triple DES, is a mode of DES encryption algorithm that uses 3 56-bit keys to encrypt data three times. Data Encryption Standard (DES) is a long-standing encryption standard in the United States, which uses symmetric key cryptography, and was ANSI x.3.92 in 1981. Des uses a 56-bit key and cipher block method, and in the Cipher block method, the text is divided into 64-bit-sized blocks of text and then encrypted. More secure than the original des,3des.
A line of code to implement 3DES encryption and decryption needs to write Jkencrypt https://github.com/jukai9316/JKEncrypt.
The following 3DES implementation is resolved, and then again, how to use jkencrypt.
Note: The Fill method is different.
In the process of interacting with the background, because Java is used pkcs5padding, and iOS only kccoptionpkcs7padding, so with kccoptionpkcs7padding | Kccoptionecbmode equivalent to pkcs5padding.
The following are the implementations of 3DES 256 in IOS development:
#import <CommonCrypto/CommonDigest.h> #import <CommonCrypto/CommonCryptor.h> #import <security/ security.h> #import "GTMBase64.h"
Key Key#define Gkey @ "Kyle_chu"//Offset # define GIV @ "Jukai"
String Encryption-(NSString *) Doencryptstr: (NSString *) originalstr{//Turn string nsdata nsdata* data = [Originalstr datausin Gencoding: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]; Offset const void *vinitvec = (const void *) [gIv utf8string]; Configuration Cccrypt ccstatus = Cccrypt (Kccencrypt, kccalgorithm3des,//3des Kccopti Onecbmode|kccoptionpkcs7padding,//Set mode vkey,//key kcckeysize3des, Vinitvec,//offset, not here, set to nil, no, must be nil, do not think @ "" Vplaintext, Plaintextbuffersize, (void *) Bufferptr, bufferptrsize, &movedbytes); NSData *mydata = [NSData datawithbytes: (const void *) Bufferptr length: (Nsuinteger) movedbytes]; NSString *result = [GTMBase64 stringbyencodingdata:mydata]; return result;}
String decryption-(nsstring*) Dodecencryptstr: (NSString *) encryptstr{nsdata *encryptdata = [GTMBase64 decodedata:[encryptstr Datausingencoding:nsutf8stringencoding]]; 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|kccopti Onecbmode, vkey, Kcckeysize3des, Vinitvec, Vplaintext, Plaintextbuffersize, (void *) Bufferptr, bufferptrsize, &movedbytes); NSString *result = [[NSString alloc] initwithdata:[nsdata datawithbytes: (const void *) Bufferptr Length: (Nsuinteger) movedbytes] encoding:nsutf8stringencoding]; return result;}
The hexadecimal implementation is omitted and can be read jkencrypt.m
Use of Jkencrypt:
1. Set the key you need, offset//key key#define Gkey @ "Kyle_chu"//Offset # define GIV @ "Jukai"
@ "Kyle_jukai" is a test string, replaced with what you need to encrypt jkencrypt * en = [[Jkencrypt alloc]init];//encryption NSString * encryptstr = [en doencryptstr: @ "Kyle_jukai"]; NSString * Encrypthex = [en Doencrypthex: @ "Kyle_jukai"]; NSLog (@ "string encryption:%@", ENCRYPTSTR); NSLog (@ "hex encryption:%@", encrypthex);//decryption NSString *decencryptstr = [en dodecencryptstr:encryptstr]; NSString *decencrypthex = [en doencrypthex:encrypthex]; NSLog (@ "string decryption:%@", DECENCRYPTSTR); NSLog (@ "string decryption:%@", Decencrypthex);
Jiang ==ios 3DES Encryption decryption