-1

Hi have to perform a login via web service sending username and password. The problem is that the server wants a strange MD5 format of password and I can't generate it.

Example

NSString password = @".";
NSString md5 = @"5058f1af8388633f609cadb75a75dc9d";

Server MD5 is "PXñ¯ƒˆc?`œ­·ZuÜ".

I need to transform 5058f1af8388633f609cadb75a75dc9d in "PXñ¯ƒˆc?`œ­·ZuÜ", but I have no ideas.

Edit

I've discovered that the result is an hash of 5058f1af8388633f609cadb75a75dc9d string.This is the .NET code on server:

byte[] passwordBytesEnc = UTF8Encoding.Default.GetBytes(".");
byte[] passwordBytesHash = MD5.Create().ComputeHash(passwordBytesEnc);
string passwordCriptata = UTF8Encoding.Default.GetString(passwordBytesHash);
Fry
  • 6,235
  • 8
  • 54
  • 93
  • Trying to store an MD5 hash as a UTF8 string is about the most brain damaged thing anyone could do. Add to that that apparently there is no salt involved whatsoever, which is _criminally_ insecure. I'd ask you to tell us which company this is, so we can avoid them. – gnasher729 May 30 '14 at 14:23
  • And instead of guessing, you could engage your brain, look up which code ñ is, and how it relates to the md5 string. Surely that's a better method than guessing. – gnasher729 May 30 '14 at 14:30

2 Answers2

0

Well, what your Server expects is the String-representation of the MD5-Hex value (for whatever reason...).

You can convert your Hex-Value to a String like so:

NSString *string = @"5058f1af8388633f609cadb75a75dc9d";
NSMutableString * newString = [[NSMutableString alloc] init]; //will contain your result-string
int i = 0;
while (i < [string length])
{
    NSString * hexChar = [string substringWithRange: NSMakeRange(i, 2)];
    int value = 0;
    sscanf([hexChar cStringUsingEncoding:NSASCIIStringEncoding], "%x", &value);
    [newString appendFormat:@"%c", (char)value];
    i+=2;
}

(--> How to convert HEX to NSString in Objective-C?)

Community
  • 1
  • 1
cania
  • 857
  • 10
  • 16
  • with your code I obtain this string PXñ¯c?`­·ZuÜ and not PXñ¯ƒˆc?`œ­·ZuÜ. I've tried to change NSASCIIStringEncoding in NSUTF8StringEncoding but the result is the same. Ideas ? – Fry May 30 '14 at 13:47
-1

try this..

- (NSString *)MD5String:(NSString *)string{
    const char *cstr = [string UTF8String];
    NSLog(@"%s",cstr);
    unsigned char result[16];
    CC_MD5(cstr, strlen(cstr), result);
    NSLog(@"%s",result);

    return [NSString stringWithFormat:
            @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}

and call like this

NSString *md5 = @"5058f1af8388633f609cadb75a75dc9d";
    [self MD5String:md5];
karthikeyan
  • 3,821
  • 3
  • 22
  • 45
  • Better close the question as duplicate of [How do I create an MD5 Hash of a string in Cocoa?](https://stackoverflow.com/questions/2018550/how-do-i-create-an-md5-hash-of-a-string-in-cocoa) if you believe the answer is a regular MD5. (question was asking for a _strange_ MD5). – Cœur Dec 30 '19 at 02:03