Java encryption and decryption des symmetric encryption algorithm

Source: Internet
Author: User

1 The following is a simple example of a des symmetric encryption algorithm (setting a key and then encrypting all the data). 2 3 first, generate a key key. 4 I saved it in the key.txt. This file is like a key. Whoever owns it will be able to unlock our class files. The code reference is as follows:5  PackageCom.neusoft.jiami;6 ImportJava.io.File;7 ImportJava.io.FileOutputStream;8 ImportJava.security.SecureRandom;9 ImportJavax.crypto.KeyGenerator;Ten ImportJavax.crypto.SecretKey; One classKey { A     PrivateString KeyName; -      PublicKey (String keyName) { -          This. KeyName =KeyName; the      } -      Public voidCreateKey (String keyName)throwsException { -         //To create a trusted source of random numbers, the DES algorithm requires -SecureRandom sr =Newsecurerandom (); +         //creating a Keygenerator object with the DES algorithm -keygenerator kg = keygenerator.getinstance ("DES"); +         //Initialize this key generator so that it has a determined key length A Kg.init (SR); at         //Generate key -Secretkey key =Kg.generatekey (); -         //Get Key Data -         byteRawkeydata[] =key.getencoded (); -         //saves the obtained key data to a file for use when decrypting -FileOutputStream fo =NewFileOutputStream (NewFile (KeyName)); in Fo.write (rawkeydata); -      } to      Public Static voidMain (String args[]) { +         Try { -             NewKey ("Key.txt"); the}Catch(Exception e) { * e.printstacktrace (); $          }Panax Notoginseng      } - } the The second step is to encrypt the class file that we want to encrypt.  + For example, I have a digestpass class that has been properly compiled to generate Digestpass.class files. At this point, the class file is available to anyone. Because the class loader of the system can load it automatically. So the next step, all we have to do is encrypt this type of file. Makes the system's ClassLoader unreadable to the correct bytecode file. The reference code is as follows: A  PackageCom.neusoft.jiami; the ImportJava.io.File; + ImportJava.io.FileInputStream; - ImportJava.io.FileOutputStream; $ ImportJava.security.SecureRandom; $ ImportJavax.crypto.Cipher; - ImportJavax.crypto.SecretKey; - Importjavax.crypto.SecretKeyFactory; the ImportJavax.crypto.spec.DESKeySpec; -  Public classJiami {Wuyi      Public Static voidMain (string[] args)throwsException { the         //The DES algorithm requires a trustworthy random number source -SecureRandom sr =Newsecurerandom (); Wu         //Get Key Data -FileInputStream fi =NewFileInputStream (NewFile ("Key.txt")); About         byteRawkeydata[] =New byte[Fi.available ()]; $ Fi.read (rawkeydata); - fi.close (); -         //Create a Deskeyspec object from the original key data -Deskeyspec DKs =NewDeskeyspec (rawkeydata); A         //Create a key factory and use it to convert Deskeyspec to a Secretkey object +Secretkey key = Secretkeyfactory.getinstance ("DES"). Generatesecret (DKS); the         //The cipher object actually completes the cryptographic operation -Cipher Cipher = cipher.getinstance ("DES"); $         //Initialize the Cipher object with a key the Cipher.init (Cipher.encrypt_mode, Key, SR); the         //now, get the file data that you want to encrypt theFileInputStream Fi2 =NewFileInputStream (NewFile ("Digestpass.class")); the         byteData[] =New byte[Fi2.available ()]; - fi2.read (data); in fi2.close (); the         //formally perform cryptographic operations the         byteEncrypteddata[] =cipher.dofinal (data); About         //overwrite the original file with encrypted data theFileOutputStream fo =NewFileOutputStream (NewFile ("Digestpass.class")); the Fo.write (EncryptedData); the fo.close (); +      } - } the The third step is to load with a custom ClassLoader. The reference code is as follows:Bayi  PackageCom.neusoft.jiami; the ImportJava.io.File; the ImportJava.io.FileInputStream; - ImportJava.io.FileOutputStream; - ImportJava.security.SecureRandom; the ImportJavax.crypto.Cipher; the ImportJavax.crypto.SecretKey; the Importjavax.crypto.SecretKeyFactory; the ImportJavax.crypto.spec.DESKeySpec; - ImportCom.neusoft.classloader.MyClassLoader; the  Public classJiemi { the      Public Static voidMain (string[] args)throwsException { the         //The DES algorithm requires a trustworthy random number source94SecureRandom sr =Newsecurerandom (); the         //Get Key Data theFileInputStream fi =NewFileInputStream (NewFile ("Key.txt")); the         byteRawkeydata[] =New byte[Fi.available ()];//= new Byte[5];98 Fi.read (rawkeydata); About fi.close (); -         //Create a Deskeyspec object from the original key data101Deskeyspec DKs =NewDeskeyspec (rawkeydata);102         //Create a key factory and use it to convert the Deskeyspec object to a Secretkey object103Secretkey key = Secretkeyfactory.getinstance ("DES"). Generatesecret (DKS);104         //The cipher object actually completes the decryption operation theCipher Cipher = cipher.getinstance ("DES");106         //Initialize the Cipher object with a key107 Cipher.init (Cipher.decrypt_mode, Key, SR);108         //now, get the data and decrypt109FileInputStream Fi2 =NewFileInputStream (NewFile ("Digestpass.class")); the         byteEncrypteddata[] =New byte[Fi2.available ()];111 Fi2.read (EncryptedData); the fi2.close ();113         //formally perform decryption operations the         byteDecrypteddata[] =cipher.dofinal (EncryptedData); the         //the data is then restored to the original class file the         //fileoutputstream fo = new FileOutputStream (New117         //File ("Digestpass.class"));118         //Fo.write (decrypteddata);119         //load the class with decrypted data and apply -Myclassloader MCL =NewMyclassloader ("e:/");121Class cl = Mcl.loadclass (Decrypteddata, "Com.neusoft.jiami.DigestPass");122Digestpass DP =cl.newinstance ();123      }124}

Java encryption and decryption des symmetric encryption algorithm

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.