软件设计 石家庄铁道大学信息学院

实验3:工厂方法模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解工厂方法模式的动机,掌握该模式的结构;
2、能够利用工厂方法模式解决实际问题。

[实验任务一]:加密算法
目前常用的加密算法有DES(Data Encryption Standard)和IDEA(International Data Encryption Algorithm)国际数据加密算法等,请用工厂方法实现加密算法系统。
实验要求:
1.画出对应的类图;
2.提交该系统的代码,该系统务必是一个可以能够直接使用的系统,查阅资料完成相应加密算法的实现;
3.注意编程规范。

1.Client.java

package org.example;

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 {
    Security.addProvider(new BouncyCastleProvider());

    KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
    kg.init(128);
    SecretKey secretKey = kg.generateKey();
    return secretKey.getEncoded();
}


private static Key toKey(byte[] key) throws Exception {
    SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);
    return secretKey;
}


private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
    Security.addProvider(new BouncyCastleProvider());
    Key k = toKey(key);
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    cipher.init(Cipher.ENCRYPT_MODE, k);
    return cipher.doFinal(data);
}


private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
    Security.addProvider(new BouncyCastleProvider());
    Key k = toKey(key);
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    cipher.init(Cipher.DECRYPT_MODE, k);
    return cipher.doFinal(data);
}

public static String getKey() {
    String result = null;
    try {
        result = Base64.encodeBase64String(initkey());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
public static String ideaEncrypt(String data, String key) {
    String result = null;
    try {
        byte[] data_en = encrypt(data.getBytes(), Base64.decodeBase64(key));
        result = Base64.encodeBase64String(data_en);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
public static String ideaDecrypt(String data, String key) {
    String result = null;
    try {
        byte[] data_de = decrypt(Base64.decodeBase64(data), Base64.decodeBase64(key));
        ;
        result = new String(data_de);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
public void work(String str, String password) {
    System.out.println("要加密的原文:" + str);
    System.out.println("密钥:" + password);
    String data_en = ideaEncrypt(str, password);
    System.out.println("密文:" + data_en);
    String data_de = ideaDecrypt(data_en, password);
    System.out.println("原文:" + data_de);
}

}

2.DES.java
package org.example;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class DES implements Method{
public void work(String str, String password) {
String endcode = null;
String opencode = null;
System.out.println("要加密的明文:" + str);
String cipherType = "DESede";
try {
KeyGenerator keyGen = KeyGenerator.getInstance(cipherType);
keyGen.init(112);
SecretKey key = keyGen.generateKey();
byte[] keyByte = key.getEncoded();
System.out.print("密钥是:");
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);
byte[] codeStringByte = str.getBytes("UTF8");
System.out.print("要加密的字符串对应的字节码是:");
for (int i = 0; i < codeStringByte.length; i++) {
System.out.print(codeStringByte[i] + ",");
}
System.out.println();
byte[] codeStringByteEnd = cp.doFinal(codeStringByte);
System.out.print("加密后的字符串对应的字节码是:");
for (int i = 0; i < codeStringByteEnd.length; i++) {
System.out.print(codeStringByteEnd[i] + ",");
}
System.out.println();
endcode = new String(codeStringByteEnd);
System.out.println("加密后的字符串是:" + endcode);
cp.init(Cipher.DECRYPT_MODE, key);
byte[] decodeStringByteEnd = cp.doFinal(codeStringByteEnd);
System.out.print("解密后的字符串对应的字节码是:");
for (int i = 0; i < decodeStringByteEnd.length; i++) {
System.out.print(decodeStringByteEnd[i] + ",");
}
System.out.println();
opencode = new String(decodeStringByteEnd);
System.out.println("解密后的字符串是:" + opencode);
} catch (Exception e) {
e.printStackTrace();
}
}

}

3.DESFactory.java
package org.example;

public class DESFactory implements MethodFactory {
public DES produceMethod() {
return new DES();
}
}

4.IDEA.java
package org.example;

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 {
    Security.addProvider(new BouncyCastleProvider());
    KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
    kg.init(128);
    SecretKey secretKey = kg.generateKey();
    return secretKey.getEncoded();
}


private static Key toKey(byte[] key) throws Exception {
    SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);
    return secretKey;
}


private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
    Security.addProvider(new BouncyCastleProvider());
    Key k = toKey(key);
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    cipher.init(Cipher.ENCRYPT_MODE, k);
    return cipher.doFinal(data);
}


private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
    Security.addProvider(new BouncyCastleProvider());
    Key k = toKey(key);
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    cipher.init(Cipher.DECRYPT_MODE, k);
    return cipher.doFinal(data);
}

public static String getKey() {
    String result = null;
    try {
        result = Base64.encodeBase64String(initkey());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

public static String ideaEncrypt(String data, String key) {
    String result = null;
    try {
        byte[] data_en = encrypt(data.getBytes(), Base64.decodeBase64(key));
        result = Base64.encodeBase64String(data_en);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

public static String ideaDecrypt(String data, String key) {
    String result = null;
    try {
        byte[] data_de = decrypt(Base64.decodeBase64(data), Base64.decodeBase64(key));
        ;
        result = new String(data_de);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

public void work(String str, String password) {
    System.out.println("要加密的原文:" + str);
    System.out.println("密钥:" + password);
    String data_en = ideaEncrypt(str, password);
    System.out.println("密文:" + data_en);
    String data_de = ideaDecrypt(data_en, password);
    System.out.println("原文:" + data_de);
}

}

5.IDEAFactory.java
package org.example;

public class IDEAFactory implements MethodFactory {
public IDEA produceMethod() {
return new IDEA();
}
}

6.Method.java
package org.example;

public interface Method {
public void work(String str, String password);

}
7.Method.java
package org.example;

public interface MethodFactory {
public Method produceMethod();

}

posted on 2024-10-11 20:03  许七安gyg  阅读(5)  评论(0编辑  收藏  举报
$(document).ready(function() { // 禁止右键 $(document).bind("contextmenu", function(){return false;}); // 禁止选择 $(document).bind("selectstart", function(){return false;}); // 禁止Ctrl+C 和Ctrl+A $(document).keydown(function(event) { if ((event.ctrlKey&&event.which==67) || (event.ctrlKey&&event.which==86)) { //alert("对不起,版权所有,禁止复制"); return false; } }); });