iOS,objectC,RC4加密解密方法

// 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]];
        }
        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];
            [iS exchangeObjectAtIndex:i withObjectAtIndex:j];
            UniChar ch = (UniChar)[aInput characterAtIndex:x];
            UniChar ch_y = ch^iY;
            byteBuffer[x] = ch_y;
        }
    NSData *adata = [[NSData alloc] initWithBytes:byteBuffer length:aInput.length];
    NSString *string = [adata base64EncodedStringWithOptions:0];
    // result = base64(rc4(string)),以base64的加密结果输出
    // 若不需要base64,则直接return byteBuffer;
    // 一般情况下都会以base64输出
    return string;
}
//rc4解密
- (NSString *)rc4Decode:(NSString *)data key:(NSString*)secret{ 
    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 *aKey = @"88888888";
NSString *beEncodeString = @"66666666";
//加密
NSString *encodeResult = [self rc4Encode:beEncodeString key:aKey];
//解密
NSString *decodeResult = [self rc4Decode:bencodeResult key:aKey];

结束!有帮助的话,麻烦点个赞,笔芯!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大炮走火

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值