package com.cn.ssl;
import java.io.*;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import javax.crypto.KeyAgreement;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.interfaces.DHPublicKey;
import javax.crypto.spec.DHParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class myDigest
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
myDigest my=new myDigest();
my.testDigest();
}
public void testDigest()
{
try
{
String myinfo="我的测试信息";
MessageDigest alg = MessageDigest.getInstance("MD5");
MessageDigest alga = MessageDigest.getInstance("SHA-1");
alga.update(myinfo.getBytes());
byte[] digesta=alga.digest();
System.out.println("本信息摘要是:"+byte2hex(digesta));
//通过某中方式传给其他人你的信息
MessageDigest algb = MessageDigest.getInstance("SHA-1");
algb.update(myinfo.getBytes());
if(algb.isEqual(digesta,algb.digest()))
{
System.out.println("信息检查正常");
}
else
{
System.out.println("摘要不相同");
}
}
catch (NoSuchAlgorithmException ex)
{
System.out.println("非法摘要算法");
}
}
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;
}
return hs.toUpperCase();
}
//我自己乱写的方法
public void dsa()
{
try
{
KeyPairGenerator keygen = KeyPairGenerator.getInstance("DSA");
SecureRandom secrand=new SecureRandom();
secrand.setSeed("tttt".getBytes());
//初始化随机产生器
keygen.initialize(512,secrand);
//初始化密钥生成器
//keygen.initialize(512);
KeyPair keys=keygen.generateKeyPair();
//生成密钥组
PublicKey pubkey=keys.getPublic();
PrivateKey prikey=keys.getPrivate();
ObjectInputStream in=new ObjectInputStream(new FileInputStream("e:/qhc/tools/myprikey.dat"));
PrivateKey myprikey= (PrivateKey)in.readObject();
in.close();
String myinfo="要加密的信息";
Signature signet=Signature.getInstance("DSA");
signet.initSign(myprikey);
signet.update(myinfo.getBytes());
byte[] signed=signet.sign();
ObjectOutputStream out1=new ObjectOutputStream(new FileOutputStream("e:/qhc/tools/myinfo.dat"));
out1.writeObject(myinfo);
out1.writeObject(signed);
out1.close();
byte[] bobEncodedPubKey=pubkey.getEncoded();
//生成编码
//传送二进制编码
//以下代码转换编码为相应key对象
X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(bobEncodedPubKey);
KeyFactory keyFactory = KeyFactory.getInstance("DSA");
PublicKey bobPubKey = keyFactory.generatePublic(bobPubKeySpec);
byte[] bPKCS = myprikey.getEncoded();
//传送二进制编码
//以下代码转换编码为相应key对象
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(bPKCS);
KeyFactory keyf = KeyFactory.getInstance("DSA");
PrivateKey otherprikey = keyf.generatePrivate(priPKCS8);
//
KeyGenerator keygen1 = KeyGenerator.getInstance("DSA");
SecretKey deskey = keygen1.generateKey();
Cipher c1 = Cipher.getInstance("DSA");
c1.init(Cipher.ENCRYPT_MODE,deskey);
byte[] cipherByte=c1.doFinal(myinfo.getBytes());
c1 = Cipher.getInstance("DSA");
c1.init(Cipher.DECRYPT_MODE,deskey);
byte[] clearByte=c1.doFinal(cipherByte);
SecretKey deskey1 = keygen1.generateKey();
byte[] desEncode=deskey.getEncoded();
SecretKeySpec destmp1=new SecretKeySpec(desEncode,"DSA");
SecretKey mydeskey=destmp1;
System.out.println("ALICE: 产生 DH 对 ...");
KeyPairGenerator aliceKpairGen = KeyPairGenerator.getInstance("DH");
aliceKpairGen.initialize(512);
KeyPair aliceKpair = aliceKpairGen.generateKeyPair();
DHParameterSpec dhParamSpec =
((DHPublicKey)pubkey).getParams();
KeyPairGenerator bobKpairGen =
KeyPairGenerator.getInstance("DH");
bobKpairGen.initialize(dhParamSpec);
KeyPair bobKpair =
bobKpairGen.generateKeyPair();
KeyAgreement bobKeyAgree =
KeyAgreement.getInstance("DH");
bobKeyAgree.init(bobKpair.getPrivate());
bobKeyAgree.doPhase(pubkey, true);
SecretKey bobDesKey = bobKeyAgree.generateSecret("DES");
byte[] bobPubKeyEnc = bobKpair.getPublic().getEncoded();
KeyAgreement aliceKeyAgree = KeyAgreement.getInstance("DH");
aliceKeyAgree.init(aliceKpair.getPrivate());
aliceKeyAgree.doPhase(bobPubKey, true);
SecretKey aliceDesKey = aliceKeyAgree.generateSecret("DES");
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void signature()
{
/*
public abstract KeyPair generateKeyPair()
产生新密钥对
Java.security.KeyPair 密钥对类
public PrivateKey getPrivate()
返回私钥
public PublicKey getPublic()
返回公钥
Java.security.Signature 签名类
public static Signature
getInstance(String algorithm) throws
NoSuchAlgorithmException
返回一个指定算法的Signature对象
参数 algorithm 如:"DSA"
public final void
initSign(PrivateKey privateKey)
throws InvalidKeyException
用指定的私钥初始化
参数:privateKey 所进行签名时用的私钥
public final void update(byte data)
throws SignatureException
public final void update(byte[] data)
throws SignatureException
public final void update
(byte[] data, int off, int len)
throws SignatureException
添加要签名的信息
public final byte[] sign()
throws SignatureException
返回签名的数组,
前提是initSign和update
public final void initVerify
(PublicKey publicKey)
throws InvalidKeyException
用指定的公钥初始化
参数:publicKey 验证时用的公钥
public final boolean verify
(byte[] signature)
throws SignatureException
验证签名是否有效,
前提是已经initVerify初始化
参数: signature 签名数组
*/
}
}
- 1
- 2
- 3
前往页