Java Security secret key

Source: Internet
Author: User
Tags decrypt object object readfile

In Java, the abstract interface of secret key is Javax.crypto.SecretKey, its algorithm type is symmetric encryption algorithm, the main characteristic of symmetric encryption algorithm is encryption and decryption is the same key, symmetric encryption algorithm mainly: Des,desede,aes, BLOWFISH,RC2,RC4 and so on. Here is an example of use:

Package Com.xtayfjpk.security;import Java.io.bytearrayoutputstream;import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.objectinputstream;import Java.io.objectoutputstream;import Java.security.spec.keyspec;import Javax.crypto.cipher;import Javax.crypto.keygenerator;import Javax.crypto.secretkey;import Javax.crypto.secretkeyfactory;import Javax.crypto.spec.deskeyspec;import Org.junit.test;public class Secretkeytest {//symmetric encryption algorithm, Java default supported algorithms are: AES, Arcfour, Blowfish, DES, Desede, HmacMD5,//  HmacSHA1 HmacSHA256 HmacSHA384 HmacSHA512, rc2private static final String algorithm = "DES"; @Testpublic void Testencrypt () Throws Exception {//Get Add/decryption device cipher cipher = cipher.getinstance (algorithm);//Generate secret key Secretkey key = Keygenerator.getinstance (algorithm). GenerateKey ()//will be secretly written to the file Writesecretkey ("Xtayfjpk.key", key, false);//encryption, Must be initialized to cryptographic mode Cipher.init (Cipher.encrypt_mode, key); String MyInfo = "my plaintext";//encryption byte[] results = cipher.dofinal (Myinfo.getbytes ()); WriteFile ("Content.dat", results);} @Testpublic void Testdecrypt () throws Exception {//Read secret key Secretkey key = Readsecretkey ("Xtayfjpk.key", false);// Read encrypted data byte[] contents = ReadFile ("Content.dat"); Cipher Cipher = cipher.getinstance (algorithm);//decryption must be initialized to decrypt mode Cipher.init (Cipher.decrypt_mode, key);//decrypt, get plaintext byte[ ] Origins = cipher.dofinal (contents); System.out.println (New String (Origins));} /** reads the contents of the file into a byte array **/public byte[] readFile (String path) throws Exception {FileInputStream cntinput = new FileInputStream (p ATH); Bytearrayoutputstream BAOs = new Bytearrayoutputstream (), int b = -1;while ((B=cntinput.read ())!=-1) {baos.write (b);} Cntinput.close (); byte[] contents = Baos.tobytearray (); Baos.close (); return contents;} /** writing binary data to a file **/public void WriteFile (String path, byte[] content) throws Exception {FileOutputStream fos = new FILEOUTP Utstream (path); fos.write (content); Fos.close ();} Public Secretkey Readsecretkey (String path, Boolean encoded) throws Exception {if (encoded) {byte[] KeyData = readFile (path ); KeySpec KeySpec = new DESKEYSPEC (KeyData); When you read the secret key encoding data, you must use this method to generate the secret key//java the document, although it is clearly stated that Secretkeyfactory.getinstance supports the AES algorithm, but in fact does not support (JDK7) Secretkey key = Secretkeyfactory.getinstance (algorithm). Generatesecret (KeySpec); return key;} else {FileInputStream FIS = new FileInputStream (path); ObjectInputStream bis = new ObjectInputStream (FIS); object object = b Is.readobject (); Bis.close (); return (Secretkey) object;}} There are two ways to write a/** secret key, one is to take advantage of the serialization mechanism of the Java object and to get its encoded data (byte data) to write to the file **/public void Writesecretkey (String path, Secretkey secretkey , Boolean encoded) throws Exception {FileOutputStream fos = new FileOutputStream (path), if (encoded) {Fos.write ( Secretkey.getencoded ()); Fos.close ();} else {ObjectOutputStream Oos = new ObjectOutputStream (FOS); Oos.writeobject (Secretkey); Oos.close ();}}}

When using a secret key, the communication process for both parties is assumed to be a two-party, previously agreed secret key algorithm:
1. Party Building key
2. By the key Builder Party A will announce the key to party B
3. The message sender encrypts the data using a key
4. Send the encrypted data to the message receiver
5. The message recipient uses the key to decrypt the data to obtain clear text

Java Security secret key

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.