iOS DES加密

+(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"]

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。