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

Source: Internet
Author: User
Tags base64 md5 md5 encryption
1, MD5 encryption, the encryption algorithm is one-way encryption, that is, the encrypted data can no longer be restored by 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 be encrypted by the agreement with the secret key to decrypt. The related classes are included in the javax.crypto.* package.
3. Base64 encoding is the most commonly used encoding method for transmitting 8bit byte code. Related classes are in Sun.misc.BASE64Decoder and Sun.misc.BASE64Encoder.
4, Urlencoder code, is a character encoding, to ensure that the parameters are transmitted by following the specification of the text composition. The related class is in the Java.net.URLEncoder package.
Details:
1, to carry out MD5 encryption, get byte[]
Copy Code code as follows:

/**
* For MD5 encryption
* @param String Original Spkey
* @return byte[] Specifies the byte[of the encryption method after MD5]
*/
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, get the 3-des key
Copy Code code as follows:

/**
* Get the 3-des key
* According to the need, such as the key is 24 bytes, MD5 encryption is 16 bytes, so after 8 bytes of 0
* @param String Original Spkey
* @return byte[] Specifies the byte[of the encryption method after MD5]
*/
Private byte[] Getenkey (String spkey)
{
Byte[] Deskey=null;
Try
{
Byte[] DesKey1 = MD5 (Spkey);
Deskey = new BYTE[24];
int i = 0;
while (I < deskey1.length && I < 24) {
Deskey[i] = Deskey1[i];
i++;
}
if (I < 24) {
Deskey[i] = 0;
i++;
}
}
catch (Exception e) {
E.printstacktrace ();
}
return deskey;
}

3, 3-des encryption
Copy Code code as follows:

/**
* 3-des Encryption
* @param byte[] src to be 3-des encrypted byte[]
* @param byte[] enkey 3-des encryption key
* @return byte[] 3-des encrypted byte[]
*/
Public byte[] Encrypt (byte[] src,byte[] enkey)
{
byte[] EncryptedData = null;
Try
{
Desedekeyspec DKs = new Desedekeyspec (Enkey);
Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("Desede");
Secretkey key = Keyfactory.generatesecret (DKS);
Cipher Cipher = cipher.getinstance ("Desede");
Cipher.init (Cipher.encrypt_mode, key);
EncryptedData = cipher.dofinal (src);
}
catch (Exception e)
{
E.printstacktrace ();
}
return EncryptedData;
}

4. BASE64 Encoding of strings
Copy Code code as follows:

/**
* BASE64 Encoding of strings
* @param byte[] src characters to encode
*
* String encoded by @return 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;
}

Current 1/2 page 12 Next read the full text

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.