• ios des加密


    des.h

    #import <Foundation/Foundation.h>
    #import <CommonCrypto/CommonCryptor.h>
    
    @interface Des : NSObject
    
    //DES加密
    + (NSString *) encryptStr:(NSString *) str key:(NSString *)sKey;
    //DES解密
    + (NSString *) decryptStr:(NSString *) str key:(NSString *)sKey;

    des.m

    #import "Des.h"
    
    static const char _bas64EncodingTable[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    static const short _base64DecodingTable[256] = {
        -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -2, -1, -1, -2, -2,
        -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
        -1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, -2, -2, 63,
        52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -2, -2, -2,
        -2,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
        15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, -2,
        -2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2,
        -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
        -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
        -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
        -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
        -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
        -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
        -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
        -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2
    };
    
    
    @implementation Des
    + (NSString *) encryptStr:(NSString *) str key:(NSString *)sKey
    {
        return [Des doCipher:str key:sKey context:kCCEncrypt];
    }
    + (NSString *) decryptStr:(NSString *) str key:(NSString *)sKey
    {
        return [Des doCipher:str key:sKey context:kCCDecrypt];
    }
    
    + (NSString *)doCipher:(NSString *)sTextIn key:(NSString *)sKey
                   context:(CCOperation)encryptOrDecrypt {
        NSStringEncoding EnC = NSUTF8StringEncoding;
        
        NSMutableData * dTextIn;
        if (encryptOrDecrypt == kCCDecrypt) {
            dTextIn = [[Des decodeBase64WithString:sTextIn] mutableCopy];
        }
        else{
            dTextIn = [[sTextIn dataUsingEncoding: EnC] mutableCopy];
        }
        NSMutableData * dKey = [[sKey dataUsingEncoding:EnC] mutableCopy];
        [dKey setLength:kCCBlockSizeDES];
        uint8_t *bufferPtr1 = NULL;
        size_t bufferPtrSize1 = 0;
        size_t movedBytes1 = 0;
        
        Byte iv[] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
        bufferPtrSize1 = ([sTextIn length] + kCCKeySizeDES) & ~(kCCKeySizeDES -1);
        bufferPtr1 = malloc(bufferPtrSize1 * sizeof(uint8_t));
        memset((void *)bufferPtr1, 0x00, bufferPtrSize1);
        CCCrypt(encryptOrDecrypt,
                kCCAlgorithmDES,
                kCCOptionPKCS7Padding,
                [dKey bytes],
                [dKey length],
                iv,
                [dTextIn bytes],
                [dTextIn length],
                (void*)bufferPtr1,
                bufferPtrSize1,
                &movedBytes1);
        
        
        NSString * sResult;
        if (encryptOrDecrypt == kCCDecrypt){
            sResult = [[ NSString alloc] initWithData:[NSData dataWithBytes:bufferPtr1
                                                                     length:movedBytes1] encoding:EnC] ;
        }
        else {
            NSData *dResult = [NSData dataWithBytes:bufferPtr1 length:movedBytes1];
            sResult = [Des encodeBase64WithData:dResult];
        }
        return sResult;
    }
    + (NSString *)encodeBase64WithData:(NSData *)objData {
        const unsigned char * objRawData = [objData bytes];
        char * objPointer;
        char * strResult;
        
        int intLength = [objData length];
        if (intLength == 0) return nil;
        
        strResult = (char *)calloc(((intLength + 2) / 3) * 4, sizeof(char));
        objPointer = strResult;
        
        while(intLength > 2) {
            *objPointer++ = _bas64EncodingTable[objRawData[0] >> 2];
            *objPointer++ = _bas64EncodingTable[((objRawData[0] & 0x03) << 4) + (objRawData[1] >> 4)];
            *objPointer++ = _bas64EncodingTable[((objRawData[1] & 0x0f) << 2) + (objRawData[2] >> 6)];
            *objPointer++ = _bas64EncodingTable[objRawData[2] & 0x3f];
            
            objRawData += 3;
            intLength -= 3;
        }
        
        if (intLength != 0) {
            *objPointer++ = _bas64EncodingTable[objRawData[0] >> 2];
            if (intLength > 1) {
                *objPointer++ = _bas64EncodingTable[((objRawData[0] & 0x03) << 4) + (objRawData[1] >> 4)];
                *objPointer++ = _bas64EncodingTable[(objRawData[1] & 0x0f) << 2];
                *objPointer++ = '=';
            } else {
                *objPointer++ = _bas64EncodingTable[(objRawData[0] & 0x03) << 4];
                *objPointer++ = '=';
                *objPointer++ = '=';
            }
        }
        
        *objPointer = '';
        
        return[NSString stringWithCString:strResult encoding:NSASCIIStringEncoding];
    }
    
    
    + (NSData *)decodeBase64WithString:(NSString *)strBase64 {
        const char* objPointer = [strBase64 cStringUsingEncoding:NSASCIIStringEncoding];
        int intLength = strlen(objPointer);
        int intCurrent;
        int i = 0, j = 0, k;
        
        unsigned char * objResult;
        objResult = calloc(intLength, sizeof(char));
        
        while ( ((intCurrent = *objPointer++) != '') && (intLength-- > 0) ) {
            if (intCurrent == '=') {
                if (*objPointer != '=' && ((i % 4) == 1)) {// || (intLength > 0)) {
                    free(objResult);
                    return nil;
                }
                continue;
            }
            
            intCurrent = _base64DecodingTable[intCurrent];
            if (intCurrent == -1) {
                continue;
            } else if (intCurrent == -2) {
                free(objResult);
                return nil;
            }
            
            switch (i % 4) {
                case 0:
                    objResult[j] = intCurrent << 2;
                    break;
                    
                case 1:
                    objResult[j++] |= intCurrent >> 4;
                    objResult[j] = (intCurrent & 0x0f) << 4;
                    break;
                    
                case 2:
                    objResult[j++] |= intCurrent >>2;
                    objResult[j] = (intCurrent & 0x03) << 6;
                    break;
                    
                case 3:
                    objResult[j++] |= intCurrent;
                    break;
            }
            i++;
        }
        
        k = j;
        if (intCurrent == '=') {
            switch (i % 4) {
                case 1:
                    free(objResult);
                    return nil;
                case 2:
                    k++;
                case 3:
                    objResult[k] = 0;
            }
        }
        
        NSData * objData = [[NSData alloc] initWithBytes:objResult length:j] ;
        free(objResult);
        return objData;
    }
  • 相关阅读:
    【原】相煎何太急——input的blur事件与button的click事件
    【原】jQuery与CSS自动生成验证码
    【转】HTML转义字符大全
    【原】来自于一位前端“布道者”的建议
    【原】如何在jQuery中实现闭包
    【转】Web前端研发工程师编程能力飞升之路
    【原】git如何删除本地和远程的仓库
    H5不同样式新闻垂直滚动效果
    mui防止软键盘弹起方法
    js显示对象所有属性和方法的函数
  • 原文地址:https://www.cnblogs.com/Percy/p/4947365.html
Copyright © 2020-2023  润新知