標籤:TE nbsp pac key des ini turn dem encrypt
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); }}
java 對稱式加密