天行健,君子以自強不息
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 一對多查詢的記錄重復問題(3594)
2.?java 加密算法 DSA 實現代碼(952)
3.?java 加密算法 RSA 實現代碼(839)
4.?Inside The JVM Part2: java如何實現安全性(581)
5.?java 加密算法 DES 實現代碼(471)
評論排行榜
1.?java虛擬機(1)
2.?Hibernate 一對多查詢的記錄重復問題(1)
3.?Tomcat 簡介(0)
4.?【轉載】JSP亂碼解決方案(0)
5.?java虛擬機參數(0)
java 加密算法 RSA 實現代碼
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
javax.crypto.BadPaddingException;
import
javax.crypto.Cipher;
import
javax.crypto.IllegalBlockSizeException;
import
javax.crypto.NoSuchPaddingException;
import
sun.security.rsa.RSAKeyPairGenerator;
public
class
YillRSA
{
/** */
/**
*
@param
args
*/
public
static
void
main(String[] args)
{
generateKeyPair();
byte
[] info
=
"
http://www.aygfsteel.com/yill/
"
.getBytes();
System.out.println(
"
Original info is
"
+
new
String(info));
System.out.println(
"
encrypted info is
"
+
new
String(encode(info)));
System.out.println(
"
Decrypted info is
"
+
new
String(decode(encode(info))));
}
public
static
void
generateKeyPair()
{
RSAKeyPairGenerator kg
=
new
RSAKeyPairGenerator();
kg.initialize(
1024
,
new
SecureRandom());
KeyPair keyPair
=
kg.generateKeyPair();
PrivateKey privateKey
=
keyPair.getPrivate();
writeKeyToDisk(
"
C:\\privateKey.dat
"
, privateKey);
PublicKey publicKey
=
keyPair.getPublic();
writeKeyToDisk(
"
C:\\publicKey.dat
"
, publicKey);
}
public
static
byte
[] encode(
byte
[] info)
{
PrivateKey privateKey
=
(PrivateKey) readKeyFromDisk(
"
C:\\privateKey.dat
"
);
try
{
Cipher cipher
=
Cipher.getInstance(
"
RSA
"
);
cipher.init(Cipher.ENCRYPT_MODE, privateKey,
new
SecureRandom());
return
cipher.doFinal(info);
}
catch
(NoSuchAlgorithmException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
catch
(NoSuchPaddingException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
catch
(InvalidKeyException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
catch
(IllegalBlockSizeException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
catch
(BadPaddingException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
return
new
byte
[
0
];
}
public
static
byte
[] decode(
byte
[] info)
{
PublicKey publicKey
=
(PublicKey) readKeyFromDisk(
"
C:\\publicKey.dat
"
);
try
{
Cipher cipher
=
Cipher.getInstance(
"
RSA
"
);
cipher.init(Cipher.DECRYPT_MODE, publicKey,
new
SecureRandom());
return
cipher.doFinal(info);
}
catch
(NoSuchAlgorithmException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
catch
(NoSuchPaddingException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
catch
(InvalidKeyException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
catch
(IllegalBlockSizeException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
catch
(BadPaddingException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
return
new
byte
[
0
];
}
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 00:08
yill
閱讀(839)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
Powered by:
BlogJava
Copyright © yill
主站蜘蛛池模板:
旅游
|
宁强县
|
保德县
|
孝感市
|
阳春市
|
六安市
|
峨山
|
新郑市
|
天水市
|
田东县
|
鄯善县
|
长乐市
|
南昌市
|
马鞍山市
|
蓝山县
|
泸定县
|
安顺市
|
余干县
|
高唐县
|
淮南市
|
乌兰浩特市
|
景东
|
永胜县
|
芜湖市
|
阳泉市
|
平果县
|
绩溪县
|
六枝特区
|
华容县
|
安图县
|
荣昌县
|
辛集市
|
重庆市
|
冀州市
|
方城县
|
龙山县
|
犍为县
|
筠连县
|
平果县
|
醴陵市
|
宜宾县
|