Encryption of Ios--3des

Source: Internet
Author: User

Today, the 3DES encryption method to do a deep understanding, because the recent project needs, to encrypt the interface, so the unanimous discussion, the use of DES encryption, but there are some problems, wasted some time, here, the author will own DES encryption method to share to everyone, Hope to help everyone ~ ~

1. First to the DSE encryption must have Base64 file, here I do not too much to introduce Base64, because Base64 are the same, as long as the download good Base64 integrated into the project is good

2. Define a des file here for Mgdes

3. The method to declare encryption and decryption in the. h file:

/**

* DES3 encryption of all incoming request parameters

*/

+ (nsstring *) encryptparameters: (nsstring *) string;

/**

* DES3 decryption of all incoming request parameters

*/

+ (nsstring *) Decryptusedes: (nsstring *) ciphertext key: (nsstring *) key;

4. Implementing class methods in. m files

(1) First define an encrypted private key

#define Des_key @"24-bit (must be 24-bit)"

(2) Import header file

#import "GTMBase64.h"

#import <CommonCrypto/CommonCryptor.h>

#import <CommonCrypto/CommonHMAC.h>

#import <CommonCrypto/CommonDigest.h>

(3) Define the offset

#define GIV @ "01234567"

(4) Implementation + method

Encryption method

+ (NSString *) Encryptparameters: (NSString *) string

{

return [self encryptusedes:string key:des_key];

}

+ (NSString *) Encryptusedes: (NSString *) plaintext key: (NSString *) key

{

nsdata* data = [plaintext datausingencoding:nsutf8stringencoding];

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 *) [key utf8string];

const void *vinitvec = (const void *) [gIv utf8string];

Kccoptionpkcs7padding

Ccstatus = Cccrypt (Kccencrypt,

Kccalgorithm3des,

Kccoptionpkcs7padding,

Vkey,

Kcckeysize3des,

Vinitvec,

Vplaintext,

Plaintextbuffersize,

(void *) Bufferptr,

Bufferptrsize,

&movedbytes);

NSData *mydata = [NSData datawithbytes: (const void *) Bufferptr length: (Nsuinteger) movedbytes];

NSString *result = [GTMBase64 stringbyencodingdata:mydata];

return result;

}

Decryption method

+ (NSString *) Decryptusedes: (NSString *) ciphertext key: (NSString *) key

{

NSData *encryptdata = [GTMBase64 decodedata:[ciphertext 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 *) [key utf8string];

const void *vinitvec = (const void *) [gIv utf8string];

Ccstatus = Cccrypt (Kccdecrypt,

Kccalgorithm3des,

Kccoptionpkcs7padding,

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;

}

5. It is important to note that the encrypted private key must be 24 bits, otherwise it will be inconsistent with the background and Android encryption results, resulting in the background can not decrypt our encrypted content

6. Attach a piece of Android code, Android and iOS and background must be consistent.

Encryption Method (Android)

public static string encode (string plaintext, string key) throws Exception {Desedekeyspec spec = new Desedekeyspec (GetKey (key). GetBytes ()); Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("Desede"); Key Deskey = Keyfactory.generatesecret (spec); Cipher Cipher = cipher.getinstance ("desede/cbc/pkcs5padding"); Ivparameterspec ips = new Ivparameterspec (Iv.getbytes ()); Cipher.init (Cipher.encrypt_mode, Deskey, IPs); byte[] EncryptData = cipher.dofinal (plaintext.getbytes (encoding)); return new String (Base64.encode (encryptData)); }

Encryption of Ios--3des

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.