📄 code.java
字号:
}while(true);
}
void Convert( int s[],int nn,int t[],int h[])
{
rsa n=new rsa(String.valueOf(Math.abs(nn)));
rsa m=new rsa("0");
Exp(m.Ddata,n.Ddata,t);
Divlev(m.Ddata,h);
s[s[0]+1]=m.Ddata[1];
s[s[0]+2]=m.Ddata[2];
s[s[0]+3]=m.Ddata[3];
s[s[0]+4]=m.Ddata[4];
s[s[0]+5]=m.Ddata[5];
// s[s[0]+6]=m.Ddata[6];///
s[0]+=5;
// s[0]+=6;
}
void setcode()
{
Clear();
int i;
String p;
p=new String();
rsa elm2=new rsa(String.valueOf(coded));
rsa elm3=new rsa(String.valueOf(coden));
char info[]=textPane.getText().toCharArray();
for(i=0;i<textPane.getText().length();i++)
{
Convert(s,(int)info[i],elm2.Ddata,elm3.Ddata);
}
textPane.setText("");
for(i=1;i<=s[0];i++)
{
p=p+String.valueOf(s[i]);
}
textPane.setText(p);
}
public void actionPerformed(ActionEvent e)
{
subwin=new JDialog();
subwin.setTitle("输入加密钥匙");
subwin.setModal(true);
subwin.setBounds(320,150,200,140);
text1=new JTextField(8);
text2=new JTextField(8) ;
subwin.add(new JLabel("设置加密私钥N"));
subwin.add(text1);
subwin.add(new JLabel("设置加密私钥D"));
subwin.add(text2);
subwin.add(button3);
subwin.add(button2);
FlowLayout flow=new FlowLayout();
subwin.setLayout(flow);
subwin.setVisible(true);
subwin.validate();
if(rsacodeflag)
{
new Warning("提示","加密中.....");
try{
setcode();
new Warning("提示","加密成功,请保存");
new Saveing();
rsacodeflag=false;
}
catch(Exception E)
{
new Warning("提示","加密异常,请重新加密");
}
}
else
{
new Warning("警告","尚未设置加密钥匙");
}
}
}
class RsaopenAction extends AbstractAction
{
public RsaopenAction()
{
super("非对称解密");
}
void Mul(int s[],int m[],int n[])
{
int i,j,k,t,p;
for(i=1;i<=n[0];i++)
for(j=1;j<=m[0];j++)
{
k=i+j;
t=(s[k-1]+m[j]*n[i])/10;
s[k-1]=(s[k-1]+m[j]*n[i])%10;
while(t!=0)
{
p=(t+s[k])/10;
s[k]=(t+s[k])%10;
t=p;
k++;
}
}
i=10000-1;
while(s[i]==0)
{
--i;
}
s[0]=i;
}
void Sublev(int m[],int n[])
{
int i;
for(i=1;i<10000-1;i++)
{
if(m[i]<n[i])
{m[i]=m[i]+10;--m[i+1];}
m[i]=m[i]-n[i];
}
i=10000-1;
while(m[i]==0)
{
--i;
}
m[0]=i;
}
void Add(int m[],int n[])
{
int i,t,j;
for(i=1;i<10000;i++)
{
m[i]=m[i]+n[i];
t=m[i]/10;
m[i]=m[i]%10;
j=i;
while(t!=0)
{
m[j+1]=m[j+1]+t;
t=m[j+1]/10;
m[j+1]=m[j+1]%10;
j++;
}
}
i=10000-1;
while(m[i]==0)
{
--i;
}
m[0]=i;
}
void Exp(int s[],int m[],int n[])
{
int temp=0;
int i,j;
int t[];
t=new int[10000];
for(i=0;i<10000;i++)
t[i]=m[i];
for(i=n[0];i>0;--i)
temp=temp*10+n[i];
for(i=1;i<temp;i++)
{
Mul(s,t,m);
for(j=0;j<10000;j++)
{t[j]=s[j];s[j]=0;}
}
for(j=0;j<10000;j++)
s[j]=t[j];
}
void Divlev(int m[],int n[])
{
int s[],t[];
s=new int[10000];
t=new int[10000];
int temp=1;
rsa temp1=new rsa("10");
rsa temp2=new rsa(String.valueOf(m[0]-n[0]));
Exp(s,temp1.Ddata,temp2.Ddata);
Mul(t,s,n);
do
{
Sublev(m,t);
if(m[10000-1]==-1)
{
Add(m,t);
if(t[0]==n[0])
{break;}
else
{
for(int i=1;i<t[0];i++)
t[i]=t[i+1];
t[t[0]]=0;
t[0]-=1;
}
}
}while(true);
}
char Explain(int m[],int t[],int h[])
{
int temp=0,i,j,flag;
rsa n=new rsa("0");
rsa te=new rsa("0");
n.Ddata[0]=5;
// n.Ddata[0]=6;
n.Ddata[1]=m[1];
n.Ddata[2]=m[2];
n.Ddata[3]=m[3];
n.Ddata[4]=m[4];
n.Ddata[5]=m[5];
// n.Ddata[6]=m[6];//
for(j=1;j<=m[0];j++)
m[j]=m[j+5];
m[0]-=5;
Exp(te.Ddata,n.Ddata,t);
Divlev(te.Ddata,h);
for(i=te.Ddata[0];i>0;--i)
{
temp=temp*10+te.Ddata[i];
}
char jiu;
// if(flag==0)
jiu=(char)temp;
// else
// jiu=(char)(temp*(-1));
return jiu;
}
void opencode()
{
char temp[]=textPane.getText().toCharArray();
for(int j=0;j<textPane.getText().length();j++)
{
s[j+1]=(int)temp[j]-(int)('0');
}
s[0]=textPane.getText().length();
String p=new String();
int i;
char enfo[] ;
enfo=new char[10000];
rsa elm1=new rsa(String.valueOf(codee));
rsa elm3=new rsa(String.valueOf(coden));
for(i=0;s[0]!=0;i++)
{
enfo[i]=Explain(s,elm1.Ddata,elm3.Ddata);
p=p+enfo[i];
}
textPane.setText(p);
}
public void actionPerformed(ActionEvent e)
{
subwin=new JDialog();
subwin.setTitle("输入解密钥匙");
subwin.setModal(true);
subwin.setBounds(320,150,200,140);
text1=new JTextField(8);
text2=new JTextField(8) ;
subwin.add(new JLabel("设置解密私钥N"));
subwin.add(text1);
subwin.add(new JLabel("设置解密私钥E"));
subwin.add(text2);
subwin.add(button4);
subwin.add(button2);
FlowLayout flow=new FlowLayout();
subwin.setLayout(flow);
subwin.setVisible(true);
subwin.validate();
if(rsaopenflag)
{
new Warning("提示","解密中.....");
try{
opencode();
new Warning("提示","解密成功,请保存");
new Saveing();
rsaopenflag=false;
}
catch(Exception E)
{
new Warning("提示","解密异常,请重新解密");
}
}
else
{
new Warning("警告","尚未设置解密钥匙");
}
}
}
class DessetcodeAction extends AbstractAction
{
public DessetcodeAction()
{super("设置密钥");
}
public void actionPerformed(ActionEvent e)
{
subwin=new JDialog();
subwin.setTitle("生成DES密钥");
subwin.setModal(true);
subwin.setBounds(320,150,140,100);
subwin.add(new JLabel("生成DES密钥"));
subwin.add(button9);
subwin.add(button2);
FlowLayout flow=new FlowLayout();
subwin.setLayout(flow);
subwin.setVisible(true);
subwin.validate();
if(dessetflag)
{
new Warning("提示","保存密钥*.bin");
Security.addProvider(new com.sun.crypto.provider.SunJCE());
String Algorithm="DES"; //定义 加密算法,可用 DES,DESede,Blowfish
try {
KeyGenerator keygen = KeyGenerator.getInstance(Algorithm);
SecretKey deskey = keygen.generateKey();
dessetflag=false;
int i = filechooser.showSaveDialog(Code.this);
if (i == JFileChooser.APPROVE_OPTION)
{
File f = filechooser.getSelectedFile();
try
{
FileOutputStream out = new FileOutputStream(f);
out.write(deskey.getEncoded ());
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
catch(Exception E)
{
new Warning("提示","获取DES密钥失败");
}
}
}
}
class DessetAction extends AbstractAction
{
public DessetAction()
{
super("对称加密");
}
public String byte2hex(byte[] b) //二行制转字符串
{
String hs="";
String stmp="";
for (int n=0;n<b.length;n++)
{
stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length()==1) hs=hs+"0"+stmp;
else hs=hs+stmp;
if (n<b.length-1) hs=hs+":";
}
return hs.toUpperCase();
}
public void actionPerformed(ActionEvent e)
{
new Warning("提示","打开密钥文件");
////open key///////
try{
int ii = filechooser.showOpenDialog(Code.this);
if (ii == JFileChooser.APPROVE_OPTION)
{
File f = filechooser.getSelectedFile();
try
{
FileInputStream fis = new FileInputStream(f);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = 0;
while ((i=fis.read()) != -1)
{ baos.write(i); }
fis.close();
byte[] rawKeyData = baos.toByteArray();
baos.close();
///////////get key///////
new Warning("提示","加密中....");
DESKeySpec dks = new DESKeySpec (rawKeyData);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES" );
SecretKey key = keyFactory.generateSecret( dks );
Cipher c1 = Cipher.getInstance("DES");
c1.init(Cipher.ENCRYPT_MODE,key);
//////////////set code///////
// System.out.println("加密前的二进串:"+byte2hex(textPane.getText().getBytes()));
byte[] cipherByte=c1.doFinal(textPane.getText().getBytes());
textPane.setText(new String(cipherByte));
// System.out.println("加密后的二进串:"+byte2hex(cipherByte));
//////////////////save cipherByte//////////////////
new Warning("提示","保存密文");
int i3 = filechooser.showSaveDialog(Code.this);
if (i3 == JFileChooser.APPROVE_OPTION)
{
File f1 = filechooser.getSelectedFile();
try
{
FileOutputStream out = new FileOutputStream(f1);
out.write(cipherByte);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
catch(Exception E)
{
new Warning("加密提示","加密出错");
}
}
}
class DesopenAction extends AbstractAction
{
public DesopenAction()
{
super("对称解密");
}
public String byte2hex(byte[] b) //二行制转字符串
{
String hs="";
String stmp="";
for (int n=0;n<b.length;n++)
{
stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length()==1) hs=hs+"0"+stmp;
else hs=hs+stmp;
if (n<b.length-1) hs=hs+":";
}
return hs.toUpperCase();
}
public void actionPerformed(ActionEvent e)
{
try {
new Warning("提示","打开密钥文件");
int ii = filechooser.showOpenDialog(Code.this);
if (ii == JFileChooser.APPROVE_OPTION)
{
File f = filechooser.getSelectedFile();
try
{
FileInputStream fis = new FileInputStream(f);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = 0;
while ((i=fis.read()) != -1)
{ baos.write(i); }
fis.close();
byte[] rawKeyData = baos.toByteArray();
baos.close();
///////////get key///////
DESKeySpec dks = new DESKeySpec (rawKeyData);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES" );
SecretKey key = keyFactory.generateSecret( dks );
Cipher c1 = Cipher.getInstance("DES");
c1.init(Cipher.ENCRYPT_MODE,key);
new Warning("提示","打开密文文件");
int i2 = filechooser.showOpenDialog(Code.this);
if (i2 == JFileChooser.APPROVE_OPTION)
{
File f2 = filechooser.getSelectedFile();
try
{
FileInputStream fis2 = new FileInputStream(f2);
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
int i4 = 0;
while ((i4=fis2.read()) != -1)
{ baos2.write(i4); }
fis2.close();
byte[] rawKeyData2 = baos2.toByteArray();
baos2.close();
//////////////////////////////////////
///////////////open key/////////////
c1 = Cipher.getInstance("DES");
c1.init(Cipher.DECRYPT_MODE, key);
byte[] clearByte=c1.doFinal(rawKeyData2);
// System.out.println("解密后的二进串:"+byte2hex(clearByte));
// System.out.println("解密后的信息:"+(new String(clearByte)));
textPane.setText(new String(clearByte));
new Warning("提示","解密成功");
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
catch(Exception E)
{
new Warning("提示","打开DES密钥文件失败");
}
}
}
class ListsetlengthAction extends AbstractAction
{
public ListsetlengthAction()
{
super("设置步长");
}
public void actionPerformed(ActionEvent e)
{
subwin=new JDialog();
subwin.setTitle("设置步长a->?");
subwin.setModal(true);
subwin.setBounds(320,150,300,120);
text1=new JTextField(8);
subwin.add(new JLabel("设置步长数字大于0"));
subwin.add(text1);
subwin.add(button5);
subwin.add(button2);
FlowLayout flow=new FlowLayout();
subwin.setLayout(flow);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -