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

📄 rsa算法.doc

📁 java自行开发rsa
💻 DOC
字号:
这是第一个代码RSAGenerator,用于生成RSA中的p,q,n,m,e,d
并把n,e,d写入磁盘中的RSAKey.ser文件。
import java.security.*;
import java.math.*;
import java.io.*;
class RSAInfo implements Serializable
{
BigInteger e;
BigInteger d;
BigInteger n;
}
public class RSAGenerator
{
RSAInfo info=new RSAInfo();
public static void main(String[] args)
{
RSAGenerator obj=new RSAGenerator();
try{
obj.getParameter();
obj.writeState();
}catch(NoSuchAlgorithmException ex)
{
System.out.println("NoSuchAlgorithmException");
}
catch(IOException ex)
{
System.out.println("IOException");
}
}
public void getParameter() throws NoSuchAlgorithmException
{
int bitlength=100;
int certainty=50;
SecureRandom sRandom=SecureRandom.getInstance("SHA1PRNG");
BigInteger one=new BigInteger("1");
BigInteger p=new BigInteger(bitlength,certainty,sRandom);
BigInteger q=new BigInteger(bitlength,certainty,sRandom);
BigInteger n=p.multiply(q);
BigInteger m=p.subtract(one).multiply((q.subtract(one)));
int len=m.bitLength();
BigInteger e;
while(true)
{
e=new BigInteger(len,sRandom);
if(m.gcd(e).equals(one))break;
}
BigInteger d=e.modInverse(m);
info.e=e;
info.d=d;
info.n=n;
}
public void writeState() throws IOException
{
FileOutputStream fos=new FileOutputStream("RSAKey.ser");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(info);
oos.flush();
fos.close();
oos.close();
}
}
这是第二个代码RSAEncrypt,用于加密。
import java.security.*;
import java.math.*;
import java.io.*;
public class RSAEncrypt
{
public static void main(String[] args)
{
if(args.length!=1)
{
System.out.println("Usage: java RSAEncrypt Number");
System.exit(1);
}
RSAEncrypt obj=new RSAEncrypt();
try{
obj.encrypt(args[0]);
}catch(ClassNotFoundException ex)
{
System.out.println("ClassNotFoundException");
}
catch(FileNotFoundException ex)
{
System.out.println("FileNotFoundException");
}
catch(IOException ex)
{
System.out.println("IOException");
}
}
public void encrypt(String num) throws ClassNotFoundException,
FileNotFoundException,IOException
{
FileInputStream fis=new FileInputStream("RSAKey.ser");
ObjectInputStream ois=new ObjectInputStream(fis);
RSAInfo info=(RSAInfo)ois.readObject();
BigInteger plainText=new BigInteger(num);
BigInteger cipherText=plainText.modPow(info.e,info.n);
System.out.println("Cipher text is:");
System.out.println(cipherText);
fis.close();
ois.close();
}
}

这是第三个代码RSADecrypt,用于从RSAKey.ser中取出e.d.n解密。
import java.security.*;
import java.math.*;
import java.io.*;
public class RSADecrypt
{
public static void main(String[] args)
{
if(args.length!=1)
{
System.out.println("Usage: java RSADecrypt Number");
System.exit(1);
}
RSADecrypt obj=new RSADecrypt();
try{
obj.decrypt(args[0]);
}catch(ClassNotFoundException ex)
{
System.out.println("ClassNotFoundException");
}
catch(FileNotFoundException ex)
{
System.out.println("FileNotFoundException");
}
catch(IOException ex)
{
System.out.println("IOException");
}
}
public void decrypt(String num) throws ClassNotFoundException,
FileNotFoundException,IOException
{
   FileInputStream fis=new FileInputStream("RSAKey.ser");
ObjectInputStream ois=new ObjectInputStream(fis);
RSAInfo info=(RSAInfo)ois.readObject();
BigInteger cipherText=new BigInteger(num);
BigInteger plainText=cipherText.modPow(info.d,info.n);
System.out.println("Plain text is:");
System.out.println(plainText);
}
}

⌨️ 快捷键说明

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