md5-sum how ?

Hi guys,

Can anyone show me how to implement the following procedure:

Take the md5-sum of the string.

Take the first 6 bytes of the resulting 16-byte-long MD5 value.

Every byte modulo 26 encodes a character from the range "a" to "z".

This string of 6 small letters is the password.

I just know how to start here:

MessageDigest md = MessageDigest.getInstance("MD5");

Thanks !

regards,

kmthien

[462 byte] By [kmthiena] at [2007-9-23]
# 1

>

> I just know how to start here:

> MessageDigest md = MessageDigest.getInstance("MD5");

Have you read the Javadoc for MessageDigest? It spells out in detail most of how to create the digest. Then all you have to do is to work out how to create a byte array from your string (String.getBytes(encoding)) and convert the result.

10 minues work if you know Java.

sabre150a at 2007-7-8 > top of java,Security,Cryptography...
# 2
so how?
kmthiena at 2007-7-8 > top of java,Security,Cryptography...
# 3
> so how?sighmakes you wonder why you even bother to reply, doesn't it ?
silk.ma at 2007-7-8 > top of java,Security,Cryptography...
# 4

> so how?

Seems to me that if you use the example given in MessageDigest, all you have to do is

Convert the string to bytes.

Take the md5-sum of the string.

Take the first 6 bytes of the resulting 16-byte-long MD5 value.

Every byte modulo 26 encodes a character from the range "a" to "z".

This string of 6 small letters is the password.

I hope this helps.

sabre150a at 2007-7-8 > top of java,Security,Cryptography...
# 5
Hi sabre150 ,I just need some clue(source code) in the following:1. take md5-sum2. every byte modulo 26 encodes a character from the range "a" to "z".Appreciate if you can assist here. Thanks !regards,kmthien
kmthiena at 2007-7-8 > top of java,Security,Cryptography...
# 6

> 1. take md5-sum

As I keep saying, check out the MessageDigest class Javadoc because it gives you an example. What part of this statement don't you understand? I am not going to 'copy and paste' the code given in the Javadoc!

> 2. every byte modulo 26 encodes a character from the

> range "a" to "z".

char encodedByte = (char) ((messageDigestByteValue & 0xff) % 26 + 'a');

The total code will be no more than about 8 lines and is almost trivial. If you find this difficult then learn to walk withJava before you start trying to run with it.

sabre150a at 2007-7-8 > top of java,Security,Cryptography...
# 7

Hi kmthien,

Use this to do md5:

public static byte[] md5(String source) {

try {

MessageDigest md = MessageDigest.getInstance("MD5");

byte[] bytes = md.digest(source.getBytes());

return bytes;

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

and modify this to encode char:

private static String getString(byte[] bytes) {

StringBuffer sb = new StringBuffer();

for (int i = 0; i < bytes.length; i++) {

byte b = bytes[i];

sb.append((int) (0x00FF & b));

if (i + 1 < bytes.length) {

sb.append("-");

}

}

return sb.toString();

}

You'll just need to modify this getString method to encode the chars every byte modulo 16.

Hope this is what you needed.

Regards,

Clive

Clive_Ba at 2007-7-8 > top of java,Security,Cryptography...
# 8

Hi,

the javascript is like below:

password_letters = new String ("abcdefghijklmnopqrstuvwxyz");

password_length = 6;

txtcaptchacode = document.getElementById("registrationForm:txtcaptchacode").value;

md5 = new String (str_md5 ("secret" + randomCode));

var password = "";

for(var i = 0; i < password_length; i++)

{

index = md5.charCodeAt (i) % password_letters.length;

password += password_letters.charAt (index);

}

so how to write in Java ?

kmthiena at 2007-7-8 > top of java,Security,Cryptography...
# 9

Hi,

I tried the following Java code:

char[] chars = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

String signstring = "secret" + "Dvu5CPVC9g";

MessageDigest md = MessageDigest.getInstance("MD5");

md.update(signstring.getBytes());

String hexCode = asHex (md.digest());

StringBuffer sb = new StringBuffer();

int index = 0;

for (int i = 0; i < 6; i++) {

index = hexCode.charAt(i)%26;

sb.append(chars[index]);

}

String password = sb.toString();

===============================

private static String asHex (byte hash[]) {

StringBuffer buf = new StringBuffer(hash.length * 2);

int i;

for (i = 0; i < hash.length; i++) {

if (((int) hash & 0xff) < 0x10)

buf.append("0");

buf.append(Long.toString((int) hash & 0xff, 16));

}

return buf.toString().toUpperCase();

}

where the password should be 'aeydmi'. But what I got is 'fpnwap'.

I'm just trying to decode image code at http://captchas.net/

Pls help, Thanks !

kmthiena at 2007-7-8 > top of java,Security,Cryptography...
# 10

I don't understand why you are uisng 'hex' and for some reason you ignored the 'byte to character' conversion that I posted. The following gives the answer you require.

import java.security.MessageDigest;

public class Test2005015

{

private MessageDigest md;

private StringBuffer sb;

public Test2005015() throws java.security.NoSuchAlgorithmException

{

md = MessageDigest.getInstance("MD5");

sb = new StringBuffer(6);

}

public synchronized String generate(String value) throws java.io.UnsupportedEncodingException

{

md.reset();

md.update("secret".getBytes("UTF-8"));

md.update(value.getBytes("UTF-8"));

byte[] digest = md.digest();

sb.setLength(0);

for (int index = 0; index < 6; index ++)

{

char encodedByte = (char) ((digest[index] & 0xff) % 26 + 'a');

sb.append(encodedByte);

}

return sb.toString();

}

public static void main(String[] args)

{

try

{

String testValue = "Dvu5CPVC9g";

Test2005015 generator = new Test2005015();

for (int count = 0; count < 10; count++)

{

String pw = generator.generate(testValue);

System.out.println(pw);

}

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

The generate() method is thread safe so you only need to create one instance of the class and that can be used over and over again.

sabre150a at 2007-7-8 > top of java,Security,Cryptography...
# 11

A slightly improved version

import java.security.MessageDigest;

public class Test2005015

{

private MessageDigest md;

private char[] result = new char[6];

public Test2005015() throws java.security.NoSuchAlgorithmException

{

md = MessageDigest.getInstance("MD5");

}

public synchronized String generate(String value) throws java.io.UnsupportedEncodingException

{

md.reset();

md.update("secret".getBytes("UTF-8"));

md.update(value.getBytes("UTF-8"));

byte[] digest = md.digest();

for (int index = 0; index < 6; index ++)

{

result[index] = (char) ((digest[index] & 0xff) % 26 + 'a');

}

return new String(result);

}

public static void main(String[] args)

{

try

{

String testValue = "Dvu5CPVC9g";

Test2005015 generator = new Test2005015();

for (int count = 0; count < 10; count++)

{

String pw = generator.generate(testValue);

System.out.println(pw);

}

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

sabre150a at 2007-7-8 > top of java,Security,Cryptography...
# 12
Hey man, Really thanks a lot for your great help !! Thank you !kmthien
kmthiena at 2007-7-8 > top of java,Security,Cryptography...