- DSE
- package com.aarony.test;
- import java.io.IOException;
- import java.security.NoSuchAlgorithmException;
- import javax.crypto.Cipher;
- import javax.crypto.KeyGenerator;
- import javax.crypto.SecretKey;
- import javax.crypto.spec.SecretKeySpec;
- import sun.misc.BASE64Decoder;
- import sun.misc.BASE64Encoder;
- /**
- *
- * 此类描述的是: 对称加密
- *
- * @author: Aarony
- * @version: 2018 年 6 月 20 日 下午 9:20:34
- */
- public class SymmetricEncryptionDESDemo {
- /**
- *
- * 此方法描述的是: 解密
- *
- * @author: Aarony
- * @version: 2018 年 6 月 20 日 下午 9:30:52
- */
- public static byte[] decryptDES(byte[] bytes, SecretKey secretKey) throws Exception {
- Cipher cipher = Cipher.getInstance("AES");
- cipher.init(Cipher.DECRYPT_MODE, secretKey);
- return cipher.doFinal(bytes);
- }
- /**
- *
- * 此方法描述的是: 加密
- *
- * @author: Aarony
- * @version: 2018 年 6 月 20 日 下午 9:29:02
- */
- public static byte[] encryptDES(byte[] bytes, SecretKey secretKey) throws Exception {
- Cipher cipher = Cipher.getInstance("AES");
- cipher.init(Cipher.ENCRYPT_MODE, secretKey);
- return cipher.doFinal(bytes);
- }
- public static SecretKey loadKeyDES(String base64Key) throws IOException {
- byte[] bytes = base642byte(base64Key);
- SecretKey key = new SecretKeySpec(bytes, "AES");
- return key;
- }
- /**
- *
- * 此方法描述的是: 获取 base64 key
- *
- * @author: Aarony
- * @version: 2018 年 6 月 20 日 下午 9:25:36
- */
- public static String genKeyDES() throws NoSuchAlgorithmException {
- KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
- keyGenerator.init(56);
- SecretKey key = keyGenerator.generateKey();
- return byte2base64(key.getEncoded());
- }
- /**
- *
- * 此方法描述的是: base64 解码
- *
- * @author: Aarony
- * @version: 2018 年 6 月 20 日 下午 9:16:57
- */
- public static byte[] base642byte(String base64) throws IOException {
- BASE64Decoder decoder = new BASE64Decoder();
- return decoder.decodeBuffer(base64);
- }
- /**
- *
- * 此方法描述的是: base 64 编码
- *
- * @author: Aarony
- * @version: 2018 年 6 月 20 日 下午 9:15:14
- */
- public static String byte2base64(byte[] bytes) {
- BASE64Encoder base = new BASE64Encoder();
- return base.encode(bytes);
- }
- }
- AES
- package com.aarony.test;
- import java.io.IOException;
- import java.security.NoSuchAlgorithmException;
- import javax.crypto.Cipher;
- import javax.crypto.KeyGenerator;
- import javax.crypto.SecretKey;
- import javax.crypto.spec.SecretKeySpec;
- import sun.misc.BASE64Decoder;
- import sun.misc.BASE64Encoder;
- /**
- *
- * 此类描述的是: 对称加密
- *
- * @author: Aarony
- * @version: 2018 年 6 月 20 日 下午 9:20:34
- */
- public class SymmetricEncryptionAESDemo2 {
- /**
- *
- * 此方法描述的是: 解密
- *
- * @author: Aarony
- * @version: 2018 年 6 月 20 日 下午 9:30:52
- */
- public static byte[] decryptDES(byte[] bytes, SecretKey secretKey) throws Exception {
- Cipher cipher = Cipher.getInstance("DES");
- cipher.init(Cipher.DECRYPT_MODE, secretKey);
- return cipher.doFinal(bytes);
- }
- /**
- *
- * 此方法描述的是: 加密
- *
- * @author: Aarony
- * @version: 2018 年 6 月 20 日 下午 9:29:02
- */
- public static byte[] encryptDES(byte[] bytes, SecretKey secretKey) throws Exception {
- Cipher cipher = Cipher.getInstance("DES");
- cipher.init(Cipher.ENCRYPT_MODE, secretKey);
- return cipher.doFinal(bytes);
- }
- public static SecretKey loadKeyDES(String base64Key) throws IOException {
- byte[] bytes = base642byte(base64Key);
- SecretKey key = new SecretKeySpec(bytes, "DES");
- return key;
- }
- /**
- *
- * 此方法描述的是: 获取 base64 key
- *
- * @author: Aarony
- * @version: 2018 年 6 月 20 日 下午 9:25:36
- */
- public static String genKeyDES() throws NoSuchAlgorithmException {
- KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
- keyGenerator.init(128);
- SecretKey key = keyGenerator.generateKey();
- return byte2base64(key.getEncoded());
- }
- /**
- *
- * 此方法描述的是: base64 解码
- *
- * @author: Aarony
- * @version: 2018 年 6 月 20 日 下午 9:16:57
- */
- public static byte[] base642byte(String base64) throws IOException {
- BASE64Decoder decoder = new BASE64Decoder();
- return decoder.decodeBuffer(base64);
- }
- /**
- *
- * 此方法描述的是: base 64 编码
- *
- * @author: Aarony
- * @version: 2018 年 6 月 20 日 下午 9:15:14
- */
- public static String byte2base64(byte[] bytes) {
- BASE64Encoder base = new BASE64Encoder();
- return base.encode(bytes);
- }
- }
来源: http://www.bubuko.com/infodetail-2652420.html