我正在使用rc4算法来加密和解密视频。我正在使用64字节并加密。 同样的方式我也在做解密,但我的视频没有播放。任何人都有这个想法???
- (void)encryptOnFly:(UInt8*)buffer bytesRead:(NSInteger)bytesRead
{
for(long long i=0;i<bytesRead;i++)
{
[self push:buffer[i]];
if([self isFull])
{
unsigned char *buf11=[self retrieveArray];
unsigned char buf1[64];
for(int j=0;j<64;j++)
{
buf1[j]=*buf11++;
}
NSData *data = [NSData dataWithBytes:buf1 length:sizeof(buf1)];
aesCrypt = [data RC4EncryptDecryptWithKey:@"ABCD" operation:kCCEncrypt];
// aesCrypt=[data EncryptAES:@"akey"];
const uint8_t *bytes = (const uint8_t*)[aesCrypt bytes];
for(int j=0;j<63;j++)
{
buf1[j]=*bytes++;
}
[[self fileDownloadOutputStream] write:buf1 maxLength:64];
[self empty];
}
}
}
这是处理可被64整除的文件的代码。
switch (type) {
case kCFStreamEventHasBytesAvailable:
[self handleBytesAvailable];
break;
case kCFStreamEventEndEncountered:
NSLog(@"REACHED :: -- > kCFStreamEventEndEncountered");
if (isEncryptionRequired)
{
if(pointer>-1)
{
remBytes=pointer+1;
int tmp=64-remBytes;
for(int k=0;k<tmp;k++)
{
if (pointer!=62)
{
if(![self isFull])
[self push:0];
}
else
if(![self isFull])
[self push:remBytes];
if ([self isFull])
{
unsigned char *buf11= [self retrieveArray];
unsigned char buf1[64];
for(int j=0;j<64;j++)
{
buf1[j]=*buf11++;
}
NSData *data = [NSData dataWithBytes:buf1 length:sizeof(buf1)];
aesCrypt = [data RC4EncryptDecryptWithKey:@"ABCD" operation:kCCEncrypt];
// aesCrypt=[data EncryptAES:@"akey"];
const uint8_t *bytes = (const uint8_t*)[aesCrypt bytes];
for(int j=0;j<63;j++)
{
buf1[j]=*bytes++;
}
[[self fileDownloadOutputStream] write:buf1 maxLength:16];
[self empty];
}}
}
else
{
unsigned char extrabuf=0;
[[self fileDownloadOutputStream] write:extrabuf maxLength:1];
///add
}
}
else
{
[[self fileDownloadOutputStream] write:buf maxLength:16];
}
[self handleStreamComplete];
break;
case kCFStreamEventErrorOccurred:
[self handleStreamError];
break;
default:
break;
}
这是解密文件的方法......
- (void)decryptFile:(NSString *)OMfileUrl
{
// NSLog(@"omurl::%@",OMfileUrl);
@try
{
__block NSURL *movieURL;
isOffline = YES;
{
const char *srcfile = [OMfileUrl cStringUsingEncoding:NSUTF8StringEncoding];
source = fopen(srcfile, "rb");
filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"assetforplay.mp4"];
AppDelegate *delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
delegate.filePath = filePath;
filePathCharArr = [filePath cStringUsingEncoding:NSUTF8StringEncoding];
remove([filePath cStringUsingEncoding:NSUTF8StringEncoding]);
if (source)
{
destination = fopen(filePathCharArr, "wb");
cancelDecryption = NO;
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{
unsigned char temp[64];
long long totalBytes=0;
fseek(source, 0L, SEEK_END);
long long sz = ftell(source);
fseek(source, 0L, SEEK_SET);
int percentage=0,prevPercentage=0;
int xx=sz%64;
percentageLabel.hidden = NO;
if (xx==0)
{
NSData *data;
while (totalBytes<=(sz-64))
{
if(!cancelDecryption)
{
percentage =((double)totalBytes/(double)sz)*100;
if (percentage!=prevPercentage)
{
prevPercentage=percentage;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[percentageLabel setText:[NSString stringWithFormat:@"%i%%",percentage]];
}];
}
fread(buffer,1, 64, source);
// memcpy( buf, buffer, 16);
// memcpy( keyoffline, [delegate secretKey], 16 + 0 * 8);
// aes_set_key( &ctx, keyoffline, 128 + 0 * 64);
// aes_decrypt( &ctx, buf, buf );
data = [NSData dataWithBytes:buffer length:64];
aesCrypt = [data RC4EncryptDecryptWithKey:@"ABCD" operation:kCCDecrypt];
// aesCrypt=[data DecryptAES:@"akey"];
const uint8_t *bytes = (const uint8_t*)[aesCrypt bytes];
for(int j=0;j<63;j++)
{
buf[j]=*bytes++;
}
fwrite(buf, 1, 64, destination);
totalBytes=totalBytes+64;
}
else
{
percentageLabel.text=@"";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *err;
if (![fileManager removeItemAtPath:filePath error:&err])
{
NSLog(@"Unable to delete the essence. Reason:%@",[err description]);
}
break;
}
}
fseek(source, -64, SEEK_END);
sz = ftell(source);
// fread(buffer, 1, 16, source);
// memcpy( buf, buffer, 16);
// memcpy( keyoffline, [delegate secretKey], 16 + 0 * 8);
// aes_set_key( &ctx, keyoffline, 128 + 0 * 64);
// aes_decrypt( &ctx, buf, buf );
data = [NSData dataWithBytes:buffer length:64];
aesCrypt = [data RC4EncryptDecryptWithKey:@"ABCD" operation:kCCDecrypt];
// aesCrypt=[data DecryptAES:@"akey"];
const uint8_t *bytes = (const uint8_t*)[aesCrypt bytes];
for(int j=0;j<63;j++)
{
buf[j]=*bytes++;
}
int buf1 = buf[63];
for(int j=0;j<buf1;j++)
{
temp[j] = buf[j];
}
fwrite(temp, 1, buf1, destination);
}
else
{
NSData *data;
while (totalBytes<=(sz-1))
{
if(!cancelDecryption)
{
percentage =((double)totalBytes/(double)sz)*100;
if (percentage!=prevPercentage)
{
prevPercentage=percentage;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[percentageLabel setText:[NSString stringWithFormat:@"%i%%",percentage]];
}];
}
fread(buffer,1, 64, source);
// memcpy( buf, buffer, 16);
// memcpy( keyoffline, [delegate secretKey], 16 + 0 * 8);
// aes_set_key( &ctx, keyoffline, 128 + 0 * 64);
// aes_decrypt( &ctx, buf, buf );
data = [NSData dataWithBytes:buffer length:64];
aesCrypt = [data RC4EncryptDecryptWithKey:@"ABCD" operation:kCCDecrypt];
// aesCrypt=[data DecryptAES:@"akey"];
const uint8_t *bytes = (const uint8_t*)[aesCrypt bytes];
for(int j=0;j<63;j++)
{
buf[j]=*bytes++;
}
fwrite(buf, 1, 64, destination);
totalBytes=totalBytes+64;
}
else
{
percentageLabel.text=@"";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *err;
if (![fileManager removeItemAtPath:filePath error:&err])
{
NSLog(@"Unable to delete the essence. Reason:%@",[err description]);
}
break;
}
}
}
dispatch_async(dispatch_get_main_queue(), ^{
percentageLabel.text=@"";
if (!cancelDecryption)
{
percentageLabel.hidden = YES;
movieURL = [NSURL fileURLWithPath:filePath];
[self.mediaPanelViewCtrl setURL:movieURL];
}
});
});
}
}
}
@catch (NSException *exception)
{
NSLog(@"Exception in decrypt file::%@",[exception description]);
}
}
这是使用RC4进行加密和解密的方法....
- (NSData*)RC4EncryptDecryptWithKey:(NSString *)key operation:(CCOperation)operation
{
// convert to C string..
int keySize = [key length];
char keyPtr[keySize];
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr
maxLength:sizeof(keyPtr)
encoding:NSUTF8StringEncoding];
// encode/decode
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength;
void *buffer = malloc(bufferSize);
size_t numBytesOut = 0;
CCCryptorStatus cryptStatus = CCCrypt(operation,
kCCAlgorithmRC4,
kCCOptionECBMode,
keyPtr,
kCCKeySizeMinRC4,
NULL,
[self bytes],
dataLength,
buffer,
bufferSize,
&numBytesOut);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer
length:numBytesOut
freeWhenDone:YES];
}
free(buffer);
return nil;
}