美文网首页工具
关于iOS的des加密解密(适合中文加密和长字符串加密解密)

关于iOS的des加密解密(适合中文加密和长字符串加密解密)

作者: 不必luo嗦 | 来源:发表于2016-04-22 14:50 被阅读3496次

    /// 工程中只需把下面的2个方法拖进去,然后就可以调用,且需要导入头文件#import"GTMBase64.h"

    #include<CommonCrypto/CommonCryptor.h>

    ///加密算法

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

    -(NSString*) encryptUseDESS:(NSString*)plainText key:(NSString*)key

    {

    NSString*ciphertext =nil;

    constchar*textBytes = [plainTextUTF8String];

    NSUIntegerdataLength =strlen(textBytes);

    unsignedcharbuffer[1024];

    memset(buffer,0,sizeof(char));

    size_tnumBytesEncrypted =0;

    CCCryptorStatuscryptStatus =CCCrypt(kCCEncrypt,kCCAlgorithmDES,

    kCCOptionPKCS7Padding,

    [keyUTF8String],kCCKeySizeDES,

    iv,

    textBytes, dataLength,

    buffer,1024,

    &numBytesEncrypted);

    if(cryptStatus ==kCCSuccess) {

    NSData*data = [NSDatadataWithBytes:bufferlength:(NSUInteger)numBytesEncrypted];

    ciphertext = [database64Encoding];

    }

    returnciphertext;

    }

    ///解密

    -(NSString*) decryptUseDES:(NSString*)cipherText key:(NSString*)key {

    NSData* cipherData = [GTMBase64decodeString:cipherText];

    ///如果需要解密的字符串太长,可以把下面的buffer数字设大一点

    unsigned char buffer[1024*100];

    memset(buffer,0,sizeof(char));

    size_tnumBytesDecrypted =0;

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

    CCCryptorStatuscryptStatus =CCCrypt(kCCDecrypt,

    kCCAlgorithmDES,

    kCCOptionPKCS7Padding,

    [keyUTF8String],

    kCCKeySizeDES,

    iv,

    [cipherDatabytes],

    [cipherDatalength],

    buffer,

    1024*100,

    &numBytesDecrypted);

    NSString* plainText =nil;

    if(cryptStatus ==kCCSuccess) {

    NSData* data = [NSDatadataWithBytes:buffer length:(NSUInteger)numBytesDecrypted];

    plainText = [[NSStringalloc]initWithData:data encoding:NSUTF8StringEncoding];

    }

    returnplainText;

    }

    相关文章

      网友评论

      本文标题: 关于iOS的des加密解密(适合中文加密和长字符串加密解密)

      本文链接:https://www.haomeiwen.com/subject/splxrttx.html