Base64.java
Package com.mstf.des; Import java.io.UnsupportedEncodingException; /** * Base64 encode/decode * @author Ceet * */public class Base64 {public static string encode (string data) {return new string (ENC Ode (Data.getbytes ()));} public static string decode (string data) {try {return new string (Decode (Data.tochararray ()), "Utf-8");} catch ( Unsupportedencodingexception e) {e.printstacktrace (); return null;}} private static char[] alphabet = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/=". ToCharArray (); Private static byte[] codes = new byte[256]; static {for (int i = 0; i <; i++) {Codes[i] =-1;} for (int i = ' a ', I <= ' z '; i++) {Codes[i] = (byte) (i-' a ');} for (int i = ' a '; I <= ' z '; i++) {Codes[i] = (byte) (+ i-' a ');} for (int i = ' 0 '; I <= ' 9 '; i++) {Codes[i] = (byte) (+ i-' 0 ');} codes[' + '] = 62;codes['/'] = 63;} public static char[] Encode (byte[] data) {char[] out = new char[((Data.length + 2)/3) * 4];FOR (int i = 0, index = 0; i < Data.length; i + = 3,Index + = 4) {Boolean quad = False;boolean trip = false; int val = (0xFF & (int) data[i]); Val <<= 8;if ((i + 1) & Lt Data.length) {val |= (0xFF & (int) Data[i + 1]); trip = true;} Val <<= 8;if ((i + 2) < Data.length) {val |= (0xFF & (int) Data[i + 2]); quad = true;} Out[index + 3] = alphabet[(quad? (Val & 0x3F): (+)];val >>= 6;out[index + 2] = alphabet[(trip? (Val & 0x3F): (+)];val >>= 6;out[index + 1] = alphabet[val & 0x3f];val >>= 6;out[index + 0] = Alphabe T[val & 0x3F];} return out;} public static byte[] Decode (char[] data) {int templen = data.length;for (int ix = 0; ix < data.length; ix++) {if (data [IX] > 255) | | Codes[data[ix]] < 0) {--templen;}} int len = (TEMPLEN/4) * 3;if ((templen% 4) = = 3) {len + 2;} if ((templen% 4) = = 2) {len + 1;} Byte[] out = new Byte[len]; int shift = 0;int Accum = 0;int index = 0; for (int ix = 0; ix < data.length; ix++) {int value = (Data[ix] > 255)? -1:codes[data[ix]]; IF (Value >= 0) {accum <<= 6;shift + 6;accum |= value;if (Shift >= 8) {shift-= 8;out[index++] = (byte) ((ACCU M >> shift) & 0xFF);}}} if (Index! = out.length) {throw new Error ("miscalculated data length (wrote" + index+ "instead of" + Out.length + ")"); } return out;}}
Desutil.java
Package com.mstf.des; Import Java.security.key;import java.security.SecureRandom; Import Javax.crypto.cipher;import javax.crypto.KeyGenerator; /** * des symmetric algorithm (encryption/decryption) * * @author Ceet * */public class Desutil {private key key; public Desutil (String strkey) {Setkey (s Trkey);} public void Setkey (String strkey) {try {keygenerator generator = keygenerator.getinstance ("DES"); Generator.init (new SecureRandom (Strkey.getbytes ())); Generate Keythis.key = Generator.generatekey () according to the parameters;} catch (Exception e) {e.printstacktrace ();}} public string Encrypt (string source) {return encrypt (source, "Utf-8"), and public string decrypt (string EncryptedData) {Retu RN Decrypt (EncryptedData, "Utf-8");} public string Encrypt (string source, String charSet) {String encrypt = Null;try {byte[] ret = Encrypt (source.getbytes (char Set)); encrypt = new String (Base64.encode (ret));} catch (Exception e) {e.printstacktrace (); encrypt = null;} return encrypt;} public string Decrypt (string EncryptedData, String charSet) {string DescrypteddaTa = null;try {byte[] ret = Descrypt (Base64.decode (Encrypteddata.tochararray ()));d Escrypteddata = new String (ret, CharSet);} catch (Exception e) {e.printstacktrace ();d escrypteddata = null;} return descrypteddata;} Private byte[] Encrypt (byte[] primarydata) {try {Cipher Cipher = cipher.getinstance ("DES");// The Cipher object actually completes the cryptographic operation Cipher.init (Cipher.encrypt_mode, This.key); Initializes the cipher object with the key (encrypted) return cipher.dofinal (Primarydata);} catch (Exception e) {e.printstacktrace (); return null;}} Private byte[] Descrypt (byte[] encrypteddata) {try {Cipher Cipher = cipher.getinstance ("DES");//Cipher object actually completes decryption operation Cipher . Init (Cipher.decrypt_mode, This.key); Initializes the cipher object with the key (decryption) return cipher.dofinal (EncryptedData);} catch (Exception e) {e.printstacktrace (); return null;}} public static void Main (string[] args) {String code = "Ceet";D esutil desutil = new Desutil ("key"); String encrypt = desutil.encrypt (code); String decrypt = Desutil.decrypt (encrypt); System.out.println ("Original content:" + code); SYSTEM.OUT.PRINTLN ("encryption:" + enCrypt); System.out.println ("Decrypt:" + Decrypt);}}
The simplest implementation of DES encryption algorithm