Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AES128在Objective-C中->;测试用例失败_Objective C_Aes - Fatal编程技术网

AES128在Objective-C中->;测试用例失败

AES128在Objective-C中->;测试用例失败,objective-c,aes,Objective C,Aes,我尝试使用objective-c运行AES128加密。但是,我没有得到正确的输出。 这也可能是一个愚蠢的objective-c错误,因为我刚刚开始使用这种语言;-) 我的测试用例如下所示: CryptoUtils* crypt = [[CryptoUtils alloc] init]; NSData* plaintext = [@"6bc1bee22e409f96e93d7e117393172a" dataUsingEncoding:NSUTF8StringEncoding]; NSDat

我尝试使用objective-c运行AES128加密。但是,我没有得到正确的输出。 这也可能是一个愚蠢的objective-c错误,因为我刚刚开始使用这种语言;-)

我的测试用例如下所示:

CryptoUtils* crypt = [[CryptoUtils alloc] init];

NSData* plaintext   = [@"6bc1bee22e409f96e93d7e117393172a" dataUsingEncoding:NSUTF8StringEncoding];
NSData* key         = [@"2b7e151628aed2a6abf7158809cf4f3c" dataUsingEncoding:NSUTF8StringEncoding];
NSData* iv          = [@"000102030405060708090A0B0C0D0E0F" dataUsingEncoding:NSUTF8StringEncoding];

NSString* encrypted = [crypt encryptData:plaintext withKey:key andIV:iv];

XCTAssertEqualObjects(@"7649abac8119b246cee98e9b12e9197d", encrypted , @"AES testcase1 not equal");
(NSString *)encryptData:(NSData*)clearText withKey:(NSData*) currentKey andIV:(NSData*) currentIV{
// Buffer for Ciphertext
NSMutableData *cipherData = [NSMutableData dataWithLength:clearText.length + kCCBlockSizeAES128];

size_t cipherLength;

CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
                                          kCCAlgorithmAES128,
                                          kCCOptionPKCS7Padding,
                                          currentKey.bytes,
                                          currentKey.length,
                                          currentIV.bytes,
                                          clearText.bytes,
                                          clearText.length,
                                          cipherData.mutableBytes,
                                          cipherData.length,
                                          &cipherLength);

if(cryptStatus){
   NSLog(@"Something terrible during encryption happened!");
} else {
   NSLog(@"Ciphertext length: %i", [cipherData length]);     
   NSString *output=[cipherData description];
   output = [output stringByReplacingOccurrencesOfString:@" " withString:@""];
   output = [output stringByReplacingOccurrencesOfString:@"<" withString:@""];
   output = [output stringByReplacingOccurrencesOfString:@">" withString:@""];
   return output;
}  
   return nil;
}
encryptData方法如下所示:

CryptoUtils* crypt = [[CryptoUtils alloc] init];

NSData* plaintext   = [@"6bc1bee22e409f96e93d7e117393172a" dataUsingEncoding:NSUTF8StringEncoding];
NSData* key         = [@"2b7e151628aed2a6abf7158809cf4f3c" dataUsingEncoding:NSUTF8StringEncoding];
NSData* iv          = [@"000102030405060708090A0B0C0D0E0F" dataUsingEncoding:NSUTF8StringEncoding];

NSString* encrypted = [crypt encryptData:plaintext withKey:key andIV:iv];

XCTAssertEqualObjects(@"7649abac8119b246cee98e9b12e9197d", encrypted , @"AES testcase1 not equal");
(NSString *)encryptData:(NSData*)clearText withKey:(NSData*) currentKey andIV:(NSData*) currentIV{
// Buffer for Ciphertext
NSMutableData *cipherData = [NSMutableData dataWithLength:clearText.length + kCCBlockSizeAES128];

size_t cipherLength;

CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
                                          kCCAlgorithmAES128,
                                          kCCOptionPKCS7Padding,
                                          currentKey.bytes,
                                          currentKey.length,
                                          currentIV.bytes,
                                          clearText.bytes,
                                          clearText.length,
                                          cipherData.mutableBytes,
                                          cipherData.length,
                                          &cipherLength);

if(cryptStatus){
   NSLog(@"Something terrible during encryption happened!");
} else {
   NSLog(@"Ciphertext length: %i", [cipherData length]);     
   NSString *output=[cipherData description];
   output = [output stringByReplacingOccurrencesOfString:@" " withString:@""];
   output = [output stringByReplacingOccurrencesOfString:@"<" withString:@""];
   output = [output stringByReplacingOccurrencesOfString:@">" withString:@""];
   return output;
}  
   return nil;
}
(NSString*)加密数据:(NSData*)带密钥的明文:(NSData*)当前密钥和iv:(NSData*)当前iv{
//密文缓冲区
NSMutableData*cipherData=[NSMutableData数据带长度:clearText.length+KCCBLOCKSIZEEAES128];
大小/密码长度;
CCCryptorStatus cryptStatus=CCCrypt(kCCEncrypt,
KCcalGorithmae128,
KCCOPIONPKCS7填充,
currentKey.bytes,
当前键长度,
currentIV.bytes,
clearText.bytes,
clearText.length,
cipherData.mutableBytes,
cipherData.length,
&密码长度);
如果(加密状态){
NSLog(@“加密过程中发生了可怕的事情!”);
}否则{
NSLog(@“密文长度:%i”,[cipherData长度]);
NSString*输出=[cipherData描述];
output=[output STRINGBYREPLACINGOCURNCESOFSTRING:@“with字符串:@]”;
output=[output STRINGBYREPLACINGOCURNCESOFSTRING:@“with字符串:@]”;
返回输出;
}  
返回零;
}
现在,我得到了一个错误的“加密”字符串。特别是,它太长了,我怀疑问题在于我传递给该方法的NSData。有人知道我做错了什么吗


谢谢

您在标题中的意思是AES128,但请参阅第一段中的AES256

您似乎正在使用不打算使用的输入数据、密钥和IV值

  • 看起来您希望输入数据长度为128位(与块大小一致),但实际上是376位。这在技术上是可以接受的,因为您使用了填充,但上下文线索表明这是一个疏忽
  • 对于AES128,输入密钥必须为128位长,但密钥为256位长。这对于AES128是不正确的
  • 看起来你希望你的IV是128位长,但实际上它是256位长。这对于AES是不正确的-IV必须具有与块大小相同的长度,即128位
  • 现在,您想做的可能是:

    char bytes[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
    NSData *clearText = [NSData dataWithBytes:&bytes length:16];
    
    char keyBytes[] = {0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c};
    NSData* currentKey = [NSData dataWithBytes:&keyBytes length:16];
    
    char ivBytes[16] = char ivBytes[16] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F};
    NSData* currentIV = [NSData dataWithBytes:&ivBytes length:16];
    
    这些更改将为您提供128位长度的输入、键和IV值

    进行这些更改后,输出将为:


    前32个字符与您提供的字符匹配。这表明您的参考值不完整,代码正常工作。

    输出长度是否大于输入长度加上块大小?检查
    cryptStatus
    是否为
    kCCSuccess
    ,以及您要处理的所有其他错误代码。