资源简介
来自StackOverflow的 Jeffrey Walton 的文章中dsa数字签名算法的java实现,包括生成签名,消息签名,验证签名三个功能的实现。

代码片段和文件信息
package JavaInteropSign;
import java.io.*;
import java.security.*;
import java.security.spec.*;
import javax.swing.JOptionPane;
/**
* @author jeffrey walton
**/
public class Main {
static String ALGORITHM = “DSA“;
public static void main(String[] args) {
try {
CreateDSAKeys();
SignDSAMessage();
VerifyDSAMessage();
} catch (Exception e) {
System.err.println(“Main: Exception “ + e.toString());
}
}
private static void VerifyDSAMessage() {
try {
// Load the public
PublicKey publicKey = LoadPublicKey(“public.dsa.java.key“);
// Load the message from file
FileInputStream mis = new FileInputStream(“dsa.java.msg“);
byte[] message = new byte[mis.available()];
mis.read(message); mis.close();
// Display the resurrected string
JOptionPane.showMessageDialog(null
new String(message 0 message.length “UTF-8“));
// Load the signature of the message from file
FileInputStream sis = new FileInputStream(“dsa.java.sig“);
byte[] signature = new byte[sis.available()];
sis.read(signature); sis.close();
// Initialize Signature object
Signature verifier = Signature.getInstance(ALGORITHM);
verifier.initVerify(publicKey);
// Load the message into the Verifier object
verifier.update(message);
// Verify the Signature on the Message
boolean result = verifier.verify(signature);
StringBuilder sb = new StringBuilder();
if( result )
{
sb.append(“Message Verified:\n“);
sb.append(new String(message 0 message.length “UTF-8“));
}
else
{
sb.append(“Message Not Verified“);
}
JOptionPane.showMessageDialog(null sb.toString());
} catch (Exception e) {
System.err.println(“VerifyDSAMessage: “ + e.toString());
}
}
private static void SignDSAMessage() {
try {
// Retrieve the Private Key
PrivateKey privateKey = LoadPrivateKey(“private.dsa.java.key“);
// Create the signer object
Signature signer = Signature.getInstance(ALGORITHM);
signer.initSign(privateKey new SecureRandom());
// Prepare the Message
String s = “Crypto Interop: \u9aa8“;
// Save the binary of the String which we will sign
byte[] message = s.getBytes(“UTF-8“);
// Insert the message into the signer object
signer.update(message);
byte[] signature = signer.sign();
// mos: message filestream
// sos: signature filestream
FileOutputStream mos = new FileOutputStream(“dsa.java.msg“);
mos.write(message);
FileOutputStream sos = new FileOutputStream(“dsa.java.sig“);
sos.write(signature);
} catch (Exception e) {
System.err.println(“SignDSAMessage: “ + e.toString());
}
}
private static PublicKey LoadPublicKey(String filename) {
PublicKey key = null;
try {
FileInputStrea
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2008-04-25 23:17 JavaInteropSign\build\
目录 0 2008-04-25 23:17 JavaInteropSign\build\classes\
目录 0 2008-04-25 23:17 JavaInteropSign\build\classes\JavaInteropSign\
目录 0 2008-04-25 23:17 JavaInteropSign\build\test\
目录 0 2008-04-25 23:17 JavaInteropSign\build\test\classes\
目录 0 2008-04-25 23:17 JavaInteropSign\build\test\results\
文件 85 2008-04-25 19:17 JavaInteropSign\manifest.mf
目录 0 2008-04-25 23:17 JavaInteropSign\nbproject\
文件 33178 2008-04-25 19:17 JavaInteropSign\nbproject\build-impl.xm
文件 455 2008-04-25 19:17 JavaInteropSign\nbproject\genfiles.properties
目录 0 2008-04-25 23:17 JavaInteropSign\nbproject\private\
文件 188 2008-04-25 19:17 JavaInteropSign\nbproject\private\private.properties
文件 1824 2008-04-25 19:17 JavaInteropSign\nbproject\project.properties
文件 585 2008-04-25 19:17 JavaInteropSign\nbproject\project.xm
目录 0 2008-04-25 23:17 JavaInteropSign\src\
目录 0 2008-04-25 23:17 JavaInteropSign\src\JavaInteropSign\
文件 5070 2008-04-25 23:12 JavaInteropSign\src\JavaInteropSign\Main.java
目录 0 2008-04-25 23:17 JavaInteropSign\test\
目录 0 2008-04-26 10:48 JavaInteropSign\
相关资源
- 微博系统(Java源码,servlet+jsp),适
- java串口通信全套完整代码-导入eclip
- jsonarray所必需的6个jar包.rar
- 三角网构TIN生成算法,Java语言实现
- java代码编写将excel数据导入到mysql数据
- Java写的cmm词法分析器源代码及javacc学
- JAVA JSP公司财务管理系统 源代码 论文
- JSP+MYSQL旅行社管理信息系统
- 推荐算法的JAVA实现
- 基于Java的酒店管理系统源码(毕业设
- java-图片识别 图片比较
- android毕业设计
- java23种设计模式+23个实例demo
- java Socket发送/接受报文
- JAVA828436
- java界面美化 提供多套皮肤直接使用
- 在线聊天系统(java代码)
- 基于Java的图书管理系统807185
- java中实现将页面数据导入Excel中
- java 企业销售管理系统
- java做的聊天系统(包括正规课程设计
- Java编写的qq聊天室
- 商店商品管理系统 JAVA写的 有界面
- JAVA开发聊天室程序
- 在linux系统下用java执行系统命令实例
- java期末考试试题两套(答案) 选择(
- JAVA3D编程示例(建模、交互)
- Java 文件加密传输
- java做的房产管理系统
- 基于jsp的bbs论坛 非常详细
评论
共有 条评论