无法解密iPhone上的视频文件
我需要解密iPhone上的视频文件 服务器正在使用无法解密iPhone上的视频文件,iphone,video,encryption,Iphone,Video,Encryption,我需要解密iPhone上的视频文件 服务器正在使用Cipher c=Cipher.getInstance(“AES/ECB/NoPadding”) 在iPhone中,我使用以下代码(),但无法成功解密文件 #import <CommonCrypto/CommonCryptor.h> @implementation NSData (AES256) - (NSData *)AES256EncryptWithKey:(NSString *)key { // 'key' shou
Cipher c=Cipher.getInstance(“AES/ECB/NoPadding”)代码>
在iPhone中,我使用以下代码(),但无法成功解密文件
#import <CommonCrypto/CommonCryptor.h>
@implementation NSData (AES256)
- (NSData *)AES256EncryptWithKey:(NSString *)key {
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
// fetch key data
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
//See the doc: For block ciphers, the output size will always be less than or
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL /* initialization vector (optional) */,
[self bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer); //free the buffer;
return nil;
}
- (NSData *)AES256DecryptWithKey:(NSString *)key {
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
// fetch key data
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
//See the doc: For block ciphers, the output size will always be less than or
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL /* initialization vector (optional) */,
[self bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesDecrypted);
if (cryptStatus == kCCSuccess) {
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}
free(buffer); //free the buffer;
return nil;
}
@end
#导入
@实现NSData(AES256)
-(NSData*)AES256EncryptWithKey:(NSString*)密钥{
//AES256的“键”应为32字节,否则将填充为空
char keyPtr[kCCKeySizeAES256+1];//为终止符预留的空间(未使用)
bzero(keyPtr,sizeof(keyPtr));//用零填充(用于填充)
//获取关键数据
[key-getCString:keyPtr-maxLength:sizeof(keyPtr)编码:NSUTF8StringEncoding];
NSU整数数据长度=[自身长度];
//参见文档:对于分组密码,输出大小将始终小于或
//等于输入大小加上一个块的大小。
//这就是为什么我们需要在这里添加一个块的大小
size\u t bufferSize=dataLength+kccblocksizeaaes128;
void*buffer=malloc(bufferSize);
大小\u t numBytesEncrypted=0;
CCCryptorStatus cryptStatus=CCCrypt(kCCEncrypt,kCCAlgorithmAES128,kCCOptionPKCS7Padding,
keyPtr,kCCKeySizeAES256,
NULL/*初始化向量(可选)*/,,
[self bytes],数据长度,/*输入*/
缓冲区,缓冲区大小,/*输出*/
&(未加密);
if(cryptStatus==kCCSuccess){
//返回的NSData拥有缓冲区的所有权,并在释放时释放缓冲区
返回[NSData DATAFTHBYTESNOCOPY:缓冲区长度:numBytesEncrypted];
}
释放(缓冲区);//释放缓冲区;
返回零;
}
-(NSData*)AES256DecryptWithKey:(NSString*)密钥{
//AES256的“键”应为32字节,否则将填充为空
char keyPtr[kCCKeySizeAES256+1];//为终止符预留的空间(未使用)
bzero(keyPtr,sizeof(keyPtr));//用零填充(用于填充)
//获取关键数据
[key-getCString:keyPtr-maxLength:sizeof(keyPtr)编码:NSUTF8StringEncoding];
NSU整数数据长度=[自身长度];
//参见文档:对于分组密码,输出大小将始终小于或
//等于输入大小加上一个块的大小。
//这就是为什么我们需要在这里添加一个块的大小
size\u t bufferSize=dataLength+kccblocksizeaaes128;
void*buffer=malloc(bufferSize);
大小\u t numBytesDecrypted=0;
CCCryptorStatus cryptStatus=CCCrypt(kCCDecrypt,kCCAlgorithmAES128,kCCOptionPKCS7Padding,
keyPtr,kCCKeySizeAES256,
NULL/*初始化向量(可选)*/,,
[self bytes],数据长度,/*输入*/
缓冲区,缓冲区大小,/*输出*/
&不加密);
if(cryptStatus==kCCSuccess){
//返回的NSData拥有缓冲区的所有权,并在释放时释放缓冲区
返回[NSData DATAFTHBYTESNOPY:缓冲区长度:numbytes加密];
}
释放(缓冲区);//释放缓冲区;
返回零;
}
@结束
我找到了解决办法。在下面的行中进行更改对我来说是java中“AES/ECB/NoPadding”模式的一种替代方法
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionECBMode,
keyPtr, kCCKeySizeAES128,
NULL /* initialization vector (optional) */,
[self bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesEncrypted);