相关文章推荐
高大的柳树  ·  sql语句包含多个select - CSDN文库·  6 月前    · 
闯红灯的馒头  ·  初识DBT以及搭建第一个DBT工程_dbt ...·  7 月前    · 
不拘小节的牛腩  ·  curl 空格和转义符 - yjy888 ...·  1 年前    · 
谈吐大方的鸵鸟  ·  「Python遍历json」相关问答|文档| ...·  1 年前    · 
痴情的领结  ·  Python内存泄漏问题 - 简书·  1 年前    · 
Code  ›  iOS RC4加密开发者社区
nsstring rc4
https://cloud.tencent.com/developer/article/1854134
乖乖的玉米
1 年前
作者头像
莫空9081
0 篇文章

iOS RC4加密

原创
前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > iOS 备忘录 > iOS RC4加密

iOS RC4加密

原创
作者头像
莫空9081
修改 于 2021-08-10 15:31:29
684 0
修改 于 2021-08-10 15:31:29
举报

iOS RC4加密

iOS RC4加密的实现,要注意加密后生成字符串的步骤,有些要求是base64加密后输出,有些要求是字节数组转16进制字符串输出,所以要特别注意。

代码如下:

// rc4加密
- (NSString *)rc4Encode:(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;
        for (short i=0; i<256; i++) {
            UniChar c = [aKey characterAtIndex:i%aKey.length];
            [iK addObject:[NSNumber numberWithChar:c]];
        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;
        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;
            // 先交换位置,再取值
            [iS exchangeObjectAtIndex:i withObjectAtIndex:j];
            int iY = [[iS objectAtIndex:t] intValue];
            UniChar ch = (UniChar)[aInput characterAtIndex:x];
            UniChar ch_y = ch^iY;
            byteBuffer[x] = ch_y;
    // 字节数组转16进制字符串输出
    NSString *resultString = [self stringFromByte:byteBuffer length:aInput.length];
//    NSData *adata = [[NSData alloc] initWithBytes:byteBuffer length:aInput.length];
//    NSString *string = [adata base64EncodedStringWithOptions:0]; // 以base64的加密结果输出
    return resultString;
//rc4解密
- (NSString *)rc4Decode:(NSString *)data key:(NSString*)secret{
    // 如果是16进制字符串
    NSData *raw = [self ByteDataFromString:data];
    // 如果是base64加密后字符串
//    NSData *raw = [[NSData alloc] initWithBase64EncodedString:data options:0];
    int cipherLength = (int)raw.length;
    UInt8 *cipher = malloc(cipherLength);
    [raw getBytes:cipher length:cipherLength];
    NSData *kData = [secret dataUsingEncoding:NSUTF8StringEncoding];
    int keyLength = (int)kData.length;
    UInt8 *kBytes = malloc(kData.length);
    [kData getBytes:kBytes length:kData.length];
    UInt8 *decipher = malloc(cipherLength + 1);
    UInt8 iS[256];
    UInt8 iK[256];
    int i;
    for (i = 0; i < 256; i++){
        iS[i] = i;
        iK[i] = kBytes[i % keyLength];
    int j = 0;
    for (i = 0; i < 256; i++){
        int is = iS[i];
        int ik = iK[i];
        j = (j + is + ik)% 256;
        UInt8 temp = iS[i];
        iS[i] = iS[j];
        iS[j] = temp;
    int q = 0;
    int p = 0;
    for (int x = 0; x < cipherLength; x++){
        q = (q + 1)% 256;
        p = (p + iS[q])% 256;
        int k = iS[p];
        iS[p] = iS[q];
        iS[q] = k;
        k = iS[(iS[q] + iS[p])% 256];
        decipher[x] = cipher[x] ^ k;
    free(kBytes);
    decipher[cipherLength] = '\0';
    return @((char *)decipher);
// 字节数组转
- (NSString *)stringFromByte:(Byte *)byteBuffer length:(NSInteger)length {
    NSMutableString *hexString = [[NSMutableString alloc] init];
    for (int i = 0; i < length; i++) {
        [hexString appendString:[NSString stringWithFormat:@"%0.2hhx", byteBuffer[i]]];
    return [hexString uppercaseString];
- (NSData *)ByteDataFromString:(NSString *)targetStr {
    NSInteger len = [targetStr length] / 2;    // Target length
    unsigned char *buf = malloc(len);
    unsigned char *whole_byte = buf;
    char byte_chars[3] = {'\0','\0','\0'};
    int i;
    for (i=0; i < [targetStr length] / 2; i++) {
        byte_chars[0] = [targetStr characterAtIndex:i*2];
        byte_chars[1] = [targetStr characterAtIndex:i*2+1];
        *whole_byte = strtol(byte_chars, NULL, 16);
        whole_byte++;
 
推荐文章
高大的柳树  ·  sql语句包含多个select - CSDN文库
6 月前
闯红灯的馒头  ·  初识DBT以及搭建第一个DBT工程_dbt python-CSDN博客
7 月前
不拘小节的牛腩  ·  curl 空格和转义符 - yjy888 - 博客园
1 年前
谈吐大方的鸵鸟  ·  「Python遍历json」相关问答|文档|产品|活动 - 七牛云
1 年前
痴情的领结  ·  Python内存泄漏问题 - 简书
1 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号