📄 peerfuncimpl.java
字号:
}
});
bb[3]=peerUI.getQuitButton();
bb[3].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try{
quitWithProxy();
}catch(Exception eq){
System.out.println(eq.toString());
}
}
});
bb[4]=peerUI.getACRButton();
bb[4].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try{
acrWithProxy();
}catch(Exception ea){
System.out.println(ea.toString());
}
}
});
bb[5]=peerUI.getRFNButton();
bb[5].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try{
rfnWithProxy();
}catch(Exception er){
System.out.println(er.toString());
}
}
});
bb[6]=peerUI.getSendButton();
bb[6].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try{
sendWithProxy();
}catch(Exception es){
System.out.println(es.toString());
}
}
});
getContentPane().add(peerUI,BorderLayout.CENTER);
pack();
setSize(600,300);
show();
}
public static String encode(String str,int keyChoice)throws Exception{
int text_len=str.length();
//System.out.println("strlen="+text_len);
int count=(int) ((text_len/chunk_size) + (((text_len%chunk_size)==0)?0:1) );
//System.out.println("count="+count);
int flag=0;
BigInteger[] bi_arr = new BigInteger[count];
byte ptext[][]=new byte[count][];
String []splitStr=new String[count];
for(;flag<(count-1);flag++){
splitStr[flag]=str.substring(flag*(chunk_size),(flag+1)*(chunk_size));
ptext[flag]=splitStr[flag].getBytes("UTF8");
bi_arr[flag]=new BigInteger(ptext[flag]);
}
splitStr[flag]=str.substring(flag*(chunk_size));
ptext[flag]=splitStr[flag].getBytes("UTF8");
bi_arr[flag]=new BigInteger(ptext[flag]);
//for(int i=0;i<count;i++)
// System.out.println(splitStr[i]);
String cipher = "";
FileInputStream f=new FileInputStream("Skey_RSA_pub_"+keyChoice+".dat");
ObjectInputStream b=new ObjectInputStream(f);
RSAPublicKey pbk=(RSAPublicKey)b.readObject();
BigInteger e=pbk.getPublicExponent();
BigInteger n=pbk.getModulus();
for(int i=0;i<bi_arr.length;i++)
{
BigInteger bi = bi_arr[i];
BigInteger c = bi.modPow(e,n);
//System.out.println(c.toString());
if(cipher.compareTo("") == 0)
{
cipher = c.toString();
}
else
{
cipher = cipher.concat(cipher_delimiter);
cipher = cipher.concat(c.toString());
}
}
//System.out.println("cipher"+keyChoice+"="+cipher);
return cipher;
}
public static String decode(String cipher,int keyChoice)throws Exception{
String[] cipher_chunks = cipher.split(cipher_delimiter);
String[] txtcode_chunks = new String[cipher_chunks.length];
String source="";
String relayStr;
char data[][]=new char[cipher_chunks.length][10000];
byte mt[][]=new byte[cipher_chunks.length][];
FileInputStream f=new FileInputStream("Skey_RSA_priv_"+keyChoice+".dat");
ObjectInputStream b=new ObjectInputStream(f);
RSAPrivateKey prk=(RSAPrivateKey)b.readObject();
BigInteger d=prk.getPrivateExponent();
BigInteger n=prk.getModulus();
for(int i=0;i<cipher_chunks.length;i++)
{
BigInteger bi = new BigInteger(cipher_chunks[i]);
BigInteger tc = bi.modPow(d,n);
mt[i]=tc.toByteArray();
for(int j=0;j<mt[i].length;j++)
data[i][j]=(char)mt[i][j];
relayStr=new String(data[i]);
relayStr=relayStr.trim();
//System.out.println(relayStr);
source=source.concat(relayStr);
}
//System.out.println("source"+keyChoice+"="+source);
return source;
}
public static void main(String args[])throws Exception{
new PeerFuncImpl();
String sourceStr,relayStr,sendoutStr;
relayStr="";
String[] splitStrGroup1,splitStrGroup2,splitStrGroup3;
String host="localhost";
int port;
try{
System.out.println("Peer starting ...\n");
String numm;
numm=JOptionPane.showInputDialog("Enter your port number");
num=Integer.parseInt(numm);
System.out.println(num);
name=JOptionPane.showInputDialog("Enter your name");
peerUI.setName(name);
peerUI.setSenderPort(""+num);
KeyPairGenerator kpg=KeyPairGenerator.getInstance("RSA");
int keyLen=num%1536+512;
kpg.initialize(keyLen);
KeyPair kp=kpg.genKeyPair();
PublicKey pbkey=kp.getPublic();
PrivateKey prkey=kp.getPrivate();
FileOutputStream f1=new FileOutputStream("Skey_RSA_pub_"+keyLen+".dat");
ObjectOutputStream b1=new ObjectOutputStream(f1);
b1.writeObject(pbkey);
FileOutputStream f2=new FileOutputStream("Skey_RSA_priv_"+keyLen+".dat");
ObjectOutputStream b2=new ObjectOutputStream(f2);
b2.writeObject(prkey);
DatagramSocket s=new DatagramSocket(num);
byte[]data=new byte[20000];
while(true)
{
DatagramPacket receiveD=new DatagramPacket(data,data.length);
s.receive(receiveD);
//System.out.println(new String(data));
sourceStr=new String(receiveD.getData());
//System.out.println("sourceStr="+sourceStr);
sourceStr=sourceStr.trim();
splitStrGroup1=sourceStr.split(";");
int index=0;
int choice=Integer.parseInt(splitStrGroup1[index]);
//for(int i=0;i<splitStrGroup1.length;i++)
// System.out.println(splitStrGroup1[i]);
while(choice==3){
index=index+2;
choice=Integer.parseInt(splitStrGroup1[index]);
}
if(choice==4){
index=index+1;
relayStr=decode(splitStrGroup1[index],keyLen);
System.out.println("relayStr="+relayStr);
}
else{
System.out.println("invalid packet!");
System.exit(0);
}
splitStrGroup2=relayStr.split(";");
choice=Integer.parseInt(splitStrGroup2[0]);
switch(choice){
case 1:
peerUI.setOnionTo(splitStrGroup2[1]);
port=Integer.parseInt(splitStrGroup2[1]);
byte[]buffer;
String tempStr="";
for(int j=0;j<index+2;j++)
{
tempStr=tempStr+"3"+";";
int t=(int)(Math.random()*10000);
tempStr=tempStr+t+";";
j=j+2;
System.out.println(tempStr);
}
for(int i=2;i<splitStrGroup2.length;i++)
{
tempStr=tempStr+splitStrGroup2[i];
if(i!=(splitStrGroup2.length-1))
tempStr=tempStr+";";
}
System.out.println(tempStr);
buffer=tempStr.getBytes();
InetAddress ia=InetAddress.getByName(host);
DatagramPacket sendD=new DatagramPacket(buffer,buffer.length,ia,port);
s.send(sendD);
break;
case 2:
System.out.println(splitStrGroup2[1]);
peerUI.setMessage(splitStrGroup2[1]);
splitStrGroup3=splitStrGroup2[1].split(":");
sFName=splitStrGroup3[1];
System.out.println("sFName="+sFName+"&");
break;
default:
JOptionPane.showMessageDialog(null,"Invalid packet entered");
System.exit(0);
}
data=new byte[20000];
}
}
catch(IOException io_e){
System.out.println(io_e.toString());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -