向iOS应用程序添加64位支持后加密被破坏
我在我的应用程序中使用以下代码进行加密。我刚刚更改了我的应用程序,以获得苹果所需的64位支持。由于启用64位,加密不再有效。我无法判断加密或解密是否有问题,但当对某些数据进行加密然后解密时,我只会以垃圾告终向iOS应用程序添加64位支持后加密被破坏,ios,encryption,64-bit,Ios,Encryption,64 Bit,我在我的应用程序中使用以下代码进行加密。我刚刚更改了我的应用程序,以获得苹果所需的64位支持。由于启用64位,加密不再有效。我无法判断加密或解密是否有问题,但当对某些数据进行加密然后解密时,我只会以垃圾告终 #import "AES256.h" #import <CommonCrypto/CommonCryptor.h> @implementation NSData (AES256) - (NSData*)AES256EncryptWithKey:(NSString*)key {
#import "AES256.h"
#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];
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];
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
#导入“AES256.h”
#进口
@实现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加密];
}
释放(缓冲区);//释放缓冲区;
返回零;
}
@结束
更新:
我发现我的钥匙有问题,这可能是部分原因。部分密钥创建使用哈希字符串。与64位iPhone相比,32位iPhone上的哈希函数会产生不同的摘要值。我不知道为什么会这样。keydata长度永远不会超过32位整数。如果我硬编码一个字符串返回这里,加密/解密仍然不起作用,所以这仍然是一个问题
+ (NSString *)hashString:(NSString *)string
{
const char *s = [string cStringUsingEncoding:NSASCIIStringEncoding];
NSData *keyData = [NSData dataWithBytes:s length:strlen(s)];
// This is the destination
uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};
// This one function does an unkeyed SHA1 hash of your hash data
CC_SHA1(keyData.bytes, (CC_LONG)keyData.length, digest);
// Now convert to NSData structure to make it usable again
NSData *convertedData = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
// description converts to hex but puts <> around it and spaces every 4 bytes
NSString *hash = [convertedData description];
hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];
return hash;
}
+(NSString*)hashString:(NSString*)string
{
const char*s=[string cStringUsingEncoding:NSASCIIStringEncoding];
NSData*keyData=[NSData dataWithBytes:s长度:strlen(s)];
//这就是目的地
uint8_t digest[CC_SHA1_digest_LENGTH]={0};
//这一个函数对散列数据执行一个未检查的SHA1散列
CC_SHA1(keyData.bytes,(CC_LONG)keyData.length,摘要);
//现在转换为NSData结构,使其再次可用
NSData*convertedData=[NSData dataWithBytes:摘要长度:CC_SHA1_摘要长度];
//description转换为十六进制,但每4个字节在其周围加上空格
NSString*哈希=[convertedData description];
hash=[hash stringByReplacingOccurrencesOfString:@“with string:@]”;
hash=[hash stringByReplacingOccurrencesOfString:@“with string:@]”;
返回散列;
}
更新2:我注意到只有在使用小于16字节的数据时才会出现问题。16位及更高版本。检查所有32位和64位加密方法的输入:密钥、IV、明文。确保它们是相同的。追踪任何差异并修复它们。Crypto旨在为小的输入差异提供大的输出差异 在某些情况下,系统更改可能会在其中一个输入中引入意外更改 你说整个输出都是垃圾;在这种情况下,请先看钥匙。有故障的IV只会损坏AES中的前16个字节。我很惊讶,如果输出损坏,您不会得到错误的填充错误。使用这个
#import "NSString+MD5Addition.h"
#import <CommonCrypto/CommonDigest.h>
@implementation NSString(MD5Addition)
- (NSString *) stringFromMD5{
if(self == nil || [self length] == 0)
return nil;
const char *value = [self UTF8String];
unsigned char outputBuffer[CC_MD5_BLOCK_BYTES];
CC_MD5(value, strlen(value), outputBuffer);
NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_BLOCK_BYTES * 2];
for(NSInteger count = 0; count < CC_MD5_BLOCK_BYTES; count++){
[outputString appendFormat:@"%02x",outputBuffer[count]];
}
return outputString;
}
#导入“NSString+MD5Addition.h”
#进口
@实现NSString(MD5Addition)
-(NSString*)stringFromMD5{
if(self==nil | |[self length]==0)
返回零;
常量字符*值=[self UTF8String];
无符号字符输出缓冲区[CC_MD5_块_字节];
CC_MD5(值、strlen(值)、outputBuffer);
NSMutableString*outputString=[[NSMutableString alloc]initWithCapacity:CC_MD5_BLOCK_BYTES*2];
对于(NSInteger count=0;count
在32位设备上运行时,它还能工作吗?如果是这样,请在这两种类型的设备(32位和64位)上进行调试,并查看在什么情况下会得到不同的结果/值。键是否正好是32