美文网首页
iOS 自定义加密算法

iOS 自定义加密算法

作者: 一路上有你Hi | 来源:发表于2020-10-09 15:00 被阅读0次

    开发中除了常见的RSA加密、MD5加密等,可以实现自动以算法配合加密方法使用,代码如下:

    // 自定义加密算法
    + (NSString *)archlyEncryptionAlgorithmWithData:(NSString *)data key:(NSString *)key isDecode:(BOOL)isDecode{
        int len = 128;
        
        if (key == nil || key.length == 0) {
            key = @"";
        }
        
        if (data.length < 1) {
            return @"";
        }
        
        NSString *md5Key = [key md5Hash];
        NSString *keyA = [[md5Key substringToIndex:16] md5Hash];
        NSString *keyB = [[md5Key substringFromIndex:16] md5Hash];
        NSString *keyC;
        if (isDecode == YES) { // 解密
            keyC = [data substringToIndex:4];
        }else{ // 加密
            NSString *nowTime = [self getNowTimeTimestampms];
            NSString *md5Time = [nowTime md5Hash];
            keyC = [md5Time substringFromIndex:md5Time.length - 4];
        }
        NSString *cryptkey = [NSString stringWithFormat:@"%@%@",keyA,[[NSString stringWithFormat:@"%@%@",keyA,keyC] md5Hash]];
        NSInteger cryptkeyLength = cryptkey.length;
        NSString *newData;
        if (isDecode == YES) { // 解密
            NSString *preparStr = [data substringFromIndex:4];
            if (preparStr.length % 4 != 0) {
                NSInteger coverLength = 4 - preparStr.length % 4;
                        for (int i = 0; i < coverLength ; i ++) {
                            preparStr = [preparStr stringByAppendingString:@"="];
                        }
            }
            newData = [preparStr base64Dencode];
            
        }else{ // 加密
            NSString *sub = [[[NSString stringWithFormat:@"%@%@",data,keyB] md5Hash] substringToIndex:16];
            newData = [NSString stringWithFormat:@"0000000000%@%@",sub,data];
        }
        NSString *result = @"";
        NSMutableArray *box = [NSMutableArray array];
        for (int i = 0; i < len; i++) {
            [box addObject:@(i)];
        }
        
        NSMutableArray *rndkArray = [NSMutableArray array];
        for (int i = 0; i < len; i ++) {
             unichar c =  [cryptkey characterAtIndex:i % cryptkeyLength];
            [rndkArray addObject:@([self ascIIToNumber:[NSString stringWithFormat:@"%C",c]])];
        }
        int j1 = 0;
        for (int i = 0; i < len; i ++) {
            int boxNum = [box[i] intValue];
            int rndkArrayNum = [rndkArray[i] intValue];
            j1 = (j1 + boxNum +rndkArrayNum) % len;
            [box exchangeObjectAtIndex:i withObjectAtIndex:j1];
        }
        
        int a2 = 0;
        int j2 = 0;
        for (int i = 0; i < newData.length; i ++) {
            a2 = (a2 +1) % len;
            int boxNum = [box[a2] intValue];
            j2 = (j2 + boxNum) % len;
            [box exchangeObjectAtIndex:a2 withObjectAtIndex:j2];
            unichar c =  [newData characterAtIndex:i];
            int ASCIIC = [self ascIIToNumber:[NSString stringWithFormat:@"%C",c]];
            
            int value = ([box[a2] intValue] + [box[j2] intValue]) % len;
            int boxValue = [box[value] intValue];
            
            int newValue = ASCIIC ^ boxValue;
            NSString *charStr = [self numberToASCII:newValue];
            result = [NSString stringWithFormat:@"%@%@",result,charStr];
        }
        
        if (isDecode == YES) { // 解密
            if (result.length < 1) {
                NSLog(@"result 为空");
                return @"";
            }
            BOOL bool1 = [[result substringToIndex:10] isEqualToString:@"0000000000"];
            BOOL bool2 = [[result substringToIndex:10] intValue] > [[self getNowTimeTimestamp] intValue];
            
            NSString *string1 = [result substringWithRange:NSMakeRange(10, 16)];
            NSString *md5Str = [[NSString stringWithFormat:@"%@%@",[result substringFromIndex:26],keyB] md5Hash];
            NSString *string2 = [md5Str substringToIndex:16];
            BOOL bool3 = [string2 isEqualToString:string1];
            if ((bool1 || bool2) && bool3) {
                return [result substringFromIndex:26];
            }else {
                return @"";
            }
            
        }else{ // 加密
            NSString *baseStr = [[result base64Encode] stringByReplacingOccurrencesOfString:@"=" withString:@""];
            return [NSString stringWithFormat:@"%@%@",keyC,baseStr];
        }
        
    }
    

    调用的辅助方法如下:
    实现NSStirng 分类

    - (NSString *)md5Hash {
        const char *plain = self.UTF8String;
        unsigned char *digest;
        digest = malloc(CC_MD5_DIGEST_LENGTH);
        
        CC_MD5(plain, (CC_LONG)strlen(plain), digest);
        
        NSString *encode = [self stringFromBytes:digest length:CC_MD5_DIGEST_LENGTH];
        free(digest);
        return encode;
    }
    
    #pragma mark - Bytes转字符串
    /**
     字符大小写可以通过修改“%02X”中的x修改,下面采用的是大写X  小写用 x
     */
    - (NSString *)stringFromBytes:(uint8_t *)bytes length:(int)length {
        NSMutableString *strM = [NSMutableString string];
        
        for (int i = 0; i < length; i++) {
            [strM appendFormat:@"%02x", bytes[i]];
        }
        
        return [strM copy];
    }
    
    // base64编码
    - (NSString *)base64Encode{
        NSData * body = [self dataUsingEncoding:NSUTF8StringEncoding];
        
        return [body base64EncodedStringWithOptions:0];
    }
    
    // base64解码
    - (NSString *)base64Dencode
    {
        
        NSData *decodeData = [[NSData alloc] initWithBase64EncodedString:self options:0]; //解码
            
        NSString *decodeString = [[NSString alloc] initWithData:decodeData encoding:NSUTF8StringEncoding];
        
        return decodeString;
    }
    

    实例方法如下:

    // 获取当前时间戳  毫秒
    - (NSString *)getNowTimeTimestampms{
    
        NSDate *datenow = [NSDate date];
    
        NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)([datenow timeIntervalSince1970]*1000)];
        
        return timeSp;
    }
    
    // 获取当前时间戳  秒
    - (NSString *)getNowTimeTimestamp{
    
        NSDate *datenow = [NSDate date];
    
        NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
    
        return timeSp;
    
    }
    
    - (int)ascIIToNumber:(NSString *)c{
        return [c characterAtIndex:0];
    }
    
    - (NSString *)numberToASCII:(int) number{
        return [NSString stringWithFormat:@"%C",(unichar)number];
    }
    

    同一套算法实现加密解密,data为需要加密或解密字符串,key为盐值(可以为空),isDecode控制加密或解密

    相关文章

      网友评论

          本文标题:iOS 自定义加密算法

          本文链接:https://www.haomeiwen.com/subject/rkulpktx.html