分组密码加密.docx

上传人:b****3 文档编号:3850516 上传时间:2022-11-25 格式:DOCX 页数:14 大小:37.85KB
下载 相关 举报
分组密码加密.docx_第1页
第1页 / 共14页
分组密码加密.docx_第2页
第2页 / 共14页
分组密码加密.docx_第3页
第3页 / 共14页
分组密码加密.docx_第4页
第4页 / 共14页
分组密码加密.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

分组密码加密.docx

《分组密码加密.docx》由会员分享,可在线阅读,更多相关《分组密码加密.docx(14页珍藏版)》请在冰豆网上搜索。

分组密码加密.docx

分组密码加密

实习二分组密码加密

一、实习目的

1.理解对称加密算法的原理,熟悉常用的对称加密算法:

DES、TripleDES、Blowfish;

2.以DES加密算法为例,掌握分组加密算法加、解密过程的实现。

二、实习要求

1.实习前认真预习第5章有关内容;

2.熟悉java中的java.security.*和java.crypto.*中的相关类;

3.按要求认真撰写实习报告。

三、实习内容

1.[基本要求]

以DES/DESede为例,利用java中的相关类实现对指定字符串的加、解密。

2.[实现提示]

(1)可以利用java中的KeyGenerator类创建对称秘钥,利用工厂类KeyGenerator的静态方法getInstance()获得KeyGenerator()类对象;

(2)方法getInstance()的参数为字符串类型,指定加密算法的名称如:

Blowfish、DES、DESede、HmacMD5或HmacSHA1等;

(3)利用工厂类Cipher的对象可以创建密码器。

同样的,getInstance()的参数为字符串类型,指定加密算法的名称。

实验截图:

以DES加密为例,客户端:

客户端解密:

实习代码:

服务器MyServer类:

packageCaesar_Modification;

importjava.awt.EventQueue;

importjava.awt.TextArea;

importjavax.crypto.BadPaddingException;

importjavax.crypto.Cipher;

importjavax.crypto.IllegalBlockSizeException;

importjavax.crypto.NoSuchPaddingException;

importjavax.swing.JFrame;

importjava.awt.Color;

importjavax.swing.JButton;

importjavax.swing.JLabel;

importsun.misc.BASE64Decoder;

importjava.awt.event.ActionListener;

importjava.awt.event.ActionEvent;

importjava.io.BufferedReader;

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.InputStreamReader;

importjava.io.ObjectInputStream;

import.ServerSocket;

import.Socket;

importjava.security.InvalidKeyException;

importjava.security.Key;

importjava.security.NoSuchAlgorithmException;

publicclassMyServer{

privateJFrameframe;

privatestaticServerSocketserver_text;

privatestaticSocketyou_text;

privatestaticServerSocketserver_key;

privatestaticSocketyou_key;

privatestaticTextAreaSDealWith_Before;

privatestaticTextAreaSDealWith_After;

publicstaticvoidmain(String[]args){

EventQueue.invokeLater(newRunnable(){

publicvoidrun(){

try{

MyServerwindow=newMyServer();

window.frame.setVisible(true);

}catch(Exceptione){

e.printStackTrace();

}

}

});

}

publicMyServer()throwsIOException{

initialize();

}

privatevoidinitialize()throwsIOException{

frame=newJFrame();

frame.getContentPane().setBackground(Color.LIGHT_GRAY);

frame.setTitle("服务器端");

frame.setBounds(100,100,450,300);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().setLayout(null);

finalTextAreaSDealWith_After=newTextArea();

SDealWith_After.setBounds(227,103,150,115);

frame.getContentPane().add(SDealWith_After);

JLabellblNewLabel=newJLabel("接收到的秘钥");

lblNewLabel.setBounds(227,10,93,15);

frame.getContentPane().add(lblNewLabel);

JLabellabel_1=newJLabel("处理后");

label_1.setBounds(227,78,54,15);

frame.getContentPane().add(label_1);

JButtonbutton_1=newJButton("清除");

button_1.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

SDealWith_After.setText("");

SDealWith_Before.setText("");

}

});

button_1.setBounds(269,228,93,23);

frame.getContentPane().add(button_1);

finalTextAreaPri_KeyS=newTextArea();

Pri_KeyS.setBounds(227,27,150,43);

frame.getContentPane().add(Pri_KeyS);

finalTextAreaSDealWith_Before=newTextArea();

SDealWith_Before.setBounds(10,103,156,115);

frame.getContentPane().add(SDealWith_Before);

JButtonbtnNewButton_1=newJButton("开启秘钥通道");

btnNewButton_1.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

try{

server_key=newServerSocket(5679);

you_key=server_key.accept();

Pri_KeyS.setText("秘钥已传送。

");

}catch(IOExceptione1){

e1.printStackTrace();

}

}

});

btnNewButton_1.setBounds(10,6,150,23);

frame.getContentPane().add(btnNewButton_1);

JButtonbtnNewButton=newJButton("解密");

btnNewButton.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

