[实验任务一]:加密算法
目前常用的加密算法有DES(Data Encryption Standard)和IDEA(International Data Encryption Algorithm)国际数据加密算法等,请用工厂方法实现加密算法系统。
实验要求:
1. 画出对应的类图;
2. 提交该系统的代码,该系统务必是一个可以能够直接使用的系统,查阅资料完成相应加密算法的实现;
package org.example; /* * Copyright 2001-2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.IOException; import javax.portlet.PortletConfig; import javax.portlet.GenericPortlet; import javax.portlet.PortletException; import javax.portlet.PortletRequestDispatcher; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import javax.portlet.WindowState; public class MyPortlet extends GenericPortlet { private static final String NORMAL_VIEW = "/normal.jsp"; private static final String MAXIMIZED_VIEW = "/maximized.jsp"; private static final String HELP_VIEW = "/help.jsp"; private PortletRequestDispatcher normalView; private PortletRequestDispatcher maximizedView; private PortletRequestDispatcher helpView; public void doView( RenderRequest request, RenderResponse response ) throws PortletException, IOException { if( WindowState.MINIMIZED.equals( request.getWindowState() ) ) { return; } if ( WindowState.NORMAL.equals( request.getWindowState() ) ) { normalView.include( request, response ); } else { maximizedView.include( request, response ); } } protected void doHelp( RenderRequest request, RenderResponse response ) throws PortletException, IOException { helpView.include( request, response ); } public void init( PortletConfig config ) throws PortletException { super.init( config ); normalView = config.getPortletContext().getRequestDispatcher( NORMAL_VIEW ); maximizedView = config.getPortletContext().getRequestDispatcher( MAXIMIZED_VIEW ); helpView = config.getPortletContext().getRequestDispatcher( HELP_VIEW ); } public void destroy() { normalView = null; maximizedView = null; helpView = null; super.destroy(); } }
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 codeStringBegin) {
String codeStringEnd=null; //加密后的密文
String decodeString=null; //密文解密后得到的明文
System.out.println("要加密的明文:20223867 尹丹彤"+codeStringBegin);
String cipherType = "DESede"; //加密算法类型,可设置为DES、DESede、AES等字符串
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("要加密的字符串是:"+ codeStringBegin);
byte[] codeStringByte=codeStringBegin.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("");
codeStringEnd=new String(codeStringByteEnd);
System.out.println("加密后的字符串是:" + codeStringEnd);
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("");
decodeString=new String(decodeStringByteEnd);
System.out.println("解密后的字符串是:" + decodeString);
System.out.println("");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
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.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
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 key 二进制密钥
* @return Key 密钥
* */
private static Key toKey(byte[] key) throws Exception{
//实例化DES密钥
//生成密钥
SecretKey secretKey=new SecretKeySpec(key,KEY_ALGORITHM);
return secretKey;
}
/**
* 加密数据
* @param data 待加密数据
* @param key 密钥
* @return byte[] 加密后的数据
* */
private static byte[] encrypt(byte[] data,byte[] key) throws Exception{
//加入bouncyCastle支持
Security.addProvider(new BouncyCastleProvider());
//还原密钥
Key k=toKey(key);
//实例化
Cipher cipher=Cipher.getInstance(CIPHER_ALGORITHM);
//初始化,设置为加密模式
cipher.init(Cipher.ENCRYPT_MODE, k);
//执行操作
return cipher.doFinal(data);
}
/**
* 解密数据
* @param data 待解密数据
* @param key 密钥
* @return byte[] 解密后的数据
* */
private static byte[] decrypt(byte[] data,byte[] key) throws Exception{
//加入bouncyCastle支持
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,String data) {
String key = getKey();
System.out.println("要加密的原文:"+data);
System.out.println("密钥:" + key);
String data_en = ideaEncrypt(data, key);
System.out.println("密文:"+data_en);
String data_de = ideaDecrypt(data_en, key);
System.out.println("原文:"+data_de);
}
}
mport java.util.Scanner;
public class Main {
public static void main(String[] args) {
DES des=new DES();
IDEA idea=new IDEA();
try {
int n = 0;
Scanner in = new Scanner(System.in);
while (n != 3) {
System.out.println("请选择要使用的加密算法 1.DES加密算法 2.IDEA加密算法");
System.out.println("3.退出");
System.out.println("请选择:");
if (in.hasNextInt()) {
n = in.nextInt();
} else {
System.out.println("输入的不是整数,请重新输入:");
continue;
}
switch (n) {
case 1: {
String a;
Scanner in1 = new Scanner(System.in);
System.out.println("选择加密文件:");
a = in1.nextLine();
des.work("1787878787878787","0E329232EA6D0D73",a);
break;
}
case 2: {
String b;
Scanner in2 = new Scanner(System.in);
System.out.println("选择加密文件:");
b = in2.nextLine();
idea.work("8787878787878787","0E329232EA6D0D73",b);
break;
}
}
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
public interface Method {
public abstract void work(String str,String password,String codeStringBegin);
}
3.注意编程规范。
« 上一篇:
10.22
» 下一篇:
10.24
posted @
2024-10-25 15:25
席
阅读(
5 )
评论()
编辑
收藏
举报
点击右上角即可分享
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统