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 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#import "XADRC4Handle.h"
@implementation XADRC4Handle
-(id)initWithHandle:(CSHandle *)handle key:(NSData *)keydata
{
if((self=[super initWithName:[handle name] length:[handle fileSize]]))
{
parent=[handle retain];
startoffs=[parent offsetInFile];
key=[keydata retain];
rc4=nil;
}
return self;
}
-(void)dealloc
{
[parent release];
[key release];
[rc4 release];
[super dealloc];
}
-(void)resetStream
{
[rc4 release];
rc4=[(XADRC4Engine *)[XADRC4Engine alloc] initWithKey:key];
}
-(int)streamAtMost:(int)num toBuffer:(void *)buffer
{
int actual=[parent readAtMost:num toBuffer:buffer];
[rc4 encryptBytes:buffer length:actual];
return actual;
}
@end
@implementation XADRC4Engine
+(XADRC4Engine *)engineWithKey:(NSData *)key
{
return [[(XADRC4Engine *)[[self class] alloc] initWithKey:key] autorelease];
}
-(id)initWithKey:(NSData *)key
{
if((self=[super init]))
{
const uint8_t *keybytes=[key bytes];
int keylength=[key length];
for(i=0;i<256;i++) s[i]=i;
j=0;
for(i=0;i<256;i++)
{
j=(j+s[i]+keybytes[i%keylength])&255;
int tmp=s[i]; s[i]=s[j]; s[j]=tmp;
}
i=j=0;
}
return self;
}
-(NSData *)encryptedData:(NSData *)data
{
NSMutableData *res=[NSMutableData dataWithData:data];
[self encryptBytes:[res mutableBytes] length:[res length]];
return [NSData dataWithData:res];
}
-(void)encryptBytes:(unsigned char *)bytes length:(int)length
{
for(int n=0;n<length;n++)
{
i=(i+1)&255;
j=(j+s[i])&255;
int tmp=s[i]; s[i]=s[j]; s[j]=tmp;
bytes[n]^=s[(s[i]+s[j])&255];
}
}
-(void)skipBytes:(int)length
{
for(int n=0;n<length;n++)
{
i=(i+1)&255;
j=(j+s[i])&255;
int tmp=s[i]; s[i]=s[j]; s[j]=tmp;
}
}
@end
|