使用 IV、Salt、RFC2898 迭代的 AES 加密,在 iPhone 中使用 SHA1 算法生成密钥

发布于 2024-12-11 07:52:46 字数 1151 浏览 5 评论 0原文

我有一个与 AES 加密相关的问题。问题是我需要使用 AES 加密技术和初始化向量、Salt、RFC2898 迭代来加密字符串,并使用 sha1 算法生成密钥。

我使用这段代码

+(NSString *)stringToSha1:(NSString *)str{
const char *s = [str 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, keyData.length, digest);

// Now convert to NSData structure to make it usable again
NSData *out = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
// description converts to hex but puts <> around it and spaces every 4 bytes
NSString *hash = [out description];
hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];

NSLog(@"Hash is %@ for string %@", hash, str);

return hash;
}

来生成 sha1 密钥,但它产生的效果与该技术在 .net 和 Android 中的效果完全不同。

Android 和 .net 已经有类和库可以做到这一点,而我则独自一人,所以我如何在 iPhone 中做到这一点。

I have a problem related to AES Encryption. The problem is I need to encrypt the string using AES encryption technique with Intialization Vector, Salt, RFC2898 iteration and Generate a key using sha1 algorithm.

I used this code

+(NSString *)stringToSha1:(NSString *)str{
const char *s = [str 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, keyData.length, digest);

// Now convert to NSData structure to make it usable again
NSData *out = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
// description converts to hex but puts <> around it and spaces every 4 bytes
NSString *hash = [out description];
hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];

NSLog(@"Hash is %@ for string %@", hash, str);

return hash;
}

For sha1 key generation but it produces totally different as this technique do in .net and Android.

Android and .net already have classes and library to do this and i left alone so how I can do it in iPhone.

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

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

发布评论

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

评论(1

つ低調成傷 2024-12-18 07:52:46

这应该是你需要的

+ (NSData *)sha1HashFromString:(NSString *)stringToHash {
    NSData *stringData = [stringToHash dataUsingEncoding:NSASCIIStringEncoding];
    uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};
    CC_SHA1([stringData bytes], [stringData length], digest);
    NSData *hashedData = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
    return [hashedData autorelease];
}

This should be what you need

+ (NSData *)sha1HashFromString:(NSString *)stringToHash {
    NSData *stringData = [stringToHash dataUsingEncoding:NSASCIIStringEncoding];
    uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};
    CC_SHA1([stringData bytes], [stringData length], digest);
    NSData *hashedData = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
    return [hashedData autorelease];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文