码迷,mamicode.com
首页 > 移动开发 > 详细

iOS DES加密

时间:2015-06-05 13:58:57      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

+(NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key
{
   //testByte是关键点
    NSData *data1 = [key dataUsingEncoding:NSUTF8StringEncoding];
    Byte *testByte = (Byte *)[data1 bytes];
   
   
    NSString *ciphertext = nil;
    NSData *textData = [plainText dataUsingEncoding:NSUTF8StringEncoding];
    NSUInteger dataLength = [textData length];
    unsigned char buffer[1024];
    memset(buffer, 0, sizeof(char));
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding,
                                          [key UTF8String], kCCKeySizeDES,
                                          testByte,
                                          [textData bytes], dataLength,
                                          buffer, 1024,
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
        ciphertext = [Base64 encode:data];
        NSLog(@"-------------%@",ciphertext);
    }
    return ciphertext;
}


首先建一个NSObject  DesCodeFunc类,写上面的工厂方法 然后在需要调用的地方写 下面的字符串即可 注意后面key需要客户给
[DesCodeFunc encryptUseDES:tfield2cart.text key:@"Markor02"]

iOS DES加密

标签:

原文地址:http://www.cnblogs.com/huangzs/p/4554349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!