try{

InputStreamin=you_text.getInputStream();

InputStreamis=you_key.getInputStream();

ObjectInputStreamb=newObjectInputStream(is);

Keyk1=(Key)b.readObject();

b.close();

inttotal_ctext=in.available();

byte[]ctext=newbyte[total_ctext];

byte[]ptext=newbyte[total_ctext];

Ciphercp=Cipher.getInstance("DES");

cp.init(Cipher.DECRYPT_MODE,k1);

BufferedReaderbr=newBufferedReader(newInputStreamReader(in));

Stringcstring=br.readLine();

BASE64Decoderbase64decoder=newBASE64Decoder();

ctext=base64decoder.decodeBuffer(cstring);

ptext=cp.doFinal(ctext);

Stringp=newString(ptext,"UTF8");

SDealWith_Before.setText(cstring);

SDealWith_After.setText(p);

}catch(IOExceptione2){

e2.printStackTrace();

}catch(NoSuchAlgorithmExceptione1){

//TODOAuto-generatedcatchblock

e1.printStackTrace();

}catch(NoSuchPaddingExceptione1){

//TODOAuto-generatedcatchblock

e1.printStackTrace();

}catch(InvalidKeyExceptione1){

//TODOAuto-generatedcatchblock

e1.printStackTrace();

}catch(IllegalBlockSizeExceptione1){

//TODOAuto-generatedcatchblock

e1.printStackTrace();

}catch(BadPaddingExceptione1){

//TODOAuto-generatedcatchblock

e1.printStackTrace();

}catch(ClassNotFoundExceptione1){

//TODOAuto-generatedcatchblock

e1.printStackTrace();

}

try{

you_text.close();

server_text.close();

server_key.close();

you_key.close();

}catch(IOExceptione1){

e1.printStackTrace();

}

}

});

btnNewButton.setBounds(111,228,93,23);

frame.getContentPane().add(btnNewButton);

JButtonbtnNewButton_2=newJButton("开启密文通道");

btnNewButton_2.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

try{

server_text=newServerSocket(5678);

you_text=server_text.accept();

}catch(IOExceptione1){

e1.printStackTrace();

}

}

});

btnNewButton_2.setBounds(10,39,150,23);

frame.getContentPane().add(btnNewButton_2);

JLabellbldes=newJLabel("通过DES加密后");

lbldes.setBounds(20,74,93,23);

frame.getContentPane().add(lbldes);

}

}

客户端MyClient类:

packageCaesar_Modification;

importjava.awt.EventQueue;

importjava.awt.TextArea;

importjavax.crypto.BadPaddingException;

importjavax.crypto.Cipher;

importjavax.crypto.IllegalBlockSizeException;

importjavax.crypto.KeyGenerator;

importjavax.crypto.NoSuchPaddingException;

importjava.security.Key;

importjavax.swing.JFrame;

importjavax.swing.JTextField;

importjavax.swing.JLabel;

importjavax.swing.JButton;

importjava.awt.event.ActionListener;

importjava.awt.event.ActionEvent;

importjava.awt.Color;

importjava.io.FileOutputStream;

importjava.io.IOException;

importjava.io.ObjectOutputStream;

importjava.io.OutputStream;

importjava.io.PrintWriter;

import.Socket;

import.UnknownHostException;

importjava.security.InvalidKeyException;

importjava.security.NoSuchAlgorithmException;

importjavax.swing.JTextArea;

importsun.misc.BASE64Encoder;

publicclassMyClient{

privateJFrameframe;

privateJTextFieldLogin_C;

privatestaticTextAreaCDealWith_Before;

privatestaticTextAreaPri_KeyC;

privatestaticKeysecretkey;

publicstaticvoidmain(String[]args){

EventQueue.invokeLater(newRunnable(){

publicvoidrun(){

try{

MyClientwindow=newMyClient();

window.frame.setVisible(true);

}catch(Exceptione){

e.printStackTrace();

}

}

});

}

publicMyClient(){

initialize();

}

privatevoidinitialize(){

frame=newJFrame();

frame.getContentPane().setBackground(Color.LIGHT_GRAY);

frame.setTitle("客户端");

frame.setBounds(100,100,450,300);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().setLayout(null);

JLabellblNewLabel=newJLabel("明文");

lblNewLabel.setBounds(169,18,112,33);

frame.getContentPane().add(lblNewLabel);

finalTextAreaCDealWith_Before=newTextArea();

CDealWith_Before.setBounds(169,61,242,143);

frame.getContentPane().add(CDealWith_Before);

JButtonbtnNewButton_1=newJButton("加密");

btnNewButton_1.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

SocketmySocket_text;

Stringstr_before="";

try{

mySocket_text=newSocket(Login_C.getText(),5678);

OutputStreamos=null;

os=mySocket_text.getOutputStream();

PrintWriterpw=newPrintWriter(os);

str_before=CDealWith_Before.getText();

Ciphercp=Cipher.getInstance("DES");

//初始化Cipher对象

cp.init(Cipher.ENCRYPT_MODE,secretkey);

byte[]ptext=str_before.getBytes("UTF8");//指定UTF8字符集,可以跨平台

byte[]ctext;

try{

ctext=cp.doFinal(ptext);

BASE64Encoderbase64encoder=newBASE64Encoder();

pw.write(base64encoder.encode(ctext));

pw.close();

}catch(IllegalBlockSizeExceptione1){

//TODOAuto-generatedcatchblock

e1.printStackTrace();

}catch(BadPaddingExceptione1){

//TODOAuto-generatedcatchblock

e1.printStackTrace();

}//加密序列化

mySocket_text.close();

}catch(UnknownHostExceptione1){

e1.printStackTrace();

}catch(IOExceptione1){

e1.printStackTrace();

}catch(NoSuchAlgorithmExceptione1){

//TODOAuto-generatedcatchblock

e1.printStackTrace();

}catch(NoSuchPaddingExceptione1){

//TODOAuto-generatedcatchblock

e1.printStackTrace();

}catch(InvalidKeyExceptione1){

//TODOAuto-generatedcatchblock

e1.printStackTrace();

}catch(Exceptione2){

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 学科竞赛

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1