数字签名问题,等待高手。。。。。。

xjspa 2005-04-17 10:02:16
本人根据网上一片文章《基于java的数字签名技术在电子政务中的应用》http://www.ahcit.com/200501/20050121.doc中的方法写的数字签名的例子,但不能读入要加密的源文件。请教高手。。。。。
import java.io.*;
import java.security.*;
import javax.crypto.*;
class encryption
{
public static void main(String[] args)
{
try
{
KeyPairGenerator DoublekeyGen=KeyPairGenerator.getInstance("RSA");
DoublekeyGen.initialize(1024);
KeyPair DoubleKey = DoublekeyGen.generateKeyPair();
PrivateKey priKey = DoubleKey.getPrivate();
PublicKey pubKey = DoubleKey.getPublic();
try
{
String fpubKey="d:\\pubKey.dat";
String fprivateKey="d:\\privateKey.dat";
File fpublic=new File(fpubKey);
File fprivate=new File(fprivateKey);
ObjectOutputStream outPub = new ObjectOutputStream(new FileOutputStream(fpublic));
ObjectOutputStream outPri =new ObjectOutputStream (new FileOutputStream(fprivate));
outPub.writeObject(pubKey);
outPri.writeObject(priKey);
outPub.close();
outPri.close();
}
catch(IOException e)
{
System.out.print(e.toString());
}
catch(SecurityException e)
{
System.out.print(e.toString());
}

KeyGenerator SinglekeyGen = KeyGenerator.getInstance("DES");
SinglekeyGen.init(56);
Key SingleKey = SinglekeyGen.generateKey();

try
{
/*以下的代码有问题,但我解决不了*/
ObjectInputStream ReadFile=new ObjectInputStream(new FileInputStream("d:\\privateKey.dat"));
byte[] ReadFilePlain=(byte[])ReadFile.readObject();
ReadFile.close();
/*将待传输的原始信息生成消息摘要MD_1*/
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(ReadFilePlain);
/*生成待传得摘要信息*/
byte[] MD_1 = messageDigest.digest();

/* 将消息摘要MD_1[]和对称密钥SingleKey合并到字节数组MK[]中,并用RSA算法中的私钥对该数组加密并保存在数组Rsa_sign[]中形成了数字签名*/
//for (int i=0;i<10;i++ )
int sl=SingleKey.getEncoded().length;
int ml=MD_1.length;
byte[] MK= new byte[ml+sl];
byte[] SK=SingleKey.getEncoded();
for (int i=0;i<ml;i++)
{
MK[i]=MD_1[i];
}

for(int i=0;i<sl;i++)
{
MK[ml+i]=SK[i];
}
System.out.print(MK.length);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, priKey);
byte[] Rsa_sign= (byte[])cipher.doFinal(MK);

/* 将待传输的信息用DES加密,并将加密后的数据保存在数组Des_info[]中*/
cipher=Cipher.getInstance("DES");
cipher.init(cipher.ENCRYPT_MODE,SingleKey);
byte[] Des_info = cipher.doFinal(ReadFilePlain);
try{
/*将需要传输的原文的密文和数字签名写到文件En_info.dat中,En_info.dat即为在网上传输的信息*/
ObjectOutputStream outPut = new ObjectOutputStream(new FileOutputStream("d:\\En_info.dat",true));
outPut.writeObject(Des_info);
outPut.writeObject(Rsa_sign);
outPut.close();
}
catch(IOException e)
{
e.printStackTrace();
}

}
catch(Exception e)
{
e.printStackTrace();
}
}
catch(NoSuchAlgorithmException e)
{
System.out.println(e);
}

}
}
...全文
134 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
haiyulong 2005-04-18
  • 打赏
  • 举报
回复
用Applet写的java小程序如果没有经过签名,那么访问客户端程序下载后会受到安全限制;下面是具体操作的文档,希望能给你一些帮助。

场景:test.jar:内含待签名的java小程序

结果:签名后,客户端能自动下载java小程序,且java小程序能访问客户端的任何资源

步骤:

(1)将java小程序打包

jar cvf test.jar test.class

(2)生成名为chinani.store的密钥库,别名为chinani

keytool -genkey -keystore chinani.store -alias chinani

密码:123456 (根据需要自己输入)

以下根据需要输入,最后确认:y

(3)导出chinani.cer数字证书

keytool -export -keystore chinani.store -alias chinani -file chinani.cer

密码:123456 (根据需要自己输入)

(4)对test.jar文件进行签名

jarsigner -keystore chinani.store test.jar chinani

密码:123456 (根据需要自己输入)

最后写一个html文件,运行此文件即可自动下载安装运行java小程序

knight_yl 2005-04-18
  • 打赏
  • 举报
回复
有问题么
dakuntk 2005-04-18
  • 打赏
  • 举报
回复
学习
hongyan2004 2005-04-18
  • 打赏
  • 举报
回复
learning
Goal3 2005-04-18
  • 打赏
  • 举报
回复
tangjiehua 2005-04-18
  • 打赏
  • 举报
回复
初学,了解一下
诗海 2005-04-18
  • 打赏
  • 举报
回复
mark
wjvonline 2005-04-17
  • 打赏
  • 举报
回复
不懂,帮你顶
superfishmanweb 2005-04-17
  • 打赏
  • 举报
回复
你在csdn的文档中心查找“ 创建访问本地文件系统的信任APPLET ”有两篇例子可以用来参考一下,或许有帮助

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