Ios des encryption/decryption (with base64)

Source: Internet
Author: User

# Include <CommonCrypto/CommonCryptor. h>

+ (NSString *) Encrypte :( NSString *) src key :( NSString *) key {

NSString * retStr = nil;

NSData * srcData = [src dataUsingEncoding: NSUTF8StringEncoding];

Size_t dataOutAvilable = ([srcData length] + kCCBlockSize3DES )&~ (KCCBlockSize3DES-1 );

Unsigned char * dataOut = (unsigned char *) malloc (dataOutAvilable * sizeof (unsigned char ));

Memset (void *) dataOut, 0x0, dataOutAvilable );

Size_t dataOutMoved = 0;

Byte iv [] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

Cccryptorstatus ccstatus = cccrypt (kccencrypt, encrypt, decrypt | kccoptionecbmode, (void const *) [Key utf8string], kcckeysize3des, IV, [srcdata bytes], [srcdata length], (void *) dataout, dataoutavilable, & dataoutmoved );

If (ccstatus = 0) {// success base64

Int base64len = base64_encode_length (dataoutmoved) + 2;

Char * TMP = new char [base64len];

Memset (TMP, 0, base64len );

Int base64ret = base64_encode (const char *) dataout, dataoutmoved, TMP, base64len );

If (base64ret> 0 ){

Retstr = [[nsstring alloc] initwithuf8string: TMP] autorelease];

}

Delete [] TMP;

}

// NSLog (@ "ccStatus: % d EncrypteStr: % @", ccStatus, retStr );

Delete dataOut;

Return retStr;

}

+ (NSString *) depolicte :( NSString *) src key :( NSString *) key {

NSString * retStr = nil;

Int base64Len = base64_decode_length ([src length]) + 2;

Char * tmp = new char [base64Len];

Memset (tmp, 0, base64Len );

CCCryptorStatus ccStatus = kCCParamError;

Int base64Ret = base64_decode ([src UTF8String], [src length], tmp, base64Len );

If (base64Ret> 0 ){

Size_t dataOutAvilable = (base64Len + kCCBlockSize3DES )&~ (KCCBlockSize3DES-1 );

Unsigned char * dataOut = (unsigned char *) malloc (dataOutAvilable * sizeof (unsigned char ));

Memset (void *) dataOut, 0x0, dataOutAvilable );

Size_t dataOutMoved = 0;

Byte iv [] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

CcStatus = CCCrypt (kCCDecrypt, encrypt, decrypt | kCCOptionECBMode, (void const *) [key UTF8String], kCCKeySize3DES, iv, (const void *) tmp, base64Ret, (void *) dataOut, dataOutAvilable, & dataOutMoved );

If (ccStatus = 0 ){

RetStr = [[NSString alloc] initWithBytes: dataOut length: dataOutMoved encoding: NSUTF8StringEncoding] autorelease];

}

Delete dataOut;

}

// NSLog (@ "ccStatus: % d depolicte: % @", ccStatus, retStr );

Delete [] tmp;

Return retStr;

}

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.