Password, file MD5 encryption, password sha256, sha384, sha512hex, and so on

Source: Internet
Author: User
Tags sha1 encryption

Package encryption; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. outputstream; import Java. io. unsupportedencodingexception; import Java. security. messagedigest; import Java. util. arrays; import Org. apache. commons. codec. binary. base64outputstream; import Org. apache. commons. codec. digest. digestutils; import Org. apache. commons. codec. digest. messagedigestalgorithms; import sun. misc. Base64decoder; import sun. misc. base64encoder;/*** encryption tool class * @ author wangzg * @ date 2014-7-25 * @ need commons-codec -*. *. jar */public class encryption {/*** @ title: Main * @ Description: * @ Param: * @ return void * @ User: wangzg * @ Date: 2014-7-25 * @ throws */public static void main (string [] ARGs) {string strsource = "admin"; string STR = getmd5 (strsource); system. out. println (strsource + ", Length:" + Str. l Ength () + ", MD5:" + Str); STR = getsha1 (strsource); system. out. println (strsource + ", Length:" + Str. length () + ", sha1:" + Str); STR = getsha256 (strsource); system. out. println (strsource + ", Length:" + Str. length () + ", sha256:" + Str); STR = getsha384 (strsource); system. out. println (strsource + ", Length:" + Str. length () + ", sha384:" + Str); STR = getsha512 (strsource); system. out. println (strsource + ", Length:" + Str. length () + ", sha51 2: "+ Str); digestfile (" Doc \ 1.txt", messagedigestalgorithms. MD5); string target = getbase64 (strsource); system. out. println (strsource); system. out. println (target); system. out. println (getfrombase64 (target);}/*** MD5 encryption, returns a 32-bit hexadecimal string * @ title: getmd5 * @ Description: * @ Param: * @ return string * @ User: wangzg * @ Date: 2014-7-25 * @ throws */public static string getmd5 (string source) {return digestutils. MD 5hex (source);}/*** sha1 encryption, returns a 40-bit hexadecimal string * @ title: getsha1 * @ Description: * @ Param: * @ return string * @ User: wangzg * @ Date: 2014-7-25 * @ throws */public static string getsha1 (string source) {return digestutils. sha1hex (source);}/*** sha256 encryption, returns a 64-bit hexadecimal string * @ title: getsha256 * @ Description: * @ Param: * @ return string * @ User: wangzg * @ Date: 2014-7-25 * @ throws */public static string getsha256 (strin G source) {return digestutils. sha256hex (source);}/*** sha384 encryption, return a 96-bit hexadecimal string * @ title: getsha384 * @ Description: * @ Param: * @ return string * @ User: wangzg * @ Date: 2014-7-25 * @ throws */public static string getsha384 (string source) {return digestutils. sha384hex (source);}/*** sha512 encryption, returns the 128-bit hexadecimal string * @ title: getsha512 * @ Description: * @ Param: * @ return string * @ User: wangzg * @ Date: 2014-7-25 * @ th Rows */public static string getsha512 (string source) {return digestutils. sha512hex (source);}/*** file encryption * @ title: digestfile * @ Description: * @ Param: Filename: Encrypted File, algorithm: encryption Algorithm * @ return void * @ User: wangzg * @ Date: 2014-7-25 * @ throws */public static void digestfile (string filename, string algorithm) {byte [] B = new byte [1024*4]; int Len = 0; fileinputstream FCM = NULL; fileoutputstream Fos = NULL; try {messagedigest MD = messagedigest. getinstance (algorithm); FCM = new fileinputstream (filename); While (LEN = Fi. Read (B ))! =-1) {Md. update (B, 0, Len);} byte [] digest = md. digest (); // system. out. println (arrays. tostring (Digest); stringbuffer filenamebuffer = new stringbuffer (128 ). append (filename ). append (". "). append (algorithm); Fos = new fileoutputstream (filenamebuffer. tostring (); outputstream encodedstream = new base64outputstream (FOS); encodedstream. write (Digest); encodedstream. flush (); encodedstream. close ();} Cat CH (exception e) {system. Out. println ("error computing Digest:" + E) ;}finally {try {If (FS! = NULL) FCM. Close (); If (FOS! = NULL) FOS. close ();} catch (exception ignored) {}}/*** base64 encryption * @ title: getbase64 * @ Description: * @ Param: * @ return string * @ User: wangzg * @ Date: 2014-7-25 * @ throws */public static string getbase64 (string Str) {byte [] B = NULL; string S = NULL; try {B = Str. getbytes ("UTF-8");} catch (unsupportedencodingexception e) {e. printstacktrace ();} If (B! = NULL) {S = new base64encoder (). encode (B);} return s;}/*** base64 decryption * @ title: getfrombase64 * @ Description: * @ Param: * @ return string * @ User: wangzg * @ Date: 2014-7-25 * @ throws */public static string getfrombase64 (string s) {byte [] B = NULL; string result = NULL; If (s! = NULL) {base64decoder decoder = new base64decoder (); try {B = decoder. decodebuffer (s); Result = new string (B, "UTF-8");} catch (exception e) {e. printstacktrace () ;}} return result ;}}

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.