【问题标题】:Discrepancy between C# MD5 and Objective C MD5C# MD5 和 Objective C MD5 之间的差异
【发布时间】:2012-04-16 22:23:32
【问题描述】:

我正在尝试通过本机 iPhone 应用程序与 C# 服务进行通信。为了进行通信,输入的密码需要进行哈希处理,并与存储在服务器上的哈希版本进行比较。我正在尝试在 Objective C 中重新创建 C# 哈希,这就是它开始变得有趣的地方

目标 C 代码:

NSString * password = @"testPass123";
const char *cPassword = [password UTF8String];    

NSString * key = @"Garbage12345";
NSData * keyData = [NSData dataFromBase64String:key];
NSUInteger len = [keyData length];
unsigned char * cKey = (unsigned char *)malloc(len);
memcpy(cKey, [keyData bytes], len);

// Concatenate into one byte array
unsigned char totalString[18];
for (int i = 0; i < strlen(cPassword); i++) {
    totalString[i] = cPassword[i];
}

for (int i = 0; i < len; i++) {
    totalString[strlen(cPassword) + i] = cKey[i];
}

// DEBUG: Display byte array
for (int i = 0; i < 18; i++) {
    NSLog(@"totalString: %x", totalString[i]);
}

// **** totalString == plainTextWithSaltBytes from the C# portion ****
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(totalString, CC_MD5_DIGEST_LENGTH, result);

for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
    NSLog(@"result: %02x", result[i]);
}

C#代码:

byte[] SaltBytes = Convert.FromBase64String("Garbage12345");

// Convert plain text into a byte array.
byte[] plainTextBytes = Encoding.UTF8.GetBytes("testPass123");

// Allocate array, which will hold plain text and salt.
byte[] plainTextWithSaltBytes = new byte[plainTextBytes.Length + SaltBytes.Length];

// Copy plain text bytes into resulting array.
for (int i = 0; i < plainTextBytes.Length; i++)
    plainTextWithSaltBytes[i] = plainTextBytes[i];

// Append salt bytes to the resulting array.
for (int i = 0; i < SaltBytes.Length; i++)
    plainTextWithSaltBytes[plainTextBytes.Length + i] = SaltBytes[i];

HashAlgorithm hash = new MD5CryptoServiceProvider();

// **** plainTextWithSaltBytes == totalString from the Obj-C portion ****
// Compute hash value of our plain text with appended salt.
byte[] hashBytes = hash.ComputeHash(plainTextWithSaltBytes);

// Convert result into a base64-encoded string.
string hashValue = Convert.ToBase64String(hashBytes);

在字节数组进入 MD5 部分之前,我得到了相同的结果。使用提供的虚拟数据返回:

74
65
73
74
50
61
73
73
31
32
33
19
aa
db
6a
07
b5
db

但是,之后我得到了不同的值,不知道从那里去哪里。

有人有什么想法吗?随时指出我做错的事情。谢谢。

【问题讨论】:

    标签: c# objective-c web-services md5


    【解决方案1】:

    我怀疑你的错误是CC_MD5 的第二个参数应该是输入而不是输出长度。传入输入长度应该可以解决您的直接问题。

    但我认为,您应该丢弃双方的代码,并使用一些专为密码散列而设计的函数。如 PBKDF2 或 bcrypt。

    您将哈希发送到服务器也很奇怪。通常,您将密码发送到服务器并在那里散列。发送散列实质上改变了密码的定义,允许不知道密码但知道散列的攻击者登录。

    【讨论】:

    • 您能否解释一下如何向服务器发送哈希比发送密码更不安全?攻击者如何知道哈希?即使他们知道哈希值,它如何允许他们登录?
    • 如果攻击者破坏了您的数据库,他将知道所有用户的哈希值,并且可以作为其中任何一个用户登录。如果您发送密码,他将无法在不反转哈希的情况下登录。
    • “即使他们知道哈希值,它如何允许他们登录?”通过像正常登录一样将哈希发送到服务器?我不太明白你的问题。
    • 我会远离 MD5。它只是不安全。如果使用 MD5,许多较新的安全实施协议(如 FIPS)将失败。 SHA1 / 2 更安全。
    • 不幸的是,服务器端不是我可以控制的,这不是我过去与 Web 服务通信/验证的方式。
    猜你喜欢
    • 2011-08-08
    • 2015-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多