签名文件签名(java代码)

代码签名  时间:2021-04-08  阅读:()

package encryptdecrypt;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.security.KeyPair;import java.security.KeyPairGenerator;import java.security.PrivateKey;import java.security.PublicKey;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.ScrollPaneConstants;publicclass FileSignature {

/**

* @param args

*/publicstaticvoid main(String[ ] args) {// TODO Auto-generated method stub

DemoWindow5 dw = new DemoWindow5("文件签名");dw.setBounds(dw.getToolkit() .getScreenSize() .width / 3,dw.getToolkit()

.getScreenSize() .height / 3,dw.getToolkit() .getScreenSize() .width / 3,dw.getToolkit()

.getScreenSize() .height / 3);dw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);dw.setVisible(true) ;

}

}classDemoWindow5extends JFrame implements ActionListener {JMenuBar menuBar = new JMenuBar();

JMenu fileMenu = new JMenu("文件");

JMenuItem openMenuItem = new JMenuItem("打开文件");

JMenuItem saveMenuItem = new JMenuItem("保存文件");

JMenu signMenu = new JMenu("签名验证");

JMenuItem generateItem = new JMenuItem("生成密钥对");JMenuItem signItem = new JMenuItem("签名文件") ;

JMenuItem validateItem = new JMenuItem("验证文件");

JTextArea jta = new JTextArea();intv = Sc rollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;inth = Sc rollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;JScrollPane jsp = new JScrollPane(jta, v, h) ;public DemoWindow5(String title) {super(title);fileMenu.add(openMenuItem);fileMenu.add(saveMenuItem);signMenu.add(generateItem);signMenu.add(signItem);signMenu.add(validateItem);menuBar.add(fileMenu);menuBar.add(signMenu);this.setJMenuBar(menuBar);add(jsp);openMenuItem.addActionListener(this);saveMenuItem.addActionListener(this);

generateItem.addActionListener(this);signItem.addActionListener(this);validateItem.addActionListener(this);

}publicvoid actionPerformed(ActionEvent e) {

// TODO Auto-generated method stubif (e.getSource() == openMenuItem) {openFile();

} elseif (e.getSource() == saveMenuItem) {saveFile();

} elseif (e.getSource() == generateItem) {generateKeys();

} elseif (e.getSource() == signItem) {sign();

} elseif (e.getSource() == validateItem) {validateSign();

}

}publicvoid openFile() {

String fileName = null;

// System.getProperty("user.dir")是获得用户当前的工作目录JFileChooser jfc = new

JFileChooser(System.getProperty("user.dir") ) ;jfc.setDialogTitle("打开文件");

//如果选中获取选择的文件的完整路径if (jfc.showOpenDialog(this) ==

J FileChooser.APPROVE_OPTION) {fileName = jfc.getSelectedFile() .getPath();

System.out.println(fileName);

}if (fileName == null)return;try {

FileInputStream fis = new FileInputStream(fileName);byte[ ] text = newbyte[f is.available()] ;f is.read(text);fis.close();

jta.setText(new String(text) );

} catch (Exception e1) {e1.printStackTrace();

}

}publicvoid saveFile() {

String fileName = null;

JFileChooser jfc = new

JFileChooser(System.getProperty("user.dir") ) ;jfc.setDialogTitle("保存文件");if (jfc.showSaveDialog(this) ==

J FileChooser.APPROVE_OPTION) {fileName = jfc.getSelectedFile() .getPath();

}if (fileName == null)return;try {

FileOutputStream fos = new FileOutputStream(fileName,false) ;fos.write(jta.getText() .getBytes()) ;fos.close();

} catch (Exception e1) {e1.printStackTrace();

}

}publicvoid generateKeys() {try {

String fileName = null;

JFileChooser jfc = new JFileChooser();jfc.setDialogTitle("生成密钥对");jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);if (jfc.showSaveDialog(this) ==

J FileChooser.APPROVE_OPTION) {

fileName = jfc.getSelectedFile() .getPath();}if (fileName == null)return;

File file = new File(fileName);if ( !file.exists() ) {file.mkdir();

}

//创建DSA密钥对生成器

KeyPairGenerator keygen =

KeyPairGenerator.getInstance("DSA");

//采用1024位DSA密钥keygen.initialize(1024);

//得到密钥对

KeyPair keys = keygen.generateKeyPair();

PublicKey pubkey = keys.getPublic();

PrivateKey prikey = keys.getPrivate();

ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName + "/公钥.dat") );out.writeObject(pubkey);out.close();out = new ObjectOutputStream(new

FileOutputStream(fileName

+ "/私钥.dat") );out.writeObject(prikey);out.close();

} catch (Exception e1) {e1.printStackTrace();

}

}publicvoid sign() {try {

String fileName = null;

JFileChooser jfc = new JFileChooser();jfc.setDialogTitle( "导入私钥文件");

if (jfc.showOpenDialog(this) ==

J FileChooser.APPROVE_OPTION) {fileName = jfc.getSelectedFile() .getPath();}if (fileName == null)return;

//获取私钥

ObjectInputStream ois = new ObjectInputStream(newFileInputStream(f ileName) );

PrivateKey prikey = (PrivateKey) ois.readObject() ;//创建SHAlwithDSA签名类java.security.Signature dsa =java.security.Signature.getInstance(

"SHA1withDSA", "SUN");

//设置生成摘要的私钥dsa.initSign(prikey);

//对文本区的文本进行签名验证dsa.update(jta.getText() .getBytes( "UTF-8"));

//得到签名后的签名摘要值byte[ ] signText = dsa.sign();jfc = new JFileChooser();jfc.setDialogTitle("保存签名文件");if (jfc.showSaveDialog(this) ==

J FileChooser.APPROVE_OPTION) {fileName = jfc.getSelectedFile() .getPath();}if (fileName == null)return;

