Simplest DES encryption algorithm implementation, des encryption algorithm

Source: Internet
Author: User

Simplest DES encryption algorithm implementation, des encryption algorithm

Base64.java

Package com. mstf. des; import java. io. unsupportedEncodingException;/*** base64 encoding/decoding * @ author ceet **/public class Base64 {public static String encode (String data) {return new String (encode (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 st Atic char [] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 +/= ". toCharArray (); private static byte [] codes = new byte [256]; static {for (int I = 0; I <256; 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) (26 + I-'A') ;}for (int I = '0'; I <= '9'; I ++) {codes [I] = (byte) (52 + 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) <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): 64)]; val >>=6; out [index + 2] = alphabet [(trip? (Val & 0x3F): 64)]; val >>=6; out [index + 1] = alphabet [val & 0x3F]; val >>= 6; out [index + 0] = alphabet [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;} byt E [] 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) (accum> 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 (strKey);} public void setKey (String strKey) {try {KeyGenerator generator = KeyGenerator. getInstance ("DES"); generator. init (new SecureRandom (strKey. getBytes (); // generate keythis Based on the parameter. key = generator. generateKey ();} catch (Exception e) {e. printStackTrace () ;}} public String encrypt (String source) {return encrypt (source, "UTF-8");} public String decrypt (String encryptedData) {return decrypt (encryptedData, "UTF-8");} public String encrypt (String source, String charSet) {String encrypt = null; try {byte [] ret = encrypt (source. getBytes (charSet); 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 (); descryptedData = new String (ret, charSet);} catch (Exception e) {e. printStackTrace (); descryptedData = null;} return descryptedData;} private byte [] encrypt (byte [] primaryData) {try {Cipher cipher = Cipher. getInstance ("DES"); // The Cipher object actually completes the encryption operation cipher. init (Cipher. ENCRYPT_MODE, this. key); // use the key to initialize the Cipher object (encrypted) return cipher. doFinal (primaryData);} catch (Exception e) {e. printStackTrace (); return null ;}} private byte [] descrypt (byte [] encryptedData) {try {Cipher cipher = Cipher. getInstance ("DES"); // The Cipher object actually completes the decryption operation cipher. init (Cipher. DECRYPT_MODE, this. key); // use the key to initialize the Cipher object (decryption) return cipher. doFinal (encryptedData);} catch (Exception e) {e. printStackTrace (); return null ;}} public static void main (String [] args) {String code = "ceet"; DESUtil 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 ("decryption:" + decrypt );}}

  

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.