Simple encryption/decryption method packaging, including Encode (), decode (), MD5 ()

Source: Internet
Author: User
Tags md5

Simple encryption/decryption method packaging, including Encode (), decode (), MD5 ()
Publication date: 2004-07-30 author: [Reprint] Source:

Package Steeven;

/*
Purpose: Simple encryption/decryption method packaging
Author: steeven@kali.com.cn
Date: 12/05/2001
Thank you: http://www-900.ibm.com/developerWorks/java/l-security/index.shtml

Description
This Class need JCE, download here:
Http://java.sun.com/security/index.html

*/

Import java.security.*;
Import javax.crypto.*;
public class Crypt {

private static String algorithm= "DES"; Define cryptographic algorithms, available des,desede,blowfish

Static Boolean debug = FALSE;

static{
Security.addprovider (New Com.sun.crypto.provider.SunJCE ());
}

 //Generate key, note this step takes longer
  public static byte[] GetKey () throws exception{
    Keygenerator keygen = keygenerator.getinstance (algorithm);
    Secretkey Deskey = Keygen.generatekey ();
    if (debug)
      System.out.println ("Generate Key:" +byte2hex ( Deskey.getencoded ()));
    return deskey.getencoded ();
 }

 //encryption
  public static byte[] Encode (byte[] input,byte[] key) throws exception{
    Secretkey Deskey = new Javax.crypto.spec.SecretKeySpec (key,algorithm);
    if (debug) {
      System.out.println ("binary string Before encryption:" +byte2hex (input ));
      System.out.println ("String before Encryption:" +new string (input));
   }
    Cipher c1 = cipher.getinstance (algorithm);
    C1.init (Cipher.encrypt_mode,deskey);
    byte[] cipherbyte=c1.dofinal (input);
    if (debug)
      System.out.println ("Encrypted binary string:" +byte2hex ( Cipherbyte));
    return cipherbyte;
 }

 //decryption
  public static byte[] Decode (byte[] input,byte[] key) throws exception{
    Secretkey Deskey = new Javax.crypto.spec.SecretKeySpec (key,algorithm);
    if (debug)
      System.out.println ("Pre-decryption Information:" +byte2hex (input)) ;
    Cipher c1 = cipher.getinstance (algorithm);
    C1.init (Cipher.decrypt_mode,deskey);
    byte[] clearbyte=c1.dofinal (input);
    if (debug) {
      System.out.println ("Decrypted binary string:" +byte2hex ( Clearbyte));
      System.out.println ("decrypted string:" + (new string (Clearbyte));
   }
    return clearbyte;
 }

 //MD5 () information summary, irreversible
  public static byte[] MD5 (byte[] input) throws exception{
    Java . Security. MessageDigest alg=java.security.messagedigest.getinstance ("MD5"); or "SHA-1"
    if (debug) {
      System.out.println ("binary string before digest:" + Byte2hex (input));
      System.out.println ("String before Digest:" +new string (input));
   }
    alg.update (input);
    byte[] Digest = Alg.digest ();
    if (debug)
      System.out.println ("binary string after digest:" +byte2hex (Digest ));
    return digest;
 }

Byte code converted to 16 binary string
public static String Byte2hex (byte[] b) {
String hs= "";
String stmp= "";
for (int n=0;n stmp= (java.lang.Integer.toHexString (B[n] & 0XFF));
if (Stmp.length () ==1)
hs=hs+ "0" +STMP;
else hs=hs+stmp;
if (n}
return Hs.touppercase ();
}

public static void Main (string[] args) throws exception{
debug = true;
byte[] key = GetKey ();
byte[] key = "Study hard". GetBytes ();
Decode (Encode ("Test Encryption". GetBytes (), key), key);
MD5 ("Test Encryption". GetBytes ());
}
}

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.