⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rsa.java

📁 密码学中RSA,AES和数值签名的实现。。。。
💻 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();
	}
}

/**
 * 将生成的密匙写入对应的文件
 * @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();
	}
}
/**
 * 从对应的文件中得到密匙
 * @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);
	//----------得到公匙的两个重要参数e,n----------
	BigInteger e =pbkey.getPublicExponent();
	BigInteger n = pbkey.getModulus();
	System.out.println("加密的密匙e="+e);
	System.out.println("取模的模数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);
	//---------得到私钥计算的两个重要参数d,n-----------
	BigInteger d=prkey.getPrivateExponent();
	BigInteger n=prkey.getModulus();
	System.out.println("解密的私钥的指数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("可选键对应操作如下");
	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("公密匙对已经生成!");
	   } 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("输入有误,请按提示键进行操作!!");
	   }
	  }
	}
	catch (Exception e)
	{
	}    
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -