IOS 3DES Encryption Decryption (one line of code is done)

Source: Internet
Author: User

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"

// Secret Key key #define Gkey            @ "Kyle_chu"// offset #define gIv             @ "Jukai"

//String Encryption-(NSString *) Doencryptstr: (NSString *) originalstr{//turn the string NSDatansdata* data =[Originalstr datausingencoding:nsutf8stringencoding]; //lengthsize_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 Amount    Const void*vinitvec = (Const void*) [GIv utf8string]; //Configure CccryptCcstatus =Cccrypt (Kccencrypt, Kccalgorithm3des,//3DESKccoptionecbmode|kccoptionpkcs7padding,//Setup ModeVkey,//Keykcckeysize3des, Vinitvec,//offset, not here, set to nil, no, must be nil, not to think @ ""Vplaintext, Plaintextbuffersize, (void*) bufferptr, Bufferptrsize,&movedbytes); NSData*mydata = [NSData datawithbytes: (Const void*) bufferptr Length: (Nsuinteger) movedbytes]; NSString*result =[GTMBase64 Stringbyencodingdata:mydata]; returnresult;}

//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|Kccoptionecbmode, vkey, Kcckeysize3des, Vinitvec, Vplaintext, Plaintextbuffersize, (void*) bufferptr, Bufferptrsize,&movedbytes); NSString*result = [[NSString alloc] initwithdata:[nsdata datawithbytes: (Const void*) bufferptr Length: (Nsuinteger) movedbytes] Encodin            G:nsutf8stringencoding]; returnresult;}

The hexadecimal implementation is omitted and can be read jkencrypt.m

Use of Jkencrypt:

// 1. Set the key and offset you need // Secret Key key #define Gkey            @ "Kyle_chu"// offset #define gIv             @ "Jukai"

//@ "Kyle_jukai" is a test string, replaced with the content you need to encryptJkencrypt* en =[[Jkencrypt alloc]init];//EncryptNSString * encryptstr = [en doencryptstr:@"Kyle_jukai"]; NSString* Encrypthex = [en doencrypthex:@"Kyle_jukai"]; NSLog (@"string encryption:%@", ENCRYPTSTR); NSLog (@"Hex Encryption:%@", Encrypthex);//decryptionNSString *decencryptstr =[en dodecencryptstr:encryptstr]; NSString*decencrypthex =[en doencrypthex:encrypthex]; NSLog (@"string decryption:%@", DECENCRYPTSTR); NSLog (@"string decryption:%@", Decencrypthex);

IOS 3DES Encryption Decryption (one line of code is done)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.