实习二数字签名与认证的实现.docx
《实习二数字签名与认证的实现.docx》由会员分享,可在线阅读,更多相关《实习二数字签名与认证的实现.docx(16页珍藏版)》请在冰点文库上搜索。
实习二数字签名与认证的实现
实习二数字签名与认证的实现
一、实习目的
1.理解数字签名的概念和作用;
2.理解身份认证的基本方式和方法;
3.掌握Hash函数和数字签名的实现。
二、实习要求
1.实习前认真预习第3章的有关内容;
2.复习数字签名和身份认证相关内容;
3.熟悉Java平台的JCE包有关类。
三、实习内容
假定两个用户A、B,他们的公私钥对分别是KPUa、KPRa和KPUb、KPRb,,分发的消息为M,哈希函数h(x)。
请基于RSA算法实现数字签名,阶梯任务如下:
以本地两个目录模拟两个用户,实现消息M和签名的模拟分发;
实现过程:
A将信息M用自己的私钥加密后与M本身一起发送给B,B将签名用A的公钥解密,得出的信息与M对比,如果一致,说明消息就是A发的,因为只有A知道自己的私钥。
运行结果:
以MD5、SHA-1等哈希函数,实现消息M的摘要,实现M及摘要签名的模拟分发;
实现过程:
A对消息M用散列函数求出摘要,再用自己的私钥对摘要进行签名,将摘要和摘要的签名一起发送给B,B接受后用A的公钥解密摘要签名,将得出的摘要与发送的摘要对比,判断是否是A发送的。
MD5:
SHA-1:
实现M密文状态下的签名与模拟分发;
实现过程:
A对消息M用B的公钥进行加密,将密文用散列函数求出摘要,再用自己的私钥对摘要进行签名,将密文摘要和摘要的签名一起发送给B,B接受后用A的公钥解密摘要签名,将得出的摘要与发送的摘要对比,判断是否是A发送的,之后再使用自己的私钥解密得到的密文,最终得到明文消息M。
运行结果:
采用SSL,建立安全通信过程,实现Socket通信的签名分发;
将方案移植到某个web应用中,实现实用的签名分发。
4、实验总结
通过这次实验,对于数字签名的方式有了更多了解,认识到非对称密钥体系真的是一个很好的体系,既可以做签名,也可以进行加解密,功能很多。
附代码:
SKey_RSA.java
packagecn.test.key;
importjava.io.FileOutputStream;
importjava.io.ObjectOutputStream;
importjava.security.KeyPair;
importjava.security.KeyPairGenerator;
importjava.security.NoSuchAlgorithmException;
importjava.security.PrivateKey;
importjava.security.PublicKey;
publicclassSKey_RSA{
publicvoidkey_RSA()throwsException{
KeyPairGeneratorkpg=KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024);
KeyPairkp=kpg.genKeyPair();
PublicKeypbkey=kp.getPublic();
PrivateKeyprkey=kp.getPrivate();
FileOutputStreamf1=newFileOutputStream("Skey_RSA_pub1.dat");
ObjectOutputStreamoo1=newObjectOutputStream(f1);
oo1.writeObject(pbkey);
FileOutputStreamf2=newFileOutputStream("Skey_RSA_pri1.dat");
ObjectOutputStreamoo2=newObjectOutputStream(f2);
oo2.writeObject(prkey);
oo1.close();
oo2.close();
f1.close();
f2.close();
}
}
Rsa_Op.java
packagecn.test.key;
importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.InputStreamReader;
importjava.io.ObjectInputStream;
importjava.io.OutputStreamWriter;
importjava.math.BigInteger;
importjava.security.Key;
importjava.security.interfaces.RSAPrivateKey;
importjava.security.interfaces.RSAPublicKey;
publicclassRsa_Op{
intflag=0;
publicbyte[]enc_pub(Strings)throwsException{
byte[]b=s.getBytes("UTF8");
FileInputStreamf=newFileInputStream("Skey_RSA_pub1.dat");
ObjectInputStreamoi=newObjectInputStream(f);
RSAPublicKeypbk=(RSAPublicKey)oi.readObject();
oi.close();
BigIntegere=pbk.getPublicExponent();
BigIntegern=pbk.getModulus();
BigIntegerm=newBigInteger(b);
BigIntegerm1=newBigInteger("0");
if((pareTo(m1))==-1){
m=m.negate();
flag=1;
}
BigIntegerc=m.modPow(e,n);
System.out.println("M用公钥加密后的密文:
");
System.out.println("c="+c);
byte[]re=c.toByteArray();
Stringcs=c.toString();
BufferedWriterbw=newBufferedWriter(newOutputStreamWriter(newFileOutputStream("Enc_pub_RSA1.dat")));
bw.write(cs,0,cs.length());
bw.close();
returnre;
}
publicvoidenc_pri(Strings)throwsException{
byte[]b=s.getBytes("UTF8");
FileInputStreamf=newFileInputStream("Skey_RSA_pri1.dat");
ObjectInputStreamoi=newObjectInputStream(f);
RSAPrivateKeyprk=(RSAPrivateKey)oi.readObject();
oi.close();
BigIntegere=prk.getPrivateExponent();
BigIntegern=prk.getModulus();
BigIntegerm=newBigInteger(b);
System.out.println("签名前的M="+m);
BigIntegerm1=newBigInteger("0");
if((pareTo(m1))==-1){
m=m.negate();
flag=1;
}
BigIntegerc=m.modPow(e,n);
System.out.println("签名后的M="+c);
//System.out.println("将密钥加密后的密文c="+c);
Stringcs=c.toString();
BufferedWriterbw=newBufferedWriter(newOutputStreamWriter(newFileOutputStream("Enc_pri_RSA1.dat")));
bw.write(cs,0,cs.length());
bw.close();
}
publicbyte[]dec_pri()throwsException{
BufferedReaderbr=newBufferedReader(newInputStreamReader(newFileInputStream("Enc_pub_RSA1.dat")));
Stringctext=br.readLine();
br.close();
BigIntegerc=newBigInteger(ctext);
FileInputStreamf=newFileInputStream("Skey_RSA_pri1.dat");
ObjectInputStreamoi=newObjectInputStream(f);
RSAPrivateKeyprk=(RSAPrivateKey)oi.readObject();
BigIntegerd=prk.getPrivateExponent();
BigIntegern=prk.getModulus();
BigIntegerm=c.modPow(d,n);
if(flag==1){
m=m.negate();
flag=0;
}
System.out.println("将c解密后得:
");
System.out.println("m="+m);
byte[]mt=m.toByteArray();
oi.close();
f.close();
returnmt;
}
publicbyte[]dec_pub()throwsException{
BufferedReaderbr=newBufferedReader(newInputStreamReader(newFileInputStream("Enc_pri_RSA1.dat")));
Stringctext=br.readLine();
br.close();
BigIntegerc=newBigInteger(ctext);
FileInputStreamf=newFileInputStream("Skey_RSA_pub1.dat");
ObjectInputStreamoi=newObjectInputStream(f);
RSAPublicKeypbk=(RSAPublicKey)oi.readObject();
BigIntegerd=pbk.getPublicExponent();
BigIntegern=pbk.getModulus();
BigIntegerm=c.modPow(d,n);
if(flag==1){
m=m.negate();
flag=0;
}
System.out.println("公钥解密后的M="+m);
byte[]b=m.toByteArray();
oi.close();
f.close();
returnb;
}
}
Sig.java:
packagecn.test.key;
importjava.io.BufferedReader;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.FileReader;
importjava.io.ObjectInputStream;
importjava.security.NoSuchAlgorithmException;
importjava.security.Signature;
importjava.security.interfaces.RSAPrivateKey;
publicclassSig{
publicvoidsigNoz(Strings,Rsa_Opro)throwsException{
SKey_RSAsr=newSKey_RSA();
sr.key_RSA();
ro.enc_pri(s);
}
publicvoidsigWithz(Stringalgorithm,Stringmsg)throwsException{
FileInputStreamf=newFileInputStream("Skey_RSA_pri1.dat");
ObjectInputStreamoi=newObjectInputStream(f);
RSAPrivateKeyprk=(RSAPrivateKey)oi.readObject();
oi.close();
Signatures=Signature.getInstance(algorithm);
s.initSign(prk);
byte[]b=msg.getBytes("UTF8");
s.update(b);
byte[]b1=s.sign();
System.out.println("摘要签名:
");
for(inti=0;iSystem.out.print(b1[i]+",");
}
FileOutputStreamf3=newFileOutputStream("Sign.dat");
f3.write(b1);
}
publicvoidsigWithz(Stringalgorithm,byte[]msg)throwsException{
FileInputStreamf=newFileInputStream("Skey_RSA_pri1.dat");
ObjectInputStreamoi=newObjectInputStream(f);
RSAPrivateKeyprk=(RSAPrivateKey)oi.readObject();
oi.close();
Signatures=Signature.getInstance(algorithm);
s.initSign(prk);
s.update(msg);
byte[]b1=s.sign();
System.out.println("密文摘要签名:
");
for(inti=0;iSystem.out.print(b1[i]+",");
}
FileOutputStreamf3=newFileOutputStream("Sign.dat");
f3.write(b1);
}
}
Dec_Sig.java:
packagecn.test.key;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.ObjectInputStream;
importjava.security.Signature;
importjava.security.interfaces.RSAPublicKey;
publicclassDec_Sig{
publicStringdec_SigNoz(Rsa_Opro)throwsException{
byte[]ss=ro.dec_pub();
Strings=newString(ss,"UTF8");
returns;
}
publicbooleandec_SigWithz(Stringmsg,Stringalgorithm)throwsException{
byte[]data=msg.getBytes("UTF8");
FileInputStreamf2=newFileInputStream("Sign.dat");
intnum=f2.available();
byte[]b=newbyte[num];
f2.read(b);
FileInputStreamf=newFileInputStream("Skey_RSA_pub1.dat");
ObjectInputStreamoi=newObjectInputStream(f);
RSAPublicKeypbk=(RSAPublicKey)oi.readObject();
Signatures=Signature.getInstance(algorithm);
s.initVerify(pbk);
s.update(data);
booleanresult=s.verify(b);
returnresult;
}
publicbooleandec_SigWithz(byte[]msg,Stringalgorithm)throwsException{
FileInputStreamf2=newFileInputStream("Sign.dat");
intnum=f2.available();
byte[]b=newbyte[num];
f2.read(b);
FileInputStreamf=newFileInputStream("Skey_RSA_pub1.dat");
ObjectInputStreamoi=newObjectInputStream(f);
RSAPublicKeypbk=(RSAPublicKey)oi.readObject();
Signatures=Signature.getInstance(algorithm);
s.initVerify(pbk);
s.update(msg);
booleanresult=s.verify(b);
returnresult;
}
}
Test1.java:
packagecn.test.key;
importjava.io.BufferedReader;
importjava.io.FileReader;
publicclassTest1{
publicstaticvoidmain(String[]args)throwsException{
//TODOAuto-generatedmethodstub
FileReaderf=newFileReader("消息M.txt");
BufferedReaderbr=newBufferedReader(f);
Stringss=br.readLine();
Rsa_Opro=newRsa_Op();
Sigs=newSig();
s.sigNoz(ss,ro);
Dec_Sigds=newDec_Sig();
Stringdecs=ds.dec_SigNoz(ro);
System.out.println("签名解密后得到的消息M'="+decs);
if(ss.equals(decs)){
System.out.println("验证签名成功!
!
");
}
}
}
Test2.java:
packagecn.test.key;
importjava.io.BufferedReader;
importjava.io.FileNotFoundException;
importjava.io.FileReader;
publicclassTest2{
publicstaticvoidmain(String[]args)throwsException{
//TODOAuto-generatedmethodstub
SKey_RSAsr=newSKey_RSA();
sr.key_RSA();
FileReaderf=newFileReader("消息M.txt");
BufferedReaderbr=newBufferedReader(f);
Stringss=br.readLine();
Rsa_Opro=newRsa_Op();
Sigs=newSig();
System.out.println("使用SHA-1算法生成摘要");
s.sigWithz("SHA1WithRSA",ss);
Dec_Sigds=newDec_Sig();
booleanok=ds.dec_SigWithz(ss,"SHA1WithRSA");
if(ok==true){
System.out.println();
System.out.println("验证签名成功!
!
");
}
}
}
Test3.java:
packagecn.test.key;
importjava.io.BufferedReader;
importjava.io.FileReader;
publicclassTest3{
/**
*@paramargs
*@throwsException
*/
pub