如何在 iPhone/iPad 中获取用于 MD5 算法的密钥
我正在使用 MD5 算法与我想使用 MD5 算法发送数据的服务器同步。 我编写了以下代码 -
NSString *string = @"ABC";
unsigned char *inStrg = (unsigned char*)[[string dataUsingEncoding:NSASCIIStringEncoding] bytes];
unsigned long lngth = [string length];
unsigned char result[MD5_DIGEST_LENGTH];
NSMutableString *outStrg = [NSMutableString string];
MD5(inStrg, lngth, result);
unsigned int i;
for (i = 0; i < MD5_DIGEST_LENGTH; i++)
{
[outStrg appendFormat:@"%02x", result[i]];
}
md5TextField.text = outStrg;
为了在服务器端解密,我需要生成 MD5 文本的密钥。 它的关键是什么? 提前致谢...
I am using MD5 algorithm for sync with server where I want to send data using MD5 algorithm.
I wrote following code-
NSString *string = @"ABC";
unsigned char *inStrg = (unsigned char*)[[string dataUsingEncoding:NSASCIIStringEncoding] bytes];
unsigned long lngth = [string length];
unsigned char result[MD5_DIGEST_LENGTH];
NSMutableString *outStrg = [NSMutableString string];
MD5(inStrg, lngth, result);
unsigned int i;
for (i = 0; i < MD5_DIGEST_LENGTH; i++)
{
[outStrg appendFormat:@"%02x", result[i]];
}
md5TextField.text = outStrg;
For decryption at server end I need its key through which MD5 text has been generated.
What will be its key?
Thanks in advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MD5 是一种不可逆的哈希函数(因此被设计为不可逆)。如果您需要简单的加密,则应该使用 AES 进行对称加密。
查看iPhone 上 NSString 的 AES 加密了解更多信息信息。
MD5 is a hashing function that is not reversible (and thus designed to be non-reversible). If you need a simple encryption, you should go for symmetric encryption using AES.
Chech out AES Encryption for an NSString on the iPhone for more infos.
正如已经提到的,您无法从哈希中获取原始值。您不太可能以原始形式将任何值存储在服务器中。存储哈希并将其与您在应用程序中生成的哈希进行比较以进行身份验证。由于 MD5 与系统无关,因此您不必担心“密钥”,因为您不会将其作为参数传递。如果您将原始值存储在服务器中,您可以计算服务器中字符串的MD5,然后进行比较。
Like already mentioned, you can't get the original value back from the hash. It is unlikely that you would be storing any values in the server in their original form. Store the hash and compare it with the hash that you generate in the app to authenticate. Since MD5 is system independent, you shouldn't worry about a 'key' as you don't pass it as an argument. If in case you are storing the original values in the server, you can calculate the MD5 of the string in the server and then compare.