Java easy-to-use MD5 encryption (can be run directly) (1) page 1/2

Source: Internet
Author: User

1. MD5 encryption. Algorithm Is one-way encryption, that is, encrypted data cannot be restored through decryption. The related classes are included in the Java. Security. messagedigest package.
2. 3-des encryption. the encryption algorithm is reversible. The decryption party can decrypt the data by using the secret key agreed with the encryption party. The related classes are included in the javax. crypto. * package.
3. base64 encoding is used to transmit 8-bit bytes. Code The most common encoding method. The related classes are in Sun. Misc. base64decoder and sun. Misc. base64encoder.
4. urlencoder encoding is a character encoding that ensures that transmitted parameters are composed of compliant texts. The related classes are in the java.net. urlencoder package.
Details:
1. Perform MD5 encryption to obtain byte [] Copy code The Code is as follows :/**
* Perform MD5 Encryption
* @ Param string original spkey
* @ Return byte [] specifies the MD5 byte after the encryption method []
*/
Private byte [] MD5 (string strsrc)
{
Byte [] returnbyte = NULL;
Try
{
Messagedigest MD5 = messagedigest. getinstance ("MD5 ");
Returnbyte = md5.digest (strsrc. getbytes ("GBK "));
}
Catch (exception E)
{
E. printstacktrace ();
}
Return returnbyte;
}

2. Obtain the Password Key of 3-des. copy the Code the code is as follows:/**
* get the password key of 3-des
* as needed, for example, if the key is 24 bytes and MD5 is encrypted to 16 bytes, therefore, add the following eight bytes of 0
* @ Param string original spkey
* @ return byte [] specify the MD5-encrypted byte []
*/
private byte [] getenkey (string spkey)
{< br> byte [] Key = NULL;
try
{< br> byte [] ey1 = MD5 (spkey );
specified ey = new byte [24];
int I = 0;
while (I same ey [I] = deskey1 [I];
I ++;
}< br> if (I <24) {
policey [I] = 0;
I ++;
}< BR >}< br> catch (exception e) {
E. printstacktrace ();
}< br> return another ey;
}

3. 3-DES encryption copy Code the code is as follows: /**
* 3-des encryption
* @ Param byte [] the byte []
* @ Param byte [] enkey 3-des encryption key to be encrypted by SRC
* @ return byte [] 3-des encrypted byte []
*/
Public byte [] encrypt (byte [] SRC, byte [] enkey)
{< br> byte [] encrypteddata = NULL;
try
{< br> desedekeyspec DKS = new desedekeyspec (enkey );
secretkeyfactory keyfactory = secretkeyfactory. getinstance ("desede");
secretkey = keyfactory. generatesecret (DKS);
cipher = cipher. getinstance ("desede");
cipher. init (cipher. encrypt_mode, key);
encrypteddata = cipher. dofinal (SRC);
}< br> catch (exception e)
{< br> E. printstacktrace ();
}< br> return encrypteddata;
}

4. encode the string with base64Copy codeThe Code is as follows :/**
* Base64 encoding of strings
* @ Param byte [] characters to be encoded by SRC
*
* @ Return string the encoded string
*/
Public String getbase64encode (byte [] SRC)
{
String requestvalue = "";
Try {
Base64encoder base64en = new base64encoder ();
Requestvalue = base64en. encode (SRC );
// System. Out. println (requestvalue );
}
Catch (exception e ){
E. printstacktrace ();
}

Return requestvalue;
}

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.