JAVA implements DES encryption and decryption __java

Source: Internet
Author: User
Tags array length decrypt stringbuffer


Des.java

Import java.security.*;

Import javax.crypto.*;
	/** * des plus decryption algorithm * * public class DES {private static String Strdefaultkey = "abcDEF123";
	Private Cipher encryptcipher = null;


	Private Cipher decryptcipher = null;
	/** * Default construction method, using the default key * @throws Exception/Public DES () throws Exception {this (Strdefaultkey); /** * Specifies the key construction method * @param strkey specified key * @throws Exception/Public DES (String strkey) throws Exception {S
		Ecurity.addprovider (New Com.sun.crypto.provider.SunJCE ());
		Key key = Getkey (Strkey.getbytes ());
		Encryptcipher = Cipher.getinstance ("DES");
		Encryptcipher.init (Cipher.encrypt_mode, key);
		Decryptcipher = Cipher.getinstance ("DES");
	Decryptcipher.init (Cipher.decrypt_mode, key); /** * Encrypt String * @param strin strings to be encrypted * @return Encrypted String * @throws Exception/public string encrypt (string s
	Trin) throws Exception {return bytearr2hexstr Encrypt (Strin.getbytes ()));
/** * Encrypted byte array * @param ARRB byte array to encrypt * @return encrypted byte array	 * @throws Exception/public byte[] Encrypt (byte[] arrb) throws Exception {return encryptcipher.dofinal (ARRB); /** * Decryption String * @param strin string to Decrypt * @return decrypted String * @throws Exception/public string decrypt (Strin
	G Strin) throws Exception {return new String (Decrypt (Hexstr2bytearr (Strin))); /** * Decrypt byte array * @param ARRB byte array to decrypt * @return decrypted byte array * @throws Exception/public byte[] Decrypt (byte
	[] Arrb throws Exception {return decryptcipher.dofinal (ARRB); /** * Generates the key from the specified string, the key requires a byte array length of 8 bits * less than 8 bits after 0, more than 8 bit only the first 8 bits * @param arrbtmp the byte array that makes up the string * @return the generated key * @thr
		OWS java.lang.Exception * * Private Key Getkey (byte[] arrbtmp) throws Exception {byte[] arrb = new Byte[8];
		for (int i = 0; i < arrbtmp.length && i < arrb.length; i++) {arrb[i] = Arrbtmp[i];
		Key key = new Javax.crypto.spec.SecretKeySpec (ARRB, "DES");
	Return key;
/** * Converts a byte array to a string representing a 16-valued value, * such as: byte[]{8,18} converted to: 0813,	 * and public static byte[] Hexstr2bytearr (string strin) * Mutually reversible conversion process * @param ARRB byte array to convert * @return converted String * @ Throws Exception This method does not handle any exceptions, all exceptions are thrown */public static String Bytearr2hexstr (byte[] arrb) throws Exception {int Ilen
		= Arrb.length;
		StringBuffer sb = new StringBuffer (Ilen * 2);
			for (int i = 0; i < Ilen i++) {int inttmp = arrb[i];
			while (Inttmp < 0) {inttmp = inttmp + 256;
			} if (Inttmp <) {Sb.append ("0");
		} sb.append (Integer.tostring (inttmp, 16));
	return sb.tostring ();  /** * Converts a string representing a value of 16 into a byte array, * and public static string Bytearr2hexstr (byte[] arrb) * Mutually reversible conversion process * @param strin The string that needs to be converted * @return the converted byte array * @throws Exception This method does not handle any exceptions, all exceptions throw the/public static byte[] Hexstr2bytearr (strin
		G Strin) throws Exception {byte[] arrb = Strin.getbytes ();
		int ilen = Arrb.length;
		byte[] Arrout = new BYTE[ILEN/2]; for (int i = 0; i < ilen i = i + 2) {String strtmp = new String (ARRB, I, 2);
		ARROUT[I/2] = (byte) integer.parseint (strtmp, 16);
	return arrout;
 }


}


Encryutil.java

/**
 * Cryptographic decryption Tool class/public
class Encryutil {
	
	/**
	 * Using the default key for DES encryption/public
	static String Encrypt (String plaintext) {
		try {return
			new DES (). Encrypt (plaintext);
		\ catch (Exception e) {
			return null;
		}

	
	/**
	 * Using the specified key for des decryption
	/public static string encrypt (string plaintext, string key) {
		try {
			return new DES (key). Encrypt (plaintext);
		catch (Exception e) {return
			null;
		}
	}
	

	/**
	 * Use default key for DES decryption
	 /public
	static string decrypt (string plaintext) {
		try {return
			new DES () . Decrypt (plaintext);
		catch (Exception e) {return
			null;
		}
	}

	
	/**
	 * Using the specified key for des decryption
	/public static string decrypt (string plaintext, string key) {
		try {
			return new DES (key). Decrypt (plaintext);
		catch (Exception e) {return
			null;
		}
	}

}


Testdes.java

public class Testdes {public
	
	static void Main (string[] args) throws Exception {
		String str = "01234abcdabcd!@#$";
		String t = "";
		SYSTEM.OUT.PRINTLN ("Encrypted:" + (t = encryutil.encrypt (str));
		System.out.println ("After decryption:" + encryutil.decrypt (t));
	}



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.