Apache Commons codec Introduction and getting Started code

Source: Internet
Author: User

Java JDK has a java.security package that provides messagedigest encoding, Digest algorithms includes MD2, MD5, SHA-1, SHA-256, SHA-384, and S HA-512 and so on;

Codec is a tool class package used in the Commons project to handle commonly used coding methods, such as Hex, SHA1, MD5, Base64, and so on, the latest version is now 1.4.

Base64/hex is symmetric encryption, MD5 is irreversible encryption, we must pay attention to

Some simple cryptographic sample code:

Package Test.ffm83.commons.codec;

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.DecoderException;

import org.apache.commons.codec.binary.Base64;

import Org.apache.commons.codec.binary.Hex;

import org.apache.commons.codec.digest.DigestUtils;

/**

 * through Commonscodec do some simple encryption and decryption, etc.

 * @author Fan Fangming

*/

Public class Codecusage {

Public static void main (string[] args) {

Stringstr = "fanfangming";

Stringbase64 = encodebasetest(str);

decodebasetest (base64);

Stringhex = encodehextest(str);

decodehextest (hex);

md5test (str);

shatest (str);

}

//Base64 encode and Decode

private static Stringencodebasetest (String str) {

Base64base64 = newBase64 ();

Try {

Str= base64.encodetostring (Str.getbytes ("UTF-8"));

}catch(Unsupportedencodingexception e) {

E.printstacktrace ();

}

System. out. println ("Base64 after encoding:" + str);

return str;

}

private static void decodebasetest (stringstr) {

str= NewString (Base64. DecodeBase64(str));

System. out. println ("Base64 after decoding:" + str);

}

//Hex encode and Decode

private static Stringencodehextest (String str) {

Try {

Str= Hex. encodehexstring (Str.getbytes ("UTF-8"));

}catch(Unsupportedencodingexception e) {

E.printstacktrace ();

}

System. out. println ("Hex code after:" + str);

return str;

}

private static Stringdecodehextest (String str) {

Hexhex = newHex ();

Try {

str= NewString ((byte[]) Hex.decode (str));

}catch(Decoderexception e) {

E.printstacktrace ();

}

System. out. println ("Hex code after:" + str);

return str;

}

//MD5 Encrypt

private static String Md5test (STRINGSTR) {

Try {

str= NewString (digestutils. Md5hex(Str.getbytes ("UTF-8"));

System. out. println ("MD5 after encoding:" + str);

}catch(Unsupportedencodingexception e) {

E.printstacktrace ();

}

return str;

}

//SHA Coding

private static String Shatest (STRINGSTR) {

Try {

str= NewString (digestutils. sha256(Str.getbytes ("UTF-8"));

System. out. println ("SHA encoded:"+ str);

}catch(Unsupportedencodingexception e) {

E.printstacktrace ();

}

return str;

}

}

The results of the operation are as follows:

Base64 after encoding: rmfurmfuz21pbmc=

Base64 after decoding: fanfangming

Hex coded after: 46616e46616e676d696e67

Hex coded after: fanfangming

MD5 after encoding: 922b65d5455be7d9a4875b6b1bde638d

SHA encoded: 31FCBFD641E54747D20288F883458664A05788A9

Apache Commons codec Introduction and getting Started code

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.