使用 CommonCrypto/CommonHMAC 加密一些数据并且总是返回 null

发布于 2024-11-16 14:21:59 字数 916 浏览 8 评论 0原文

我尝试使用密钥 keyData 加密 clearTextData。我确实进行了检查,以确保这两个值都是有效的并且经过验证。

NSData *keyData = [PRIVATE_KEY dataUsingEncoding:NSUTF8StringEncoding];
NSData *clearTextData = [data dataUsingEncoding:NSUTF8StringEncoding];

uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};

CCHmacContext hmacContext;
CCHmacInit(&hmacContext, kCCHmacAlgSHA1, keyData.bytes, keyData.length);
CCHmacUpdate(&hmacContext, clearTextData.bytes, clearTextData.length);
CCHmacFinal(&hmacContext, digest);

NSData *out = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];

NSLog(@"encrypted data: %@", [NSString stringWithUTF8String:[out bytes]]);

日志总是返回说加密数据:(null)

有什么想法吗?

* 更新 *

以下是我传递的密钥和数据的示例:

密钥:983745hjhgfd3454

数据: {“data”:“lala”,“pubKey”:“75948458”,“sig”:“val”}

I tried the following to encrypt the clearTextData using the key keyData. And I did check to make sure that both of those values are valid and going through.

NSData *keyData = [PRIVATE_KEY dataUsingEncoding:NSUTF8StringEncoding];
NSData *clearTextData = [data dataUsingEncoding:NSUTF8StringEncoding];

uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};

CCHmacContext hmacContext;
CCHmacInit(&hmacContext, kCCHmacAlgSHA1, keyData.bytes, keyData.length);
CCHmacUpdate(&hmacContext, clearTextData.bytes, clearTextData.length);
CCHmacFinal(&hmacContext, digest);

NSData *out = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];

NSLog(@"encrypted data: %@", [NSString stringWithUTF8String:[out bytes]]);

The log always comes back saying encrypted data: (null)

Any ideas?

* UPDATE *

Here are examples of the key and data that I am passing:

key: 983745hjhgfd3454

data:
{"data":"lala","pubKey":"75948458","sig":"val"}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海未深 2024-11-23 14:21:59

来自加密的数据是数据,尝试将其转换为字符串在编码上失败。您指定的是 UTF8 编码,我也尝试过 UTF32 编码,但也失败了。只需记录返回的数据,因为这些十六进制值比字符串表示形式更有用。

如果您仍然希望看到尽可能多的字符串,您可以这样做。

NSData *output = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];

    //This is useful
NSLog(@"encrypted data: %@", output);

    //Not useful but you may be able to visualize some of the string
char *outstr = malloc(sizeof(char) * (CC_SHA1_DIGEST_LENGTH + 1));
memcpy(outstr, [output bytes], CC_SHA1_DIGEST_LENGTH);
outstr[CC_SHA1_DIGEST_LENGTH] = 0;
NSLog(@"encrypted data string: %s", outstr);
free(outstr);

我在以下行中也取得了一些成功。(打印与上面不同的字符串)

NSLog(@"encrypted data: %@", [[[NSString alloc] initWithData:output encoding:NSISOLatin2StringEncoding] autorelease]);

This data from the crypto is data and attempting to turn it into string is failing on the encoding. You are specifying UTF8 encoding and I have also tried UTF32 encoding and that fails as well. Just log the data returned as those hex values are more beneficial than a string representation.

If you would still like to see as much as the string as possible you can do this.

NSData *output = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];

    //This is useful
NSLog(@"encrypted data: %@", output);

    //Not useful but you may be able to visualize some of the string
char *outstr = malloc(sizeof(char) * (CC_SHA1_DIGEST_LENGTH + 1));
memcpy(outstr, [output bytes], CC_SHA1_DIGEST_LENGTH);
outstr[CC_SHA1_DIGEST_LENGTH] = 0;
NSLog(@"encrypted data string: %s", outstr);
free(outstr);

And I also had some success with the following line.(Prints a different string than above)

NSLog(@"encrypted data: %@", [[[NSString alloc] initWithData:output encoding:NSISOLatin2StringEncoding] autorelease]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文