• iOS DES ECB 模式加密


    //iOS DES  ECB 模式加密 
    #import <CommonCrypto/CommonCryptor.h>


    static Byte iv[] = {1,2,3,4,5,6,7,8};
    +(NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key
    
     {
        
             NSString *ciphertext = nil;
        
             const char *textBytes = [plainText UTF8String];
        
             NSUInteger dataLength = [plainText length];
         
    
            unsigned char buffer[1024];
        
             memset(buffer, 0, sizeof(char));
        
             size_t numBytesEncrypted = 0;
       
             CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
                                                     
                                                                                                kCCOptionECBMode|kCCOptionPKCS7Padding,  //kCCOptionECBMode  kCCOptionPKCS7Padding
                                                     
                                                                                                [key UTF8String], kCCKeySizeDES,
                                                     
                                                                                                iv,
                                                     
                                                                                              textBytes, dataLength,
                                                     
                                                                                                buffer, 1024,
                                                     
                                                                                                &numBytesEncrypted);
                if (cryptStatus == kCCSuccess) {
            
                    
                     NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
                    //NSLog(@"ssf:%s",buffer);
                    ciphertext = [ViewController base64Encoding:data];
                   // NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
                    //        Byte* bb = (Byte*)[data bytes];
                    //        ciphertext = [self parseByteArray2HexString:bb];
            
                 }
        
             return ciphertext;
       
         
    }
    
    
    
     static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    
    
    +(NSString *)base64Encoding:(NSData*) text
    {
        if (text.length == 0)
            return @"";
        
        char *characters = malloc(text.length*3/2);
        
        if (characters == NULL)
            return @"";
        
        int end = text.length - 3;
        int index = 0;
        int charCount = 0;
        int n = 0;
        
        while (index <= end) {
            int d = (((int)(((char *)[text bytes])[index]) & 0x0ff) << 16)
            | (((int)(((char *)[text bytes])[index + 1]) & 0x0ff) << 8)
            | ((int)(((char *)[text bytes])[index + 2]) & 0x0ff);
            
            characters[charCount++] = encodingTable[(d >> 18) & 63];
            characters[charCount++] = encodingTable[(d >> 12) & 63];
            characters[charCount++] = encodingTable[(d >> 6) & 63];
            characters[charCount++] = encodingTable[d & 63];
            
            index += 3;
            
            if(n++ >= 14)
            {
                n = 0;
                characters[charCount++] = ' ';
            }
        }
        
        if(index == text.length - 2)
        {
            int d = (((int)(((char *)[text bytes])[index]) & 0x0ff) << 16)
            | (((int)(((char *)[text bytes])[index + 1]) & 255) << 8);
            characters[charCount++] = encodingTable[(d >> 18) & 63];
            characters[charCount++] = encodingTable[(d >> 12) & 63];
            characters[charCount++] = encodingTable[(d >> 6) & 63];
            characters[charCount++] = '=';
        }
        else if(index == text.length - 1)
        {
            int d = ((int)(((char *)[text bytes])[index]) & 0x0ff) << 16;
            characters[charCount++] = encodingTable[(d >> 18) & 63];
            characters[charCount++] = encodingTable[(d >> 12) & 63];
            characters[charCount++] = '=';
            characters[charCount++] = '=';
        }
        NSString * rtnStr = [[NSString alloc] initWithBytesNoCopy:characters length:charCount encoding:NSUTF8StringEncoding freeWhenDone:YES];
        return rtnStr;
    }
  • 相关阅读:
    网站性能优化(1)
    网站性能优化(2)
    mac终端使用
    CSS尺寸 ScrollTop、ScrollHeight、ClientHeight、OffsetHeight总结(四)
    Canvas 与 SVG 的比较
    tsconfig.json配置详解
    Rxjs基本原理
    HTML总结
    盒模型之BFC与文档流对比(二)
    JS深拷贝与浅拷贝
  • 原文地址:https://www.cnblogs.com/qingjoin/p/3347585.html
Copyright © 2020-2023  润新知