zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    各项硬件使用剖析(一)---让你一眼就能区分瓶颈是Memory、processor ORdisk!
    基本算术运算符
    网页计算器 && 简易网页时钟 && 倒计时时钟
    页面加载后累加,自加1&&判断数字是否为两位数
    累加按钮,自加1&&输入两个数字,比较大小
    用typeof查看数据类型&&用parseInt解析数字,并求和
    鼠标移过,改变图片路径
    单一按钮显示/隐藏&&提示框效果
    简易选项卡&&简易JS年历
    函数传参,改变Div任意属性的值&&图片列表:鼠标移入/移出改变图片透明度
  • 原文地址:https://www.cnblogs.com/qingjoin/p/3347585.html
Copyright © 2011-2022 走看看