dec_rsa.java

来自「RSA加密算法java代码」· Java 代码 · 共 72 行

JAVA
72
字号
package src;
import java.security.*;


import java.security.spec.*;


import javax.crypto.*;


import javax.crypto.spec.*;


import javax.crypto.interfaces.*;


import java.security.interfaces.*;


import java.math.*;


import java.io.*;


public class Dec_RSA{
	RSAPrivateKey prk;
	
	public Dec_RSA(String RSAPrivateKeyFileName) throws Exception
	{
		 FileInputStream f=new FileInputStream(RSAPrivateKeyFileName);
	     ObjectInputStream b=new ObjectInputStream(f);
	     prk=(RSAPrivateKey)b.readObject( );
	     b.close();
	     f.close();
	     
	}
	public String deCrypt(String Msg) throws Exception
	{
		BigInteger c=new BigInteger(Msg);
		BigInteger d=prk.getPrivateExponent();
        BigInteger n=prk.getModulus();
        BigInteger m=c.modPow(d,n);
        return new String(m.toByteArray(),"utf8");
	}

   public static void main(String args[]) throws Exception{


        BufferedReader in= 
        new BufferedReader(new InputStreamReader(new FileInputStream("Enc_RSA.dat")));
        String ctext=in.readLine();
        BigInteger c=new BigInteger(ctext);
        FileInputStream f=new FileInputStream("Skey_RSA_priv.dat");
        ObjectInputStream b=new ObjectInputStream(f);
        RSAPrivateKey prk=(RSAPrivateKey)b.readObject( );
        BigInteger d=prk.getPrivateExponent();
        BigInteger n=prk.getModulus();
        System.out.println("d= "+d);
        System.out.println("n= "+n);
        BigInteger m=c.modPow(d,n);
        System.out.println("m= "+m);
        byte[] mt=m.toByteArray();
        System.out.println("PlainText is ");
        System.out.println(new String(mt,"utf8"));

     
   }
   


}

⌨️ 快捷键说明

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