MD5 using 16 binary

Source: Internet
Author: User

MD5 using 16 binary message digestCategory: java_secruity2012-12-28 13:11 719 People read Comments (0) favorite reports Message digest

Because the data is represented in the computer and eventually in binary form, sometimes binary is used to solve the problem more intuitively.

However, the binary number is too long. For example , an int type occupies 4 bytes and32 bits. For example , 100, a binary number expressed in int type would be:

00000000 00000000 00000000 01100100

Faced with such a long number of thinking or operation, no one would like it. Therefore,c,c++, as well as Java, does not provide a way to write binary numbers directly in code.

An approach to the expression of eight decimal numbers

How to express an octal number? If this number is 876, we can conclude that it is not an octal number because it is not possible to have more than 7 Arabic numerals in octal numbers. But if this number is 123, is 567, or 12345670, then it is octal or 10 binary, it is possible. So, a number, if you want to indicate that it uses octal, you must precede it with a 0, such as:123 is decimal, but 0123 means octal. This is how the octal number is expressed. Now, for the same number, such as 100, we can use the usual 10 in the code , for example, when the variable is initialized:

int a = 100;

We can also write this:

int a = 0144; 0144 is the eight-in-one system ;

how a 10 binary number turns into a 8 binary, we'll learn later. Remember, when you're using octal, you can't have the last 0 less. Otherwise the computer will be 10 binary. However, there is a place where octal numbers are used, but you cannot use Add 0, which is the "escape character" expression that we learned before to express a character.

How to express hexadecimal numbers

If you do not use a special writing style,The 16 binary number will also be10 into the mix. A random number: 9876, it is 10 binary. 16 number must be  0x begins. For example  0x1 represents a 1 represents a decimal. In addition, such as: 0xff,0xff,0x102a, and so on. The 0x style in 0 is the digital 0, not the letter o)

Here are some examples of usage:

int a = 0x100f;

int B = 0x70 + A;

 

At this point, we have learned all the following:10 binary,8 binary,16 binary number expression. The last point is important,10 binary number has positive and negative points, such as 12 means positive 12, and 12 for negative 12, but 8 and 16 binary can only use up to unsigned positive integers, if you are in the code:- 078, or write:-0xf2, the compiler does not treat it as a negative number.

Example of using hexadecimal for message digest

public static void Main (string[] args) {

try {

MessageDigest md=messagedigest.getinstance ("MD5");

String password= "Cranetower";

String name= "0112345";

Byte nam[]=name.getbytes ("Utf-8");

Byte psd[]=password.getbytes ("Utf-8");

Md.update (PSD);

Md.update (NAM);

byte Encryption[]=md.digest ();

StringBuffer sb=new StringBuffer (encryption.length*2);

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

sb.append (Character.fordigit (encryption[i]&0xf0>>4,));

Sb.append (Character.fordigit (encryption[i]&0x1f>>4,16));

}

} catch (Exception e) {

E.printstacktrace ();

}

SB Output Result: 40f1608051804000407131e020002000

Detailed

The message digest uses the single function MD algorithm, originally only needs to give the password message digest, in order to enhance the password crack difficulty, the update joins the user name to make its summary information more irregular. The byte array after the message digest is encryption to 16, in order for the result to be displayed as a 32-byte string. Here the binary bytes are converted to 16 bytes, each four bits is a 16 binary number, so 16*8/4=32 bytes.

The static char fordigit(int digit, int radix) method in Charactor.

0XF0 is a decimal 2,402-binary representation of 00000000 00000000 00000000 11110000

The bitwise and characteristic is that the corresponding bit is 1 o'clock the result is 1, otherwise 0, so the encryption[i]&0xf0 operation result is 00000000 00000000 00000000 xxxx0000 This form , Move the four-bit to the right and turn the 00000000 00000000 00000000 0000xxxx into this form. The shift operation can refer to "Java Development Combat Classic"p39 page , so that the result encryption[i]&0xf0>>4 low 0 digits not more than 4, in line with the hexadecimal conversion requirements

Note: The 16 binary number here is not easy to select the corresponding bit is 0, the bitwise and the 16 binary number can not be too small

Examples can also be useful in Bouncycastle package hex class Conversion

Import Java.security.MessageDigest;

Import Org.bouncycastle.util.encoders.Hex;

public class Md5test {

public static void Main (string[] args) throws Exception {

MessageDigest md=messagedigest.getinstance ("MD5");

String password= "Cranetower";

String name= "0112345";

Byte nam[]=name.getbytes ("Utf-8");

Byte psd[]=password.getbytes ("Utf-8");

Md.update (PSD);

Md.update (NAM);

byte Encryption[]=md.digest ();

String hexmd5 = new String (Hex.encode (encryption));

System.out.println (HEXMD5);

}

}

Result 740f96a8a558f4509477e37e324072f0

Note: The point is why to convert to 16 binary instead of 10 or 8, because the data is in the binary form of the computer, the source string is initially binary, that is, the binary array, and the length of this array is 16, In order to show the result as a 32-byte string (why might it be for good looks or international unification). Here the binary bytes are converted to 16 bytes, each four bits is a 16 binary number, so 16*8/4=32 bytes.

MD5 using 16 binary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.