?? rsa.java
字號:
import java.io.*;
import java.math.BigInteger;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
public class RSA {
private static final String publickey = "publickey.key";
private static final String privatekey = "privatekey.key";
private static final String Algorithm ="RSA";
/**
* 生成密匙對
*
*/
public static void genpairKey() {
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance(Algorithm);
kpg.initialize(1024);
KeyPair keypair = kpg.generateKeyPair();
PublicKey pbkey = keypair.getPublic();
byte[] t1 = pbkey.getEncoded();
BigInteger tem1 = new BigInteger(t1);
System.out.println("public Key:"+tem1);
PrivateKey prkey = keypair.getPrivate();
byte[] t2 = prkey.getEncoded();
BigInteger tem2 = new BigInteger(t2);
System.out.println("private Key:"+tem2);
writeKey(publickey, pbkey);
writeKey(privatekey, prkey);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 將生成的密匙寫入對應(yīng)的文件
* @param filename
* @param obj
*/
public static void writeKey(String filename,Object obj) {
File file = new File(filename);
try {
FileOutputStream fout = new FileOutputStream(file);
ObjectOutputStream out =new ObjectOutputStream(fout);
out.writeObject(obj);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 從對應(yīng)的文件中得到密匙
* @param filename
* @return
*/
public static Object getKey(String filename) {
File file = new File(filename);
try {
FileInputStream fin = new FileInputStream(file);
ObjectInputStream in = new ObjectInputStream(fin);
Object t = in.readObject();
return t;
} 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;
}
/**
* 加密
*/
public static String Encrypt(String ming) {
StringBuffer buffer = new StringBuffer();
final int blocksize = 40;
RSAPublicKey pbkey =(RSAPublicKey)getKey(publickey);
//----------得到公匙的兩個(gè)重要參數(shù)e,n----------
BigInteger e =pbkey.getPublicExponent();
BigInteger n = pbkey.getModulus();
System.out.println("加密的密匙e="+e);
System.out.println("取模的模數(shù)n="+n);
int mleng = ming.length();
int i = (mleng-1)/blocksize;
for(int j=0; j<i+1; j++) {
int t=j*blocksize+blocksize;
if(t>mleng) t = mleng;
String tem = ming.substring(j*blocksize,t);
byte[] b=tem.getBytes();
BigInteger m = new BigInteger(b);
BigInteger mi = m.modPow(e, n);
buffer.append(mi.toString()+"~");
}
buffer.deleteCharAt(buffer.length()-1);
return buffer.toString();
}
public static String Decrypt(String mi) {
StringBuffer buffer = new StringBuffer();
String[] mis = mi.split("~");
RSAPrivateKey prkey = (RSAPrivateKey)getKey(privatekey);
//---------得到私鑰計(jì)算的兩個(gè)重要參數(shù)d,n-----------
BigInteger d=prkey.getPrivateExponent();
BigInteger n=prkey.getModulus();
System.out.println("解密的私鑰的指數(shù)d="+d);
System.out.println("解密的私鑰的模n="+n);
for(int i=0; i<mis.length; i++) {
BigInteger t =new BigInteger(mis[i]);
BigInteger bigm = t.modPow(d, n);
byte[] ming = bigm.toByteArray();
buffer.append(new String(ming));
}
return buffer.toString();
}
/*
從文件中讀取字符串
*/
public static String getString(String path) {
File file = new File(path);
StringBuffer buffer = new StringBuffer();
byte[] bytes = new byte[1024];
int i = 0;
try
{
FileInputStream fin = new FileInputStream(file);
while((i=fin.read(bytes))>0) {
buffer.append(new String(bytes,0,i));
}
fin.close();
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
return buffer.toString();
}
/*
將字符串寫入文件中
*/
public static void storeString(String str, String path) {
File file = new File(path);
try
{
FileOutputStream fout = new FileOutputStream(file);
fout.write(str.getBytes());
fout.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println("可選鍵對應(yīng)操作如下");
System.out.println("1:生成公密匙對。2:加密。3:解密。4:退出。");
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
String in = null;
String mi = null;
try
{
while(true){
in = read.readLine();
if(in.equals("1")){
System.out.println("生成中..");
genpairKey();
System.out.println("公密匙對已經(jīng)生成!");
} else if(in.equals("2")){
System.out.println("加密中...");
String str = getString("ming.txt");
mi = Encrypt(str);
storeString(mi,"mi.txt");
System.out.println("加密成功!");
} else if(in.equals("3")){
System.out.println("解密中.....");
String ming=Decrypt(mi);
storeString(ming, "DEming.txt");
System.out.println("解密成功!");
} else if(in.equals("4")){
System.out.println("Byte Byte!");
break;
} else {
System.out.println("輸入有誤,請按提示鍵進(jìn)行操作!!");
}
}
}
catch (Exception e)
{
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -