Symmetric encryption algorithm in java

xiaoxiao2021-03-06  25

Generate KEY and save it in the file:

KeyGenerator keyGen = KeyGenerator.getInstance ( "DESede"); Key key = keyGen.generateKey (); FileOutputStream out = new FileOutputStream ( "testsec.dat"); ObjectOutputStream os = new ObjectOutputStream (out); os.writeObject (key); Os.flush ();

Get the key from the file:

FileInputStream in = new fileinputstream ("testsec.dat"); ObjectInputStream INS = New ObjectInputStream (in); key key = (SecretKey) INS.READOBJECT ();

Encrypt with Key:

Cipher cipher = copher.getinstance ("deSede"); cipher.init (cipher.encrypt_mode, key); byte [] ciphertext = Cipher.dofinal ("I am Chinese" .getbytes ()); string s1 = new sun. Misc.base64encoder (). ENCODE (CIPHERTEXT);

Decryption with Key:

Cipher cipher = Cipher.getInstance ( "DESede"); cipher.init (Cipher.DECRYPT_MODE, key); String s2 = "fbpq7VuHRs / QtC1k4R0zxQ =="; byte [] b1 = new sun.misc.BASE64Decoder () decodeBuffer (. S2); byte [] b = cipher.dofinal (b1); string s = new string (b);

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

New Post(0)