zoukankan      html  css  js  c++  java
  • 使用iOS进行Rc4加密解密

    搜了一次,网络上绝大部分,都转的是下面的这个封装的:

    http://www.cocoachina.com/bbs/read.php?tid-77608.html

    首先非常感谢楼主的分享,但是使用后发现这个里面的代码实际上是有问题的:

    为了帮后人摆脱此坑的困扰,本人稍微做了一下修正,具体如下:

    +(NSString*)HloveyRC4:(NSString*)aInput key:(NSString*)aKey
    {

        NSMutableArray *iS = [[NSMutableArray alloc] initWithCapacity:256];
        NSMutableArray *iK = [[NSMutableArray alloc] initWithCapacity:256];

        for (int i= 0; i<256; i++) {
            [iS addObject:[NSNumber numberWithInt:i]];
        }

        int j=1;
      //改动1,s-box的长度应当是256,楼主之前写的是255
        for (short i=0; i<256; i++) {

            UniChar c = [aKey characterAtIndex:i%aKey.length];

            [iK addObject:[NSNumber numberWithChar:c]];
        }

        j=0;

        for (int i=0; i<256; i++) {
            int is = [[iS objectAtIndex:i] intValue];
            UniChar ik = (UniChar)[[iK objectAtIndex:i] charValue];

            j = (j + is + ik)%256;
            NSNumber *temp = [iS objectAtIndex:i];
            [iS replaceObjectAtIndex:i withObject:[iS objectAtIndex:j]];
            [iS replaceObjectAtIndex:j withObject:temp];

        }

        int i=0;
        j=0;

        

        Byte byteBuffer[aInput.length];



        for (short x=0; x<[aInput length]; x++) {
            i = (i+1)%256;

            int is = [[iS objectAtIndex:i] intValue];
            j = (j+is)%256;

            int is_i = [[iS objectAtIndex:i] intValue];
            int is_j = [[iS objectAtIndex:j] intValue]; 

            int t = (is_i+is_j) % 256;
            int iY = [[iS objectAtIndex:t] intValue];

            //改动2:增加交换is_iis_j的具体内容

            [iS exchangeObjectAtIndex:i withObjectAtIndex:j];


            UniChar ch = (UniChar)[aInput characterAtIndex:x];
            UniChar ch_y = ch^iY;

            

            byteBuffer[x] = ch_y;


        }

        [iS release];
        [iK release];

        return result;
    }

    + (NSString *)byteToBase64String:(Byte [])byteBuffer lengh:(NSInteger)buffLen

    {

        NSData *adata = [[NSData alloc] initWithBytes:byteBuffer length:buffLen];

        NSString *string = [adata base64EncodedStringWithOptions:0];

        return string;

    }

    参考资料:

    http://baike.baidu.com/link?url=GDR7_BqVSKStCioLhfGikGMJ_MBwiGCsuB1bowM3ZRvZ0tNGesVt15AKLPXVjV5qfCaAMxwjolEM9dNy9ca3qa

    百度百科,c语言实现

  • 相关阅读:
    个人号微信机器人开发
    群控系统开发sdk服务端调用方法
    微信个人号scrm客服通信协议定义
    微信crm客服系统使用sdk定制开发(持续更新中!)
    微信客服crm系统接口定义(完善中)
    压测工具-ab
    设计模式之美学习-结构型-享元模式(二十五)
    设计模式之美学习-结构型-组合模式(二十四)
    设计模式之美学习-结构型-门面模式(二十三)
    设计模式之美学习-结构型-适配器模式(二十二)
  • 原文地址:https://www.cnblogs.com/Peterahan/p/4625971.html
Copyright © 2011-2022 走看看