"Internship Program Record" (i) cryptographic algorithms MD5 and RSA

Source: Internet
Author: User
Tags md5 digest md5 encryption md5 hash

What is MD5 encryption?

MD5 's full name is Message-digest algorithm 5 (Information-Digest algorithm), in the early 90 by MIT Laboratory for Computer Science and RSA Data Security Inc Ronald L. Riv EST developed, through the development of MD2, MD3 and MD4.

MD5 algorithm

First of all, MD5 is irreversible and can only be encrypted and not decrypted. For example, the plaintext is yanzi1225627, get MD5 encrypted string is: 14F2AE15259E2C276A095E7394DA0CA9 but not by the back of a large series of inverted yanzi1225627. Therefore, you can use it to store user-entered passwords on the server. It is also used to download a file to verify whether the file is tampered with in the middle, see the principle: http://blog.csdn.net/forgotaboutgirl/article/details/7258109 It's easier to implement MD5 in Java on Android or on a PC, because Java has done it in java.security.MessageDigest. Here is a Md5util.java class:

 PackageOrg.md5.util;Importjava.security.MessageDigest; Public classMd5util { Public Final Staticstring getmd5string (string s) {CharHexdigits[] = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ',                ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ',                ' A ', ' B ', ' C ', ' D ', ' E ', ' F ' }; Try {            byte[] Btinput =s.getbytes (); //MessageDigest object for obtaining the MD5 digest algorithmMessageDigest mdinst = messagedigest.getinstance ("MD5"); //updates the digest with the specified bytesmdinst.update (btinput); //Get ciphertext            byte[] MD =mdinst.digest (); //convert ciphertext to 16-binary string form            intj =md.length; CharStr[] =New Char[J * 2]; intK = 0;  for(inti = 0; I < J; i++) {                byteBYTE0 =Md[i]; Str[k+ +] = hexdigits[byte0 >>> 4 & 0xf]; Str[k+ +] = hexdigits[byte0 & 0xf]; }            return NewString (str); }        Catch(Exception e) {e.printstacktrace (); return NULL; }    }}

Test class:

 Public class Test {    publicstaticvoid  main (string[] args) {        = "yanzi1225627" ;        System.out.println ("MD5 after encryption =" + md5util.getmd5string (srcstring));}    }

Results

MD5 after encryption =14f2ae15259e2c276a095e7394da0ca9

Private Static Final CharHex_digits[] = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ',          ' A ', ' B ', ' C ', ' D ', ' E ', ' F ' };  Public StaticString tohexstring (byte[] b) {//String to ByteStringBuilder SB =NewStringBuilder (B.length * 2);  for(inti = 0; i < b.length; i++) {sb.append (hex_digits[(b[i)& 0xf0) >>> 4]); Sb.append (Hex_digits[b[i]& 0x0f]); }        returnsb.tostring (); }   Publicstring MD5 (string s) {Try {          //Create MD5 HashMessageDigest digest = java.security.MessageDigest.getInstance ("MD5");          Digest.update (S.getbytes ()); byteMessagedigest[] =digest.digest (); returntohexstring (messagedigest); } Catch(nosuchalgorithmexception e) {e.printstacktrace (); }                                return""; }  

Second, RSA encryption

RSA is reversible, a string can be encrypted by RSA, after the encrypted string to the peer, such as the server, and then decrypt. The premise is that the server knows the decrypted private key, of course, this private key is best not to network transmission. The following variables are required in the RSA algorithm description:

1, p and q are unequal, large enough of two prime numbers. P and Q are confidential.

2, n = p*q n is public

3, f (n) = (p-1) * (q-1)

4, E is and f (n) coprime prime number

5. Calculation parameter D

6. The public key ku= (e,n) private key kr= (D,n) is computed by the above 5 steps.

The following two articles have a clear description of this:

Http://wenku.baidu.com/view/e53fbe36a32d7375a417801b.html

Http://bank.hexun.com/2009-06-24/118958531.html

Here is the Java implementation Rsautil.java class:

"Internship Program Record" (i) cryptographic algorithms MD5 and RSA

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.