📄 rsa.java
字号:
private JTextField d;
private JTextField e;
private JTextArea input;
private JTextArea output;
}
class DecodeListener implements ActionListener
{
public DecodeListener(JTextField aP, JTextField aQ, JTextField ad, JTextField ae,
JTextArea out, JTextArea out1)
{
P = aP;
Q = aQ;
d = ad;
e = ae;
output = out;
output1 = out1;
}
public void actionPerformed(ActionEvent ee)
{
// TODO
BigInteger PrimeP = new BigInteger(P.getText());
BigInteger PrimeQ = new BigInteger(Q.getText());
BigInteger n =PrimeP.multiply(PrimeQ);
int nLen = n.bitLength();
int m=(int)(Math.ceil((double)(nLen)/16.0));
nLen = (nLen-1) / 16;
String inStr = output.getText();
output1.setText(PublicMethod.Decode(inStr,PrimeP,PrimeQ,n,nLen,m,d));
}
private JTextField P;
private JTextField Q;
private JTextField d;
private JTextField e;
private JTextArea output;
private JTextArea output1;
}
class PublicMethod
{
public static void GetPrime( JTextField prime )
{
BigInteger num = new BigInteger("0");
Random rand = new Random();
do
{
int length = (int)(Math.random()*20+100);
System.out.println(length);
num = new BigInteger( length, 5 , rand );
prime.setText(num.toString());
}
while(MillerRobin(num)==false);
}
public static boolean MillerRobin(BigInteger num)
{
int time = 1000;
BigInteger mod = num.mod(new BigInteger("2"));
if(mod.equals(new BigInteger("0")))
{
return false;
}
int s = 0, j=0;
BigInteger t=num.subtract(new BigInteger("1"));
while( t.mod(new BigInteger("2")).equals("0") )
{
t.divide(new BigInteger("2"));
++s;
}
for(int i=0; i<time; ++i)
{
BigInteger a = new BigInteger(100, new Random()).mod(num.subtract(new BigInteger("3"))).add(new BigInteger("2"));
BigInteger y = powmod(a, t, num);
if(y.equals(new BigInteger("1"))==false
&& y.equals(num.subtract(new BigInteger("1")))==false)
{
j=1;
while(j<=s&&y.equals(num.subtract(new BigInteger("1")))==false)
{
y = y.multiply(y).mod(num);
if(y.equals(new BigInteger("1")))
{
return false;
}
++j;
}
if(y.equals(num.subtract(new BigInteger("1")))==false)
{
return false;
}
}
}
return true;
}
public static BigInteger powmod( BigInteger a, BigInteger t, BigInteger num )
{
BigInteger A = new BigInteger("1");
while(t.equals(new BigInteger("0"))==false)
{
if(t.mod(new BigInteger("2")).equals(new BigInteger("1")))
{
A = A.multiply(a).mod(num);
}
a = a.multiply(a).mod(num);
t=t.divide(new BigInteger("2"));
}
return A;
}
public static BigInteger invmod(BigInteger a, BigInteger b)
{
System.out.println(a+" "+b);
BigInteger s0=new BigInteger("1"), s1=new BigInteger("0"), s2, q, t, b0=b;
while(b.equals(new BigInteger("0"))==false)
{
q=a.divide(b);
s2=s0.subtract( q.multiply(s1) );
if(s2.compareTo(new BigInteger("0"))!=-1)
{
s2=s2.mod(b0);
}
else
{
s2=b0.subtract( s2.multiply(new BigInteger("-1")).mod(b0) );
}
s0=s1;
s1=s2;
t=b;
b=a.mod(b);
a=t;
}
if(a.equals(new BigInteger("1")))
{
return s0;
}
else
{
return new BigInteger("0");
}
}
public static String Encode(String inStr,BigInteger PrimeP,BigInteger PrimeQ,
BigInteger n,int nLen,int m,JTextField d)
{
BigInteger res = new BigInteger("0");
StringBuffer outBuf = new StringBuffer();
int i,k,j;
for(i=0;i<inStr.length();i=j)
{
BigInteger t = new BigInteger("0");
for(j=i;j<i+nLen&&j<inStr.length();j++)
{
t=t.shiftLeft(16);
long num = inStr.charAt (j);
t=t.add(BigInteger.valueOf(num));
}
res = PublicMethod.powmod(t,new BigInteger(d.getText()),n);
String buf = new String();
for(k=0;k<m;++k)
{
long num = (res.and(BigInteger.valueOf(65535))).longValue();
res = res.shiftRight(16);
buf = (char)(num)+buf;
}
outBuf = outBuf.append(buf);
}
return outBuf.toString();
}
public static String Decode(String inStr,BigInteger PrimeP,BigInteger PrimeQ,
BigInteger n,int nLen,int m,JTextField e)
{
StringBuffer outBuf = new StringBuffer();
BigInteger res = new BigInteger("0");
int i,j;
for(i=0;i<inStr.length();i+=j)
{
BigInteger t = new BigInteger("0");
for(j=0;j<m&&j+i<inStr.length();j++)
{
t = t.shiftLeft(16);
long num =(long)(inStr.charAt (j+i));
t=t.add(BigInteger.valueOf(num));
}
res = PublicMethod.powmod(t,new BigInteger(e.getText()),n);
String buf = new String();
while(res.compareTo(new BigInteger("0"))>0)
{
long num =(res.and(BigInteger.valueOf(65535))).longValue();
buf = (char)(num) + buf;
res = res.shiftRight(16) ;
}
outBuf = outBuf.append(buf);
}
return outBuf.toString();
}
public static String read(String infileName)
{
String ans = new String ();
try
{
//BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream (infileName)));
FileInputStream fis = new FileInputStream(infileName);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader (isr);
int t;
while(true)
{
t= br.read();
System.out.println(t);
if(t==-1) break;
ans = ans + (char)(t);
}
/*
byte b[] = new byte[1024];
int by;
int len=0;
while(true)
{
by = fis.read();
if(by==-1) break;
else b[len++]=(byte)by;
}
//System.out.println("byte"+b);
//System.out.println("len="+b.length);
String temp = new String(b,0,len);
//System.out.println("Sytem="+temp);
ans = temp;
*/
br.close();
isr.close();
fis.close();
}
catch(FileNotFoundException e)
{
System.out.println("FileStreamsTest: "+e);
}
catch(IOException e)
{
System.err.println("FileStreamsTest: "+e);
}
System.out.println("READSTR="+ans.length());
return ans;
}
public static void write(String outfileName,String outStr)
{
try
{
/*
/*
PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outfileName)));
out.print(outStr);
out.close();
*/
FileOutputStream fos = new FileOutputStream(outfileName);
OutputStreamWriter osw = new OutputStreamWriter(fos,"UNICODE");
BufferedWriter out = new BufferedWriter (osw);
int c;
for(int i=0;i<outStr.length();i++)
{
c=(int)outStr.charAt(i);
System.out.println(c);
out.write(c);
//out.flush();
}
out.close();
osw.close();
fos.close();
//System.out.println("=========================");
//System.out.println(outStr);
//System.out.println("=========================");
//byte[] by = outStr.getBytes();
// System.out.println("byte="+by.length);
//System.out.println("=========================");
//fos.write(by);
//out.write()
//out.close();
//fos.close();
/*
/////////////////////////////////////////////////////
File file = new File(outfileName);
if(!file.exists()) file.createNewFile();
FileWriter fw=new FileWriter(file);
PrintWriter pw = new PrintWriter (fw);
pw.print(outStr);
pw.close();
fw.close();
//////////////////////////////////////////////////////
*/
}
catch(FileNotFoundException e)
{
System.out.println("FileStreamsTest: "+e);
}
catch(IOException e)
{
System.err.println("FileStreamsTest: "+e);
}
System.out.println("WRITE="+outStr.length());
return ;
}
public static String input(String infileName)
{
String ans = new String();
try
{
FileInputStream in = new FileInputStream(infileName);
ObjectInputStream s = new ObjectInputStream(in);
ans = (String)s.readObject();
s.close();
in.close();
}
catch(FileNotFoundException e)
{
System.out.println("FileStreamsTest: "+e);
}
catch(IOException e)
{
System.err.println("FileStreamsTest: "+e);
}
catch(ClassNotFoundException e)
{
System.out.println("FileStreamsTest: "+e);
}
return ans;
}
public static void output(String outfileName,String outStr)
{
try
{
FileOutputStream f = new FileOutputStream(outfileName);
ObjectOutputStream s = new ObjectOutputStream(f);
s.writeObject(outStr);
s.close();
f.close();
}
catch(FileNotFoundException e)
{
System.out.println("FileStreamsTest: "+e);
}
catch(IOException e)
{
System.err.println("FileStreamsTest: "+e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -