Java symmetric encryption

Source: Internet
Author: User

DSE

 Packagecom.aarony.test;Importjava.io.IOException;Importjava.security.NoSuchAlgorithmException;ImportJavax.crypto.Cipher;ImportJavax.crypto.KeyGenerator;ImportJavax.crypto.SecretKey;ImportJavax.crypto.spec.SecretKeySpec;ImportSun.misc.BASE64Decoder;ImportSun.misc.BASE64Encoder;/*** * This class describes: Symmetric encryption * *@author: Aarony *@version: June 20, 2018 PM 9:20:34*/ Public classSymmetricencryptiondesdemo {/*** * This method describes: decryption * *@author: Aarony *@version: June 20, 2018 PM 9:30:52*/     Public Static byte[] Decryptdes (byte[] bytes, Secretkey secretkey)throwsException {Cipher Cipher= Cipher.getinstance ("AES");        Cipher.init (Cipher.decrypt_mode, Secretkey); returncipher.dofinal (bytes); }    /*** * This method describes: Encryption * *@author: Aarony *@version: June 20, 2018 PM 9:29:02*/     Public Static byte[] Encryptdes (byte[] bytes, Secretkey secretkey)throwsException {Cipher Cipher= Cipher.getinstance ("AES");        Cipher.init (Cipher.encrypt_mode, Secretkey); returncipher.dofinal (bytes); }     Public StaticSecretkey loadkeydes (String base64key)throwsIOException {byte[] bytes =Base642byte (Base64key); Secretkey Key=NewSecretkeyspec (Bytes, "AES"); returnkey; }    /*** * This method describes: Get base64 key * *@author: Aarony *@version: June 20, 2018 PM 9:25:36*/     Public StaticString Genkeydes ()throwsnosuchalgorithmexception {keygenerator keygenerator= Keygenerator.getinstance ("AES"); Keygenerator.init (56); Secretkey Key=Keygenerator.generatekey (); returnbyte2base64 (key.getencoded ()); }    /*** * This method describes: Base64 decoding * *@author: Aarony *@version: June 20, 2018 PM 9:16:57*/     Public Static byte[] Base642byte (String base64)throwsIOException {Base64decoder decoder=NewBase64decoder (); returnDecoder.decodebuffer (base64); }    /*** * This method describes: Base 64 encoding * *@author: Aarony *@version: June 20, 2018 PM 9:15:14*/     Public StaticString Byte2base64 (byte[] bytes) {Base64encoder base=NewBase64encoder (); returnBase.encode (bytes); }}

Aes

 Packagecom.aarony.test;Importjava.io.IOException;Importjava.security.NoSuchAlgorithmException;ImportJavax.crypto.Cipher;ImportJavax.crypto.KeyGenerator;ImportJavax.crypto.SecretKey;ImportJavax.crypto.spec.SecretKeySpec;ImportSun.misc.BASE64Decoder;ImportSun.misc.BASE64Encoder;/*** * This class describes: Symmetric encryption * *@author: Aarony *@version: June 20, 2018 PM 9:20:34*/ Public classSymmetricEncryptionAESDemo2 {/*** * This method describes: decryption * *@author: Aarony *@version: June 20, 2018 PM 9:30:52*/     Public Static byte[] Decryptdes (byte[] bytes, Secretkey secretkey)throwsException {Cipher Cipher= Cipher.getinstance ("DES");        Cipher.init (Cipher.decrypt_mode, Secretkey); returncipher.dofinal (bytes); }    /*** * This method describes: Encryption * *@author: Aarony *@version: June 20, 2018 PM 9:29:02*/     Public Static byte[] Encryptdes (byte[] bytes, Secretkey secretkey)throwsException {Cipher Cipher= Cipher.getinstance ("DES");        Cipher.init (Cipher.encrypt_mode, Secretkey); returncipher.dofinal (bytes); }     Public StaticSecretkey loadkeydes (String base64key)throwsIOException {byte[] bytes =Base642byte (Base64key); Secretkey Key=NewSecretkeyspec (Bytes, "DES"); returnkey; }    /*** * This method describes: Get base64 key * *@author: Aarony *@version: June 20, 2018 PM 9:25:36*/     Public StaticString Genkeydes ()throwsnosuchalgorithmexception {keygenerator keygenerator= Keygenerator.getinstance ("DES"); Keygenerator.init (128); Secretkey Key=Keygenerator.generatekey (); returnbyte2base64 (key.getencoded ()); }    /*** * This method describes: Base64 decoding * *@author: Aarony *@version: June 20, 2018 PM 9:16:57*/     Public Static byte[] Base642byte (String base64)throwsIOException {Base64decoder decoder=NewBase64decoder (); returnDecoder.decodebuffer (base64); }    /*** * This method describes: Base 64 encoding * *@author: Aarony *@version: June 20, 2018 PM 9:15:14*/     Public StaticString Byte2base64 (byte[] bytes) {Base64encoder base=NewBase64encoder (); returnBase.encode (bytes); }}

Java symmetric encryption

Related Article

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.