软件设计——工厂方法模式之加密算法 1、类图
2、源代码 2.1 结构目录
2.2 所需jar包 这里分享几个下载 jar 包的网址: http://www.mvnrepository.com/ http://mvnrepository.com/ http://findjar.com http://sourceforge.net/ 注:将 jar 包放入 lib 文件夹后要进行构建路径 2.3 MethodFactory.java (抽象接口) package test3; /** * 抽象工厂类 * @author dell * */ public interface MethodFactory { public Method produceMethod(); } 2.4 DESFactory.java package test3; /** * 具体工厂类DES * @author dell * */ public class DESFactory implements MethodFactory { public DES produceMethod() { System.out.println("使用DES算法"); return new DES(); } } 2.5 IDEAFactory.java package test3; /** * 具体工厂类IDEA * @author dell * */ public class IDEAFactory implements MethodFactory { public IDEA produceMethod() { System.out.println("使用IDEA算法"); return new IDEA(); } } 2.6 Method.java(抽象接口) package test3; /** * 抽象方法类 * @author dell * */ public interface Method { public void work(String str, String password); } 2.7 DES.java package test3; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; /** * 具体方法类DES * @author dell * */ public class DES implements Method{ public void work(String str, String password) { String begincode = "人生苦短及时行乐"; // 要加密的明文 String endcode = null; // 加密后的密文 String opencode = null; // 密文解密后得到的明文 System.out.println("要加密的明文:" + begincode); String cipherType = "DESede"; // 加密算法类型,可设置为DES(56)、DESede(112)、AES等字符串(128) try { // 获取密钥生成器 KeyGenerator keyGen = KeyGenerator.getInstance(cipherType); // 初始化密钥生成器,不同的加密算法其密钥长度可能不同 keyGen.init(112); // 生成密钥 SecretKey key = keyGen.generateKey(); // 得到密钥字节码 byte[] keyByte = key.getEncoded(); // 输出密钥的字节码 System.out.println("密钥是:"); for (int i = 0; i < keyByte.length; i++) { System.out.print(keyByte[i] + ","); } System.out.println(""); // 创建密码器 Cipher cp = Cipher.getInstance(cipherType); // 初始化密码器 cp.init(Cipher.ENCRYPT_MODE, key); System.out.println("要加密的字符串是:" + begincode); byte[] codeStringByte = begincode.getBytes("UTF8"); System.out.println("要加密的字符串对应的字节码是:"); for (int i = 0; i < codeStringByte.length; i++) { System.out.print(codeStringByte[i] + ","); } System.out.println(""); // 开始加密 byte[] codeStringByteEnd = cp.doFinal(codeStringByte); System.out.println("加密后的字符串对应的字节码是:"); for (int i = 0; i < codeStringByteEnd.length; i++) { System.out.print(codeStringByteEnd[i] + ","); } System.out.println(""); endcode = new String(codeStringByteEnd); System.out.println("加密后的字符串是:" + endcode); System.out.println(""); // 重新初始化密码器 cp.init(Cipher.DECRYPT_MODE, key); // 开始解密 byte[] decodeStringByteEnd = cp.doFinal(codeStringByteEnd); System.out.println("解密后的字符串对应的字节码是:"); for (int i = 0; i < decodeStringByteEnd.length; i++) { System.out.print(decodeStringByteEnd[i] + ","); } System.out.println(""); opencode = new String(decodeStringByteEnd); System.out.println("解密后的字符串是:" + opencode); System.out.println(""); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("DES加密算法"); DES des = new DES(); try { des.work("8787878787878787", "0E329232EA6D0D73"); } catch (Exception e) { System.out.println(e.getMessage()); } } } 2.8 IDEA.java package test3; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import org.apache.commons.codec.binary.Base64; import org.bouncycastle.jce.provider.BouncyCastleProvider; import javax.crypto.spec.SecretKeySpec; import java.security.Key; import java.security.Security; public class IDEA implements Method { public static final String KEY_ALGORITHM = "IDEA"; public static final String CIPHER_ALGORITHM = "IDEA/ECB/ISO10126Padding"; public static byte[] initkey() throws Exception { // 加入bouncyCastle支持 Security.addProvider(new BouncyCastleProvider()); // 实例化密钥生成器 KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM); // 初始化密钥生成器,IDEA要求密钥长度为128位 kg.init(128); // 生成密钥 SecretKey secretKey = kg.generateKey(); // 获取二进制密钥编码形式 return secretKey.getEncoded(); } /** * 转换密钥 * * @param 标签:String,软件设计,System,10.18,密钥,println,public,加密算法,out From: https://www.cnblogs.com/kongxiangzeng/p/18658606