FileOutputStream fos = new FileOutputStream(fileName);fos.write(signText);fos.close();

} catch (Exception e1) {e1.printStackTrace();

}

}

publicvoid validateSign() {try {

String fileName = null;

JFileChooser jfc = new JFileChooser();jfc.setDialogTitle("导入公钥文件");if (jfc.showOpenDialog(this) ==

J FileChooser.APPROVE_OPTION) {fileName = jfc.getSelectedFile() .getPath();}if (fileName == null)return;

ObjectInputStream in = new ObjectInputStream(newFileInputStream(f ileName) );

PublicKey pubkey = (PublicKey) in.readObject();jfc = new JFileChooser();jfc.setDialogTitle("打开签名文件");if (jfc.showOpenDialog(this) ==

J FileChooser.APPROVE_OPTION) {fileName = jfc.getSelectedFile() .getPath();}if (fileName == null)return;

//从签名文件中读取签名摘要

FileInputStream fis = new FileInputStream(fileName);byte[ ] sigToVerfiy = newbyte[fis.available() ] ;f is.read(sigToVerf iy);fis.close();

//创建SHA1withDSA签名类java.security.Signature sig =java.security.Signature.getInstance(

"SHA1withDSA", "SUN");

//设置验证的公钥sig.initVerify(pubkey);

//验证文本区sig.update(jta.getText() .getBytes( "UTF-8"));

//显示验证信息

if (sig.verify(sigToVerfiy)) {

JOptionPane.showMessageDialog(null, "签名正确");} else {

JOptionPane.showMessageDialog(null, "签名不正确");}

} catch (Exception e1) {e1.printStackTrace();

}

}

}

易探云330元/年,成都4核8G/200G硬盘/15M带宽,仅1888元/3年起

易探云服务器怎么样?易探云是国内一家云计算服务商家,致力香港云服务器、美国云服务器、国内外服务器租用及托管等互联网业务,目前主要地区为运作香港BGP、香港CN2、广东、北京、深圳等地区。目前,易探云推出的国内云服务器优惠活动,国内云服务器2核2G5M云服务器低至330元/年起;成都4核8G/200G硬盘/15M带宽,仅1888元/3年起!易探云便宜vps服务器配置推荐:易探云vps云主机,入门型云...

Digital-VM80美元新加坡和日本独立服务器

Digital-VM商家的暑期活动促销,这个商家提供有多个数据中心独立服务器、VPS主机产品。最低配置月付80美元,支持带宽、流量和IP的自定义配置。Digital-VM,是2019年新成立的商家,主要从事日本东京、新加坡、美国洛杉矶、荷兰阿姆斯特丹、西班牙马德里、挪威奥斯陆、丹麦哥本哈根数据中心的KVM架构VPS产品销售,分为大硬盘型(1Gbps带宽端口、分配较大的硬盘)和大带宽型(10Gbps...

王小玉网-美国洛杉矶2核4G 20元/月,香港日本CN2 2核2G/119元/季,美国300G高防/80元/月!

 活动方案:美国洛杉矶 E5 2696V2 2核4G20M带宽100G流量20元/月美国洛杉矶E5 2696V2 2核4G100M带宽1000G流量99元/季香港CN2 E5 2660V2 2核2G30M CN2500G流量119元/季日本CN2E5 2660 2核2G30M CN2 500G流量119元/季美国300G高防 真实防御E5 2696V2 2核2G30M...

代码签名为你推荐
phpweb破解如何破解网络锁http500http 550错误access数据库修复请问Access数据库修复恢复该怎么办啊,有些页和模块打不开了,也不知道是怎么回事,丢了文件还新iphone也将禁售苹果ID换了个新的怎么还是停用重庆电信断网重庆电信的最近是怎么回事啊!老断网美要求解锁iPhone如何看美版苹果是有锁无锁重庆电信dns重庆的DNS服务器地址是多少?厦门三五互联科技股份有限公司厦门三五互联怎么样?即时通请问有没有人知道即时通是什么?怎样先可以开??三五互联股票三五互联是干什么的?
网通服务器租用 美国独立服务器 安云加速器 云鼎网络 html空间 共享主机 129邮箱 什么是服务器托管 南通服务器 cdn加速是什么 双线机房 web应用服务器 日本代理ip ledlamp 1美元 域名和主机 apache启动失败 symantec 香港打折信息 神棍节 更多