Java實(shí)現(xiàn)的RSA加密解密算法示例
本文實(shí)例講述了Java實(shí)現(xiàn)的RSA加密解密算法。分享給大家供大家參考,具體如下:
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import javax.crypto.Cipher;
public class RSAUtils{
public static String makekeyfile(String pubkeyfile, String prikeyfile) {
String result = "生成公私鑰文件失敗";
try{
// KeyPairGenerator用于生成公私鑰對,基于RSA算法生成對象
KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
// 初始化密鑰對生成器,密鑰大小為1024位
gen.initialize(1024);
// //生成強(qiáng)隨機(jī)數(shù)
// SecureRandom random = new SecureRandom();
// gen.initialize(1024,random);
// 生成一個(gè)密鑰對,保存在pair中
KeyPair pair = gen.generateKeyPair();
// 得到私鑰
RSAPrivateKey priKey = (RSAPrivateKey) pair.getPrivate();
// 得到公鑰
RSAPublicKey pubKey = (RSAPublicKey) pair.getPublic();
// 生成私鑰文件
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(prikeyfile));
os.writeObject(priKey);
os.flush();
os.close();
//生成公鑰文件
os = new ObjectOutputStream(new FileOutputStream(pubkeyfile));
os.writeObject(pubKey);
os.flush();
os.close();
result = "生成公鑰文件【"+pubkeyfile+"】生成私鑰文件【"+prikeyfile+"】";
}catch(Exception e){
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
try{
String pubfile = "F:/images/pub.key";
String prifile = "F:/images/pri.key";
String result = null;
//result = makekeyfile(pubfile, prifile);
result = markPuPra(pubfile, prifile);
System.out.println(result);
}catch(Exception e){
e.printStackTrace();
}
}
public static String markPuPra(String pubfile,String prifile){
String results = "加解密出錯(cuò)";
try{
ObjectInputStream os = new ObjectInputStream(new FileInputStream(pubfile));
RSAPublicKey pubkey = (RSAPublicKey) os.readObject();
os.close();
os = new ObjectInputStream(new FileInputStream(prifile));
RSAPrivateKey prikey = (RSAPrivateKey) os.readObject();
os.close();
String utf = "UTF-8";
String msg = "##中國%%的)人@+_";
// 使用公鑰加密私鑰解密
System.out.println("原文: " + msg);
byte[] puk = handleData(pubkey, msg.getBytes(utf), 1);
System.out.println("加密后文件數(shù)據(jù): " + new String(puk, utf));
byte[] dpuk = handleData(prikey, puk, 0);
System.out.println("解密后文件數(shù)據(jù): " + new String(dpuk, utf));
msg = "jd#我0們的¥人+=#新";
// 使用私鑰加密公鑰解密
System.out.println("原文: " + msg);
byte[] prk = handleData(prikey, msg.getBytes(utf), 1);
System.out.println("加密后文件數(shù)據(jù): " + new String(prk, utf));
byte[] dprk = handleData(pubkey, prk, 0);
System.out.println("解密后文件數(shù)據(jù): " + new String(dprk, utf));
results = "加解密完成";
}catch(Exception e){
e.printStackTrace();
}
return results;
}
/**
*
* @param k
* @param data
* @param encrypt 1 加密 0解密
* @return
* @throws Exception
*/
public static byte[] handleData(Key key, byte[] data, int type) throws Exception {
if (key != null) {
Cipher ci = Cipher.getInstance("RSA");
if (type == 1) {
ci.init(Cipher.ENCRYPT_MODE, key);
byte[] res = ci.doFinal(data);
return res;
}
if (type == 0) {
ci.init(Cipher.DECRYPT_MODE, key);
byte[] res = ci.doFinal(data);
return res;
}
}
return null;
}
}
PS:關(guān)于加密解密感興趣的朋友還可以參考本站在線工具:
文字在線加密解密工具(包含AES、DES、RC4等):
http://tools.jb51.net/password/txt_encode
MD5在線加密工具:
http://tools.jb51.net/password/CreateMD5Password
在線散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt
在線MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha
在線sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java字符與字符串操作技巧總結(jié)》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
java虛擬機(jī)學(xué)習(xí)筆記進(jìn)階篇
在本篇內(nèi)容里小編給大家分享了關(guān)于java虛擬機(jī)學(xué)習(xí)筆記的進(jìn)階內(nèi)容,需要的朋友們跟著學(xué)習(xí)下。2019-06-06
完美解決因數(shù)據(jù)庫一次查詢數(shù)據(jù)量過大導(dǎo)致的內(nèi)存溢出問題
今天小編就為大家分享一篇完美解決因數(shù)據(jù)庫一次查詢數(shù)據(jù)量過大導(dǎo)致的內(nèi)存溢出問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
關(guān)于重寫equals()方法和hashCode()方法及其簡單的應(yīng)用
這篇文章主要介紹了關(guān)于重寫equals()方法和hashCode()方法及其簡單的應(yīng)用,網(wǎng)上的知識有些可能是錯(cuò)誤的,關(guān)于?equals()?方法的理解,大家討論不一樣,需要的朋友可以參考下2023-04-04
java自定義任務(wù)類定時(shí)執(zhí)行任務(wù)示例 callable和future接口使用方法
Callable是類似于Runnable的接口,實(shí)現(xiàn)Callable接口的類和實(shí)現(xiàn)Runnable的類都是可被其它線程執(zhí)行的任務(wù)2014-01-01
使用Java實(shí)現(xiàn)在Excel中創(chuàng)建下拉列表
下拉列表(下拉框)可以確保用戶僅從預(yù)先給定的選項(xiàng)中進(jìn)行選擇,這樣不僅能減少數(shù)據(jù)輸入錯(cuò)誤,還能節(jié)省時(shí)間提高效率,下面我們就來看看如何在java中利用免費(fèi)庫實(shí)現(xiàn)創(chuàng)建下拉列表吧2024-03-03

