ios des 加/解密(with base64)

來源:互聯網
上載者: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, kCCAlgorithm3DES, kCCOptionPKCS7Padding | kCCOptionECBMode, (void const*)[key UTF8String], kCCKeySize3DES, iv, [srcData bytes], [srcData length], (void*)dataOut, dataOutAvilable, &dataOutMoved);

    if (ccStatus == 0) {//成功 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] initWithUTF8String:tmp] autorelease];

        }

        delete []tmp;

    }

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

    delete dataOut;

    return retStr;

}

+ (NSString*)Decypte:(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, kCCAlgorithm3DES, kCCOptionPKCS7Padding | 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 Decypte:%@",ccStatus,retStr);

    delete []tmp;

    return retStr;

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.