160705. Summary: Common methods in Commons-codec.jar

Source: Internet
Author: User
Tags base64 md5 encryption

First, BASE64 encoding and decoding
Import org.apache.commons.codec.EncoderException;
Import org.apache.commons.codec.binary.Base64;
public class TestBase64 {
public static void Main (string[] args) throws Encoderexception, Unsupportedencodingexception {
Base64 base64 = new Base64 ();
String str = "AAAAA Me";
String result = base64.encodetostring (Str.getbytes ("UTF-8"));//Encoding
SYSTEM.OUT.PRINTLN (result);
Byte[] decode = Base64.decode (Result.getbytes ());//decoding
System.out.println (New String (decode));
}
}
Hex Coding and decoding
Import org.apache.commons.codec.DecoderException;
Import Org.apache.commons.codec.binary.Hex;
public class Testhex {
public static void Main (string[] args) throws Decoderexception, Unsupportedencodingexception {
String str = "Test";
/** Code */
String hexstring = hex.encodehexstring (Str.getbytes ("UTF-8"));//Direct one step
System.out.println (hexstring);
char[] Encodehex = Hex.encodehex (Str.getbytes (), true);//first converted to char array, the second parameter means whether all converted to lowercase
System.out.println (New String (Encodehex));
/** Decoding */
byte[] Decodehex = Hex.decodehex (Encodehex),//char array type
System.out.println (New String (Decodehex));
byte[] DecodeHex2 = Hex.decodehex (Hexstring.tochararray ());//String type, this method requires the incoming char[]
System.out.println (New String (DECODEHEX2));
}
}
Third, MD5 encryption (MD5 is an irreversible algorithm, can only be encrypted)
Import java.io.UnsupportedEncodingException;
Import Org.apache.commons.codec.digest.DigestUtils;
public class TestMD5 {
public static void Main (string[] args) throws Unsupportedencodingexception {
String str = "Test";
String MD5 = Digestutils.md5hex (Str.getbytes ("UTF-8"));
SYSTEM.OUT.PRINTLN (MD5);
}
}
Iv.. SHA Encryption
Import java.io.UnsupportedEncodingException;
Import Org.apache.commons.codec.digest.DigestUtils;
public class Testsha {
public static void Main (string[] args) throws Unsupportedencodingexception {
String str = "Test China";
String Sha1hex = Digestutils.sha1hex (Str.getbytes ("UTF-8"));
System.out.println (Sha1hex);
}
}
V. Metaphone and SOUNDEX
Metaphone establish the same key to pronounce similar words, more accurate than Soundex, but metaphone no fixed length, Soundex is fixed the first English word plus 3 numbers. This is usually used in similar tones, and can also be used in MP3 software development
Metaphone () is more accurate than the SOUNDEX () function because Metaphone () understands basic English pronunciation rules

Import Org.apache.commons.codec.language.Metaphone;
Import Org.apache.commons.codec.language.RefinedSoundex;
Import Org.apache.commons.codec.language.Soundex;
public class Testmetaphoneandsoundex {
public static void Main (string[] args) {
String str = "TESTGGGGGG";
/**metaphone no fixed length */
Metaphone Metaphone = new Metaphone ();
String Metaphoneencode = Metaphone.encode (str);
System.out.println (Metaphoneencode);
/**refinedsoundex*/
Refinedsoundex Refinedsoundex = new Refinedsoundex ();
String Refinedsoundexencode = Refinedsoundex.encode (str);
System.out.println (Refinedsoundexencode);
/**soundex fixed first english word plus 3 digits */
Soundex Soundex = new Soundex ();
String Soundexencode = Soundex.encode (str);
System.out.println (Soundexencode);
}
}
Liu, Urlcodec
Import org.apache.commons.codec.DecoderException;
Import org.apache.commons.codec.EncoderException;
Import Org.apache.commons.codec.net.URLCodec;
public class Testurlcodec {
public static void Main (string[] args) throws Encoderexception, Decoderexception {
String url = "Http://baidu.com?name= hello";
Urlcodec codec = new Urlcodec ();
String encode = codec.encode (URL);
System.out.println (encode);
String decode = Codec.decode (encode);
System.out.println (decode);
}
}
In addition to these there are many algorithms such as HMAC, you can choose according to needs, Commons-codec-1.10.jar is a cryptographic decoding related jar package

160705. Summary: Common methods in Commons-codec.jar

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.