iOS develops various encryption, decryption, codec

Source: Internet
Author: User

1.AES Plus decryption, add category to NSData Nsdata+aes

Add header file #import <CommonCrypto/CommonCryptor.h>

-(NSData *) Aes256encryptwithkey: (NSString *) key {//encryption

Char keyptr[kcckeysizeaes256+1];
Bzero (keyptr, sizeof (KEYPTR));
[Key Getcstring:keyptr maxlength:sizeof (KEYPTR) encoding:nsutf8stringencoding];
Nsuinteger datalength = [self length];
size_t buffersize = datalength + kCCBlockSizeAES128;
void *buffer = malloc (buffersize);
size_t numbytesencrypted = 0;
Cccryptorstatus cryptstatus = Cccrypt (Kccencrypt, kCCAlgorithmAES128,
kccoptionpkcs7padding | Kccoptionecbmode,
Keyptr, kCCBlockSizeAES128,
Null
[Self bytes], datalength,
Buffer, buffersize,
&numbytesencrypted);
if (Cryptstatus = = kccsuccess) {
return [NSData Datawithbytesnocopy:buffer length:numbytesencrypted];
}
Free (buffer);
return nil;
}


-(NSData *) Aes256decryptwithkey: (NSString *) key {//decryption
Char keyptr[kcckeysizeaes256+1];
Bzero (keyptr, sizeof (KEYPTR));
[Key Getcstring:keyptr maxlength:sizeof (KEYPTR) encoding:nsutf8stringencoding];
Nsuinteger datalength = [self length];
size_t buffersize = datalength + kCCBlockSizeAES128;
void *buffer = malloc (buffersize);
size_t numbytesdecrypted = 0;
Cccryptorstatus cryptstatus = Cccrypt (Kccdecrypt, kCCAlgorithmAES128,
kccoptionpkcs7padding | Kccoptionecbmode,
Keyptr, kCCBlockSizeAES128,
Null
[Self bytes], datalength,
Buffer, buffersize,
&numbytesdecrypted);
if (Cryptstatus = = kccsuccess) {
return [NSData Datawithbytesnocopy:buffer length:numbytesdecrypted];
}
Free (buffer);
return nil;

}

2.BASE64 codec reference

GTMDefines.h
GTMBase64.h
Gtmbase64.m

http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/?r=87

http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/?r=575


3. Various encryption, Digital digest. Add category Nsstring+encrypto to NSString

(1). MD5 Digital Digest

-(NSString *) MD5 {

const char *CSTR = [self utf8string];
unsigned char result[16];
CC_MD5 (CStr, strlen (CSTR), result);
return [NSString stringwithformat:@ "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
Result[0], result[1], result[2], result[3],
RESULT[4], result[5], result[6], result[7],
RESULT[8], result[9], result[10], result[11],
RESULT[12], result[13], result[14], result[15]
];
}


(2) SHA1 Summary

-(NSString*) sha1{     const char *CSTR = [self cstringusingencoding:nsutf8stringencoding];    NSData *data = [NSData dat Awithbytes:cstr length:self.length];     uint8_t digest[cc_sha1_digest_length];     CC _SHA1 (Data.bytes, Data.length, Digest);     nsmutablestring* output = [nsmutablestring  Stringwithcapacity:cc_sha1_digest_length * 2];     for (int i = 0; i < cc_sha1_digest_length; i++)         [Output appendformat:@ "%02x", digest[i]];     return output;}  
(3) Base64 code
-(NSString *) base64{nsdata * data = [self datausingencoding:nsasciistringencoding allowlossyconversion:yes];     data = [GTMBase64 encodedata:data];     NSString * output = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; return output; }
 (4) SHA1 combined with Base64 
-(NSString *) sha1_ base64{    const char *CSTR = [self cstringusingencoding:nsutf8stringencoding];    NSData *data = [NSD ATA datawithbytes:cstr length:self.length];     uint8_t digest[cc_sha1_digest_length];     CC_SHA1 (Data.bytes, Data.length, Digest);     NSData * base64 = [[NSData alloc]initwithbytes: Digest length:cc_sha1_digest_length];    base64 = [GTMBase64 encodedata:base64];     NSString * output = [[NSString alloc] initwithdata:base64 encoding:nsutf8stringencoding];     return output;  }   
(5) MD5 combined with base64-(NSString *) md5_base64{const char *CSTR = [self utf8string];    unsigned char digest[cc_md5_digest_length];     CC_MD5 (CStr, strlen (CSTR), Digest);    NSData * base64 = [[NSData alloc]initwithbytes:digest length:cc_md5_digest_length];     Base64 = [GTMBase64 encodedata:base64];     NSString * output = [[NSString alloc] initwithdata:base64 encoding:nsutf8stringencoding]; return output;}

4. 3DES is a symmetric encryption method, because the same key is used.

For encryption and decryption security what we can google,baidu themselves to find information reference.

I'm simply talking about an achievable scenario in communication encryption.

The same 3DES encryption is basically unified, the system also provides the API directly, the basic code is as follows

3DES plus decryption
+ (nsstring*) TripleDES: (nsstring*) plaintext Encryptordecrypt: (ccoperation) Encryptordecrypt
{
const void *vplaintext;
size_t Plaintextbuffersize;

if (Encryptordecrypt = = Kccdecrypt)//decryption
{
NSData *encryptdata = [GTMBase64 decodedata:[plaintext datausingencoding:nsutf8stringencoding];
Plaintextbuffersize = [EncryptData length];
vplaintext = [EncryptData bytes];
}
else//encryption
{
nsdata* data = [plaintext datausingencoding:nsutf8stringencoding];
plaintextbuffersize = [data length];
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);
memset (void *) IV, 0x0, (size_t) sizeof (iv));

const void *vkey = (const void *) [Deskey utf8string];
NSString *initvec = @ "init Vec";
const void *vinitvec = (const void *) [Initvec utf8string];
Byte iv[] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
Ccstatus = Cccrypt (Encryptordecrypt,
Kccalgorithm3des,
kccoptionpkcs7padding | Kccoptionecbmode,
Vkey,
Kcckeysize3des,
Nil
Vplaintext,
Plaintextbuffersize,
(void *) Bufferptr,
Bufferptrsize,
&movedbytes);
if (Ccstatus = = kccsuccess) NSLog (@ "SUCCESS");
/*else if (ccstatus = = KCC Paramerror) return @ "PARAM ERROR";
else if (ccstatus = = Kccbuffertoosmall) return @ "BUFFER TOO SMALL";
else if (ccstatus = = kccmemoryfailure) return @ "MEMORY FAILURE";
else if (ccstatus = = Kccalignmenterror) return @ "ALIGNMENT";
else if (ccstatus = = Kccdecodeerror) return @ "DECODE ERROR";
else if (ccstatus = = kccunimplemented) return @ "unimplemented"; */

NSString *result;

if (Encryptordecrypt = = Kccdecrypt)
{
result = [[[NSString Alloc] initwithdata:[nsdata datawithbytes: (const void *) Bufferptr
Length: (Nsuinteger) movedbytes]
Encoding:nsutf8stringencoding]
Autorelease];
}
Else
{
NSData *mydata = [NSData datawithbytes: (const void *) Bufferptr length: (Nsuinteger) movedbytes];
result = [GTMBase64 stringbyencodingdata:mydata];
}

return result;
}

iOS develops various encryption, decryption, codec

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.