Java中对称密钥、非对称密钥和数字签名的用法

1、非对称密钥:

 

package com.mysec;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import static com.mysec.myutil.Util.println;
public class MDTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		try {
			byte[] pw1 = new String("dkfdsfsdflasdfasdfasdf").getBytes();
			byte[] pw2 = new String("dkfdsfsdflasdfasdfasdf").getBytes();
			//MD2/MD5/SHA1
			//非对称密钥
			MessageDigest md = MessageDigest.getInstance("MD5");
			System.out.println("服务提供者:"+md.getProvider());
			byte[] b1 = new String("这是散列字节").getBytes();
			
			md.update(b1);
			byte[] r1 = md.digest(pw1);
			
			md.update(b1);
			byte[] r2 = md.digest(pw2);
			
			println("pw1加密后:"+new String(r1));
			println("pw2加密后:"+new String(r2));
			//转换为16进制方便读取放入数据库
			println(convertHex(r1));
			println(convertHex(r2));
			println(md.isEqual(r1, r2));
			
			//println(md.getAlgorithm());
			//println(md.getDigestLength());
			
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static String convertHex(byte[] b){
		StringBuffer sb = new StringBuffer();
		for(byte tmp:b){
			sb.append(Integer.toHexString(tmp&0XFF));
		}
		return sb.toString();
	}

}

 

 2、对称密钥算法

 

package com.mysec;
import static com.mysec.myutil.Util.println;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
public class CipherTest {

	/**
	 * @param args
	 * @throws NoSuchPaddingException 
	 * @throws NoSuchAlgorithmException 
	 */
	public static byte[] encoder(Key key,String text){
		return null;
	}
	public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
		// TODO Auto-generated method stub
		//对称算法名  
		String info = "this is a my info!";
		KeyGenerator generator = KeyGenerator.getInstance("DES");
		//初始化长度,DES只能是56
		generator.init(56);
		//生成密钥
		Key key = generator.generateKey();
		byte[] keyEncode = key.getEncoded();
		println("密钥是:"+toHexString(keyEncode));
		
		Cipher cipher  = Cipher.getInstance("DES/ECB/PKCS5Padding");
		println(cipher.getProvider().getName());
		
		println("原文:"+info);
		//用cipher进行加密
		cipher.init(Cipher.ENCRYPT_MODE,key);
		byte[] encoderResult = cipher.doFinal(info.getBytes());
		println("密文:"+toHexString(encoderResult));
		
		//用cipher进行解密
		cipher.init(Cipher.DECRYPT_MODE, key);
		byte[] decryptResult = cipher.doFinal(encoderResult);
		println("解密后:"+new String(decryptResult));
	}
	
	public static String toHexString(byte[] b){
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<b.length;i++){
			sb.append(Integer.toHexString(b[i]&0XFF));
		}
		return sb.toString();
	}

}

 

 3、数字签名

package com.mysec;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import static com.mysec.myutil.Util.println;
public class SignTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		String info = "this is my info!";
		
		//获得密钥对
		KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
		//初始化长度
		generator.initialize(1024);
		//生成密钥对
		KeyPair keyPair = generator.generateKeyPair();
		//获得私钥
		PrivateKey privateKey = keyPair.getPrivate();
		println("私钥是:"+toHexString(privateKey.getEncoded()));
		//获得公钥
		PublicKey publicKey = keyPair.getPublic();
		println("公钥是:"+toHexString(publicKey.getEncoded()));
		
		
		Signature sign = Signature.getInstance("MD5WithRSA");
		//对私钥进行签名
		sign.initSign(privateKey);
		sign.update(info.getBytes());
		//获得签名值
		byte[] signature = sign.sign();
		println("签名值是:"+toHexString(signature));
		
		//用公钥初始化签名对象
		sign.initVerify(publicKey);
		//这里应该是解密后的信息,这里直接用原来的
		sign.update(info.getBytes());
		
		//签证签名
		if(sign.verify(signature)){
			System.out.println("验证签名成功!");
		}else{
			System.out.println("验证签名失败!");
			
		}
	}
	
	public static String toHexString(byte[] b){
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<b.length;i++){
			sb.append(Integer.toHexString(b[i]&0XFF));
		}
		return sb.toString();
	}

}
 

 

你可能感兴趣的:(java,算法,Security)