日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

dsa数字签名c语言编程,对文件进行DSA数字签名

發(fā)布時(shí)間:2023/12/20 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 dsa数字签名c语言编程,对文件进行DSA数字签名 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

package com.ivan.security.algorithm;

/**

* 對(duì)文件進(jìn)行DSA數(shù)字簽名

* @author Ivan

* @DataTime 2006-12-12 0:58

*

*/

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.security.InvalidKeyException;

import java.security.KeyPair;

import java.security.KeyPairGenerator;

import java.security.NoSuchAlgorithmException;

import java.security.PrivateKey;

import java.security.PublicKey;

import java.security.SecureRandom;

import java.security.Signature;

import java.security.SignatureException;

import com.iuxi.security.util.Tool;

public class DSAUtil ...{

private PublicKey pubKey;

private PrivateKey priKey;

public String pubfile = "";

public String prifile = "";

public DSAUtil() ...{}

public DSAUtil(String pubfile, String prifile) ...{

this.pubfile = pubfile;

this.prifile = prifile;

}

/** *//**

* 采用DSA算法生成非對(duì)稱密鑰對(duì)

*

*/

public void genKeys() ...{

try ...{

KeyPairGenerator keygen = KeyPairGenerator.getInstance("DSA");

SecureRandom secrand = new SecureRandom();

secrand.setSeed("random generator".getBytes());

keygen.initialize(512,secrand); //初始化密鑰生成器

KeyPair keys = keygen.generateKeyPair();//生成密鑰對(duì)

pubKey = keys.getPublic();

priKey = keys.getPrivate();

}catch(NoSuchAlgorithmException ex) ...{

ex.printStackTrace();

}

}

public String getPrifile() ...{

return prifile;

}

public void setPrifile(String prifile) ...{

this.prifile = prifile;

}

public String getPubfile() ...{

return pubfile;

}

public void setPubfile(String pubfile) ...{

this.pubfile = pubfile;

}

/** *//**

* 保存密鑰對(duì)信息到磁盤

* @param filepath1 公鑰的存放路徑

* @param filepath2 私鑰的存放路徑

*/

public void saveKeys() ...{

if((!pubfile.equals(""))&&(!prifile.equals("")))

this.saveKeys(pubfile,prifile);

else

System.err.println("保存密鑰出錯(cuò),請(qǐng)先設(shè)置保存路徑!");

}

public void saveKeys(String filepath1, String filepath2) ...{

try ...{

FileOutputStream fos = new FileOutputStream(filepath1);

ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeObject(pubKey);

oos.close();

fos = new FileOutputStream(filepath2);

oos = new ObjectOutputStream(fos);

oos.writeObject(priKey);

oos.close();

}catch(IOException ex) ...{

System.err.println("保存密鑰對(duì)信息出錯(cuò)!");

ex.printStackTrace();

}

}

/** *//**

* 從磁盤讀取密鑰信息

* @param filepath 密鑰存放路徑

* @return 返回密鑰信息,為 Object 類,然后可根據(jù)具體的 PublicKey 或 PrivateKey 進(jìn)行強(qiáng)制類型轉(zhuǎn)換

*/

private Object getKey(String filepath) ...{

Object obj = null;

try ...{

FileInputStream fis = new FileInputStream(filepath);

ObjectInputStream ois = new ObjectInputStream(fis);

obj = ois.readObject();

ois.close();

}catch(IOException ex) ...{

System.err.println("讀取密鑰信息錯(cuò)誤!");

}catch(ClassNotFoundException ex) ...{

ex.printStackTrace();

}

return obj;

}

/** *//**

* 獲取公鑰,如果當(dāng)前公鑰為空,則從磁盤文件讀取,并將其設(shè)置為當(dāng)前公鑰,否則直接返回當(dāng)前公鑰

* @return 公鑰

*/

public PublicKey getPubKey() ...{

if(pubKey != null)

return pubKey;

if(!pubfile.equals("")) ...{

pubKey = (PublicKey)this.getKey(pubfile);

return this.pubKey;

}

else ...{

System.err.println("讀取公鑰信息錯(cuò)誤!");

return null;

}

}

public PublicKey getPubKey(String filepath) ...{

pubKey = (PublicKey)this.getKey(filepath);

return this.pubKey;

}

/** *//**

* 獲取私鑰,如果當(dāng)前私鑰為空,則從磁盤文件讀取,并將其設(shè)置為當(dāng)前私鑰,否則直接返回當(dāng)前私鑰

* @return 私鑰

*/

public PrivateKey getPriKey() ...{

if(priKey != null)

return priKey;

if(!prifile.equals("")) ...{

priKey = (PrivateKey)this.getKey(pubfile);

return this.priKey;

}

else ...{

System.err.println("讀取私鑰信息錯(cuò)誤!");

return null;

}

}

public PrivateKey getPriKey(String filepath) ...{

priKey = (PrivateKey)this.getKey(filepath);

return this.priKey;

}

/** *//**

* 利用當(dāng)前私鑰對(duì)信息進(jìn)行簽名

* @return 簽名信息(byte類型)

*/

public byte[] signBytes(byte[] info) ...{

byte[] signed = null;

if(priKey == null) ...{

System.err.println("======== 提取密鑰信息錯(cuò)誤,無法完成簽名 =========");

return null;

}

try ...{

Signature signet = Signature.getInstance("DSA");

signet.initSign(priKey);

signet.update(info);

signed = signet.sign();

}catch(NoSuchAlgorithmException ex) ...{

ex.printStackTrace();

}catch(InvalidKeyException ex) ...{

System.err.println("簽名錯(cuò)誤,無效的密鑰");

ex.printStackTrace();

}catch(SignatureException ex) ...{

ex.printStackTrace();

}

return signed;

}

/** *//**

* 驗(yàn)證簽名信息

* @param info 待驗(yàn)證的信息

* @param signed 該驗(yàn)證信息的簽名

* @return 若驗(yàn)證正常,則返回 true , 否則返回 false

*/

public boolean checkSign(byte[] info, byte[] signed) ...{

if(pubKey == null) ...{

System.err.println("======== 提取密鑰信息錯(cuò)誤,無法完成簽名 =========");

return false;

}

try ...{

Signature checkSignet = Signature.getInstance("DSA");

checkSignet.initVerify(pubKey);

checkSignet.update(info);

if(checkSignet.verify(signed)) ...{

System.out.println("======== 簽名正常 =========");

return true;

}

else ...{

System.err.println("======== 簽名信息異常 =========");

return false;

}

}catch(NoSuchAlgorithmException ex) ...{

ex.printStackTrace();

}catch(InvalidKeyException ex) ...{

System.err.println("驗(yàn)證簽名錯(cuò)誤,無效的密鑰");

ex.printStackTrace();

}catch(SignatureException ex) ...{

ex.printStackTrace();

}

return true;

}

public static void main(String[] args) ...{

DSAUtil dsa = new DSAUtil("pubKey.dat","priKey.dat");

dsa.genKeys();

dsa.saveKeys();

String test = "This is just a test!";

byte[] signedData = dsa.signBytes(test.getBytes());

System.out.println(Tool.byte2hex(signedData));

dsa.checkSign(test.getBytes(),signedData);

}

}

總結(jié)

以上是生活随笔為你收集整理的dsa数字签名c语言编程,对文件进行DSA数字签名的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。