天行健,君子以自强不息
BlogJava
首页
新随笔
联系
聚合
管理
12 Posts :: 0 Stories :: 2 Comments :: 0 Trackbacks
公告
记录java生活点滴,即为查阅,亦可共享
mailto:
cafebabe@yeah.net
留言簿
(4)
给我留言
查看公开留言
查看私人留言
随笔档案
2009年8月 (1)
2008年4月 (2)
2008年3月 (9)
搜索
最新评论
1. re: Hibernate 一对多查询的记录重复问题
直接用select distinct 对象名 +后面的一堆就可以了
--秦
2. re: java虚拟机[未登录]
谢谢!
--HH
阅读排行榜
1. Hibernate 一对多查询的记录重复问题(3583)
2. java 加密算法 DSA 实现代码(945)
3. java 加密算法 RSA 实现代码(833)
4. Inside The JVM Part2: java如何实现安全性(574)
5. java 加密算法 DES 实现代码(467)
评论排行榜
1. java虚拟机(1)
2. Hibernate 一对多查询的记录重复问题(1)
3. Tomcat 简介(0)
4. 【转载】JSP乱码解决方案(0)
5. java虚拟机参数(0)
java 加密算法 DSA 实现代码
package
com.yill;
import
java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.ObjectInputStream;
import
java.io.ObjectOutputStream;
import
java.security.InvalidKeyException;
import
java.security.Key;
import
java.security.KeyPair;
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
sun.security.provider.DSAKeyPairGenerator;
public
class
YillDSA
{
public
static
void
main(String[] args)
{
byte
[] myInfo
=
"
http://www.blogjava.net/yill/
"
.getBytes();
byte
[] wrongInfo
=
"
http://www.blogjava.net/yill
"
.getBytes();
System.out.println(
"
My info is
"
+
new
String(myInfo));
generateKeyPair();
byte
[] signature
=
generateSignature(myInfo);
System.out.println(
"
The signature is
"
+
new
String(signature));
System.out.println(
"
Verify result is
"
+
verifySignature(myInfo, signature));
System.out.println(
"
Verify result of wrong info is
"
+
verifySignature(wrongInfo, signature));
}
public
static
void
generateKeyPair()
{
DSAKeyPairGenerator kg
=
new
DSAKeyPairGenerator();
kg.initialize(
1024
,
false
,
new
SecureRandom());
KeyPair keyPair
=
kg.generateKeyPair();
PrivateKey privateKey
=
keyPair.getPrivate();
writeKeyToDisk(
"
C:\\privateKeyDSA.dat
"
, privateKey);
PublicKey publicKey
=
keyPair.getPublic();
writeKeyToDisk(
"
C:\\publicKeyDSA.dat
"
, publicKey);
}
public
static
byte
[] generateSignature(
byte
[] info)
{
try
{
PrivateKey privateKey
=
(PrivateKey) readKeyFromDisk(
"
C:\\privateKeyDSA.dat
"
);
Signature sign
=
Signature.getInstance(
"
DSA
"
);
sign.initSign(privateKey);
sign.update(info);
return
sign.sign();
}
catch
(NoSuchAlgorithmException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
catch
(InvalidKeyException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
catch
(SignatureException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
return
new
byte
[
0
];
}
public
static
boolean
verifySignature(
byte
[] info,
byte
[] signature)
{
PublicKey publicKey
=
(PublicKey) readKeyFromDisk(
"
C:\\publicKeyDSA.dat
"
);
Signature sign;
try
{
sign
=
Signature.getInstance(
"
DSA
"
);
sign.initVerify(publicKey);
sign.update(info);
return
sign.verify(signature);
}
catch
(NoSuchAlgorithmException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
catch
(InvalidKeyException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
catch
(SignatureException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
return
false
;
}
private
static
void
writeKeyToDisk(String file, Key key)
{
try
{
ObjectOutputStream out
=
new
ObjectOutputStream(
new
FileOutputStream(file));
out.writeObject(key);
}
catch
(FileNotFoundException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
catch
(IOException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
}
private
static
Key readKeyFromDisk(String file)
{
try
{
ObjectInputStream in
=
new
ObjectInputStream(
new
FileInputStream(
file));
return
(Key) in.readObject();
}
catch
(FileNotFoundException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
catch
(IOException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
catch
(ClassNotFoundException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
return
null
;
}
}
posted on 2008-03-19 20:34
yill
阅读(945)
评论(0)
编辑
收藏
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
Chat2DB
C++博客
博问
管理
Powered by:
BlogJava
Copyright © yill