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 ());
}
}