?? clientsend.java
字號:
import java.security.Provider;
import java.security.*;
import java.security.spec.*;
import java.io.*;
import javax.crypto.*;
import javax.crypto.Cipher;
public class ClientSend extends Thread{
public static void main(String[] args){
ClientSend my=new ClientSend();
String file="info.dat";
my.run(file);
}
//生成一對文件myprikey.dat和mypubkey.dat---私鑰和公鑰,
//公鑰要用戶發送(文件,網絡等方法)給其它用戶,私鑰保存在本地
public boolean generatekey()
{
try {
KeyPairGenerator keygen=KeyPairGenerator.getInstance("RSA");
keygen.initialize(1024); //密鑰大小
// KeyPair keys=keygen.genKeyPair();
KeyPair keys=keygen.generateKeyPair(); //生成密鑰組
PublicKey pubkey=keys.getPublic();
PrivateKey prikey=keys.getPrivate();
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("MyPrikey.dat"));
out.writeObject(prikey);
out.close();
System.out.println("寫入對象 prikeys ok");
/* 將公鑰pubKey保存在文件pubKey.dat文件中,供接收方使用*/
out=new ObjectOutputStream(new FileOutputStream("MyPubkey.dat"));
out.writeObject(pubkey);
out.close();
System.out.println("寫入對象 pubkeys ok");
System.out.println("生成密鑰對成功");
return true;
}
catch (java.lang.Exception e) {
e.printStackTrace();
System.out.println("生成密鑰對失敗");
return false;
}
}
public String byte2hex(byte[] b)
{
String hs="";
String stmp="";
for (int n=0;n<b.length;n++)
{
stmp=(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 run(String filename)
{
Security.addProvider(new com.sun.crypto.provider.SunJCE());
//第一步生成密鑰對,如果已經生成過,本過程就可以跳過,對用戶來講myprikey.dat要保存在本地
//而mypubkey.dat給發布給其它用戶
if ((new File("MyPrikey.dat")).exists()==false) {
if (generatekey()==false) {
System.out.println("生成密鑰對失敗");
return;
}
}
try {
//從文件中讀取私要進行簽名
ObjectInputStream in=new ObjectInputStream(new FileInputStream("MyPrikey.dat"));
PrivateKey priKey=(PrivateKey)in.readObject();
in.close();
System.out.println("讀私鑰");
/*生成DES算法的密鑰Key*/
KeyGenerator SinglekeyGen = KeyGenerator.getInstance("DES");
SinglekeyGen.init(56);
Key SingleKey = SinglekeyGen.generateKey();
System.out.println("生成DES對稱密鑰");
/*從文件info.dat讀出需要傳輸的原始數據并保存在數組info_Plain[]中*/
RandomAccessFile fin=new RandomAccessFile(filename,"rw");//以只讀形式打開文件
long f1=fin.length(); //文件的長度
int f2=(int)f1;
byte[] info_Plain=new byte[f2]; //建一個字節數組b存放文件
fin.read(info_Plain,0,f2);
fin.close();
System.out.println("原文件信息:"+new String(info_Plain));
// System.out.println("原文件二進制信息:"+byte2hex(info_Plain));
/* 將待傳輸的信息用DES加密,并將加密后的數據保存在數組Des_info[]中*/
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE,SingleKey);
byte[] Des_info= cipher.doFinal(info_Plain);
System.out.println("加密成功");
// System.out.println("明文加密后得到的信息:"+byte2hex(Des_info));
/*將待傳輸的原始信息生成消息摘要MD_1*/
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(info_Plain);
byte[] MD_1 = messageDigest.digest(); //得到消息的摘要MD_1里
System.out.println("消息摘要生成成功");
// System.out.println("經過MD5消息摘要是:"+byte2hex(MD_1));
//把對稱密鑰加到摘要md_1后形成新的字節數組MD_Key;
byte[] MD_Key=JOIN(MD_1,SingleKey);
// System.out.println("合并摘要和加密密鑰后的二進制信息為:"+byte2hex(MD_Key));
/* 將消息摘要MD_1[]和對稱密鑰SingleKey合并到字節數組MD_Key[]中,并用RSA算法中的私鑰對該數組加密并保存在數組Rsa_sign[]中形成了數字簽名*/
Cipher cipher1 = Cipher.getInstance("RSA/ECB/PKCS1Padding","SUN");
System.out.println(cipher.getProvider().getInfo());
cipher1.init(Cipher.ENCRYPT_MODE, priKey);
byte[] Rsa_sign = cipher1.doFinal(MD_Key);
System.out.println("公鑰生成數字簽名成功!");
//System.out.println("RSA加密后的:"+byte2hex(Rsa_sign));
/*將需要傳輸的原文的密文和數字簽名寫到文件En_info.dat中,En_info.dat即為在網上傳輸的信息*/
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("En_info.dat"));
out.writeObject(Des_info);
out.writeObject(Rsa_sign);
out.close();
System.out.println("原文件加密的密文和數字簽名寫到En_info.dat成功#!");
}catch (Exception e) {
e.printStackTrace();
System.out.println(e.toString());
}
}
/* 合并數組的算法(主要就是通過循環將對稱密鑰添加到消息摘要數組的后面,形成一個新的數組MD_Key[],對稱密鑰要通過強制類型轉化為byte類型)*/
public byte[] JOIN(byte[] A,Key deskey)
{ System.out.println("開始合并!");
byte[] B=deskey.toString().getBytes();
System.out.println("密鑰在字符數組B中"+byte2hex(B));
int alen=A.length;
int blen=B.length;
int length=alen+blen;
System.out.println("總長"+alen+blen+(alen+blen));
byte[] C=new byte[length];
int i;
int j=0;
for( i=0;i<length;i++)
{
if(i<alen)
{ C[i]=A[i];
// System.out.println(C[i]);
}
else
{
if(j<blen)
{
C[i]=B[j];
j++;
// System.out.println(C[i]);
}
}
}
System.out.println("合并消息摘要和對稱密鑰成功!");
return C;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -