Java built-in base64 + md5 encryption using code, and specified characters md5 encryption code

Source: Internet
Author: User

Java built-in base64 + md5 encryption using code


Package org. rui. base64; import java. io. IOException; import sun. misc. BASE64Decoder; import sun. misc. BASE64Encoder; public class Base64 {/*** @ param args */public static void main (String [] args) {Base64 base = new Base64 (); string pass = "123"; String en = base. encode (pass); System. out. println (en); String de = base. decode (en); System. out. println (de);} // enter the password public String encode (String pass) {BASE64Encoder base = new BASE64Encoder (); pass = base. encode (pass. getBytes (); return pass;} // public String encode (byte [] B) {return encode (new String (B ));} // decrypt public String decode (String pass) {BASE64Decoder base = new BASE64Decoder (); byte [] B = null; try {B = base. decodeBuffer (pass);} catch (IOException e) {e. printStackTrace ();} return new String (B );}}

Md5 + base64

Package org. rui. base64; import java. security. messageDigest; import java. security. noSuchAlgorithmException; public class Md5 {/*** @ param args */public static void main (String [] args) {String pass = "1234 "; byte [] B = getDigest (pass. getBytes (); // System. out. println ("md5" + new String (B); Base64 b64 = new Base64 (); pass = b64.encode (B); // System. out. println ("md5 encrypted base64:" + pass); // If the Database Password is String dbpass = "gdyb21LQTT8229gxPtBV"; System. out. println (pass); System. out. println (dbpass); System. out. println ("consistent password:" + pass. equals (dbpass);} // MD5 encryption public static byte [] getDigest (byte [] B) {try {MessageDigest md = MessageDigest. getInstance ("MD5"); md. update (B); return md. digest ();} catch (NoSuchAlgorithmException e) {// FIXME: DDDe. printStackTrace ();} return null ;}}

Handwritten md5 encryption code

package snail.su.rui.util;import java.security.MessageDigest;public class Md5 {public final static String MD5(String s){char hexDigits[]={'0','1','2','3','4','5','9','7','8','6', 'l','i','a','n','g','r'};// char hexDigits[]={'0','1','2','3','4','5','6','7','8','9',// 'h','e','g','k','e','f'};//e10hkg3949eh59heee56e057f20f883e try { byte[] strTemp=s.getBytes(); MessageDigest mdTemp=MessageDigest.getInstance("MD5");mdTemp.update(strTemp);byte[] md=mdTemp.digest();int j=md.length;char str[]=new char[j*2];int k=0;for(int i=0;i
 
  >>4 & 0xf];str[k++]=hexDigits[byte0 & 0xf];}return new String(str);} catch (Exception e) {return null;}}public static void main(String [] args){String addmd5=Md5.MD5("123456");System.out.println("123="+addmd5);if(addmd5.equals("e10adc3949ba59abbe56e057f20f883e")){System.out.println("Md5.main()");}}}
 


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.