DES symmetric encryption and its source code (original)

zhaozj2021-02-16  54

Package util;

Import javax.crypto.cipher; import javax.crypto.keygenerator; import java.security. *; import java.io. *;

/ ** * Created by Intellij IDEA. * User: Administrator * Date: DEC 4, 2003 * Time: 9:29:19 Pm * To change this template Uses | File Templates.

* @ Author xiaoyusong@tang.com * This class is an encrypted processing class. Mainly used to encrypt, decrypt operations. * / public class desencryptutil {/ ** * Get the DES encryption key. In the process of transaction processing, it should be more * change the key. Support for JCE, if the JDK version is below 1.4, you need * Install JCE-1_2_2 to use it. * @Return Key return symmetric key * @throws java.security.NoSuchAlgorithmException * @see util.EncryptUtil including encrypting and decrypting * / public static Key getKey () throws NoSuchAlgorithmException {Security.insertProviderAt (new com.sun.crypto .provider.sunjce (), 1); KeyGenerator generator = keygenerator.getinstance ("des"); generator.init (new securerandom ()); key key = generator.generateKey (); return key;}

/ ** * Encrypt the specified data according to the key provided * @Param Key Key * @Param Data Need to encrypt data * @return Byte [] encrypted data * @Throws util.encryptException * / public static Byte [] DOENCRYPT (Key Key, Byte [] DATA) throws EncryptException {Try {// Get a cipher object copher cipher = copher.getinstance ("des / ecb / pkcs5padding);

// encrypt copher.init (cipher.encrypt_mode, key); // Byte [] stringBytes = Amalgam.getbytes ("UTF8"); Byte [] Raw = Cipher.dofinal (data); // Base64encoder Encoder = new base64encoder ); // string base64 = encoder.Encode (RAW); Return Raw;} Catch (Exception E) {E.PrintStackTrace (); Throw new EncryptException ("Do Encrypt Occurs Exception. [" E.getMessage () " ] ");}} / ** * Decrypt the given encrypted data by decrypt the specified key * @Param Key key * @Param RAW data * @return Byte [] Decryption data * @throws util.EncryptException * / public static byte [] doDecrypt (Key key, byte [] raw) throws EncryptException {try {// Get a cipher object cipher cipher = Cipher.getInstance ( "DES / ECB / PKCS5Padding"); / / Decrypt cipher.init (cipher.decrypt_mode, key); // base64decoder decoder = new base64decoder (); // Byte [] RAW = DECODER.DECodeBuffer (data); Byte [] data = cipher.dofinal (RAW); // string result = new string (StringBytes, "UTF8"); //system.out.println ("the Decrypted Data IS: Result); Return Data;} Catch (Exception E) {E.PrintStackTrace (); throw new encryptException ("Do Decrypt Occurs Exception. [" E.GetMessage () "]");}}

/ ** * Get a key to password * @Param Key Key * @Param CipherMode password type * @Return Cipher * @Throws util.EncryptException When encryption occurs, an exception information is generated * / public static copher getCipher. Key key, int cipherMode) throws EncryptException {try {cipher cipher = Cipher.getInstance ( "DES / ECB / PKCS5Padding"); cipher.init (cipherMode, key); return cipher;} catch (Exception e) {e.printStackTrace ( ); throw new EncryptException ( ". Generate Cipher occurs Exception [" e.getMessage () "]");}} public static void main (String [] args) throws Exception {Key key = DESEncryptUtil.getKey (); file file = new file ( "test.html"); // just find documents issued in the current run directory FileInputStream in = new FileInputStream (file); ByteArrayOutputStream bout = new ByteArrayOutputStream (); byte [] tmpbuf = new byte [ 1024]; int count = 0; while (count = in.read (tmpbuf))! = -1) {Bout.write (TmpBuf, 0, count); TMPBUF = New byte [1024];

File = new file ("encrypt_result.dat"); OutputStream out = new fileoutputstream (file); out.write (); out.close ();

Byte [] Data = Desencryptutil.dodecrypt (key, raw); file = new file ("decrypt_result.html); out = new fileoutputstream (file); out.write (data); out.flush (); out.close ();

转载请注明原文地址:https://www.9cbs.com/read-22926.html

New Post(0)