签名算法

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

import org.apache.commons.codec.binary.Base64;

/*
 * RSA签名算法。私钥签名,公钥验证签名
 */
public class RSA {
	public static final String src="xiaoweiba";
	public static void main(String[] args) throws Exception {
		jdkRSA();
	}
	
	public static void jdkRSA() throws Exception{
		//初始化密钥对
		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
		keyPairGenerator.initialize(512);
		KeyPair keyPair = keyPairGenerator.generateKeyPair();
		RSAPublicKey rsaPublicKey = (RSAPublicKey)keyPair.getPublic();
		RSAPrivateKey rsaPrivateKey = (RSAPrivateKey)keyPair.getPrivate();
		System.out.println("Public key:"+Base64.encodeBase64String(rsaPublicKey.getEncoded()));
		System.out.println("Private key:"+Base64.encodeBase64String(rsaPrivateKey.getEncoded()));
		
		//私钥签名
		PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
		PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
		Signature signature = Signature.getInstance("MD5withRSA");
		signature.initSign(privateKey);
		signature.update(src.getBytes());
	    byte[] result = signature.sign();
	    System.out.println("jdk rsa sign:"+Base64.encodeBase64String(result));
		
		//验证签名
		X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
		keyFactory = KeyFactory.getInstance("RSA");
		PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
	    signature = Signature.getInstance("MD5withRSA");
		signature.initVerify(publicKey);
		signature.update(src.getBytes());
		boolean bool = signature.verify(result);
		System.out.println("jdk rsa verify:"+ bool);	
}
}

结果:

Public key:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMCWyz3Pidv34P+fyHY1S2g8C3rdOqJEFQ5noq0z2MhwNyGfegCyy6yx4iiJhNhRUdCBnLzn2xijSrECLHfdFicCAwEAAQ==
Private key:MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAwJbLPc+J2/fg/5/IdjVLaDwLet06okQVDmeirTPYyHA3IZ96ALLLrLHiKImE2FFR0IGcvOfbGKNKsQIsd90WJwIDAQABAkBdD4CeGY8QD7NVUixXkmEhuQIDGKughS13jHR7JTZ3g4mKPaP8HwaVHFFMji5K/noPNe32jVKXe5XmQ0ICzrHBAiEA/D/rf+f5RL7m2nG6uCZS+Zg0ICm9qYs1evYQBP5Egc8CIQDDc80l9iqEiPKW2zB+kDJdJ1Oe8Y7P9Rx5/nP6m+X0KQIhAOgglkHlIfEOT/iTkgUmV1MSql0yrDNaRMO7rjA6IKbnAiA6fUD7BzuaBdFyy40rksM/lTE081ylZthquXbDnDuoYQIhAPXR6C7bVkRctGJKmLjypjX/A9oK0RLI9Pmoy0TA1gEa
jdk rsa sign:QADWxax8VZILuaCmMwaKVRazvMDliLjBVOgw3tmaJGKxnTqynAZMrvWvc5tBmk6hEEwyN9Qk7RWGlyqr75Hy8A==
jdk rsa verify:true

 

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.DSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import org.apache.commons.codec.binary.Base64;

/*
 * DSA签名算法。私钥签名,公钥验证签名
 */
public class DSA {
	public static final String src="xiaoweiba";
	public static void main(String[] args) throws Exception {
		jdkDSA();
	}
	
	public static void jdkDSA() throws Exception{
		//初始化密钥
		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA");
		keyPairGenerator.initialize(512);
		KeyPair keyPair = keyPairGenerator.generateKeyPair();
		DSAPublicKey dsaPublicKey = (DSAPublicKey)keyPair.getPublic();
		DSAPrivateKey dsaPrivateKey = (DSAPrivateKey)keyPair.getPrivate();
		System.out.println("Public key:"+Base64.encodeBase64String(dsaPublicKey.getEncoded()));
		System.out.println("Private key:"+Base64.encodeBase64String(dsaPrivateKey.getEncoded()));
		
		//私钥签名
		PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(dsaPrivateKey.getEncoded());
		KeyFactory keyFactory = KeyFactory.getInstance("DSA");
		PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
		Signature signature = Signature.getInstance("SHA1withDSA");
		signature.initSign(privateKey);
		signature.update(src.getBytes());
	    byte[] result = signature.sign();
	    System.out.println("jdk dsa sign:"+Base64.encodeBase64String(result));
	
	    //公钥验证
	    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(dsaPublicKey.getEncoded());
		keyFactory = KeyFactory.getInstance("DSA");
		PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
	    signature = Signature.getInstance("SHA1withDSA");
		signature.initVerify(publicKey);
		signature.update(src.getBytes());
		boolean bool = signature.verify(result);
		System.out.println("jdk dsa verify:"+ bool);
	}
}


结果:

 

Public key:MIHxMIGoBgcqhkjOOAQBMIGcAkEA/KaCzo4Syrom78z3EQ5SbbB4sF7ey80etKII864WF64B81uRpH5t9jQTxeEu0ImbzRMqzVDZkVG9xD7nN1kuFwIVAJYu3cw2nLqOuyYO5rahJtk0bjjFAkBnhHGyepz0TukaScUUfbGpqvJE8FpDTWSGkx0tFCcbnjUDC3H9c9oXkGmzLik1Yw4cIGI1TQ2iCmxBblC+eUykA0QAAkEAp9FKQqjTSnoUcjfyFjc9y8a/Vte+LQ7xTJUJ6ZoZMgGKBqy50rdcTbhw1YxKrmdH/kOXThzAx8eJkDcp/s8fXQ==
Private key:MIHGAgEAMIGoBgcqhkjOOAQBMIGcAkEA/KaCzo4Syrom78z3EQ5SbbB4sF7ey80etKII864WF64B81uRpH5t9jQTxeEu0ImbzRMqzVDZkVG9xD7nN1kuFwIVAJYu3cw2nLqOuyYO5rahJtk0bjjFAkBnhHGyepz0TukaScUUfbGpqvJE8FpDTWSGkx0tFCcbnjUDC3H9c9oXkGmzLik1Yw4cIGI1TQ2iCmxBblC+eUykBBYCFGgDck5hZOOS9YSi8oFtbH0+cTZw
jdk dsa sign:MC0CFFw8owyCnQW2mFLUK8neId/I0mjuAhUAlJUmu8oMYClmMrYS+WaR/xnsVcg=
jdk dsa verify:true

 

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import org.apache.commons.codec.binary.Base64;

/*
 * ECDSA签名算法。私钥签名,公钥验证签名
 */
public class ECDSA {
	public static final String src="xiaoweiba";
	public static void main(String[] args) throws Exception {
		jdkECDSA();
	}
	
	public static void jdkECDSA() throws Exception{
		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");
		keyPairGenerator.initialize(256);
		KeyPair keyPair = keyPairGenerator.generateKeyPair();
		ECPublicKey ecPublicKey = (ECPublicKey)keyPair.getPublic();
		ECPrivateKey ecPrivateKey = (ECPrivateKey)keyPair.getPrivate();
		System.out.println("Public key:"+Base64.encodeBase64String(ecPublicKey.getEncoded()));
		System.out.println("Private key:"+Base64.encodeBase64String(ecPrivateKey.getEncoded()));
		
		//私钥签名
		PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(ecPrivateKey.getEncoded());
		KeyFactory keyFactory = KeyFactory.getInstance("EC");
		PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
		Signature signature = Signature.getInstance("SHA1withECDSA");
		signature.initSign(privateKey);
		signature.update(src.getBytes());
	    byte[] result = signature.sign();
	    System.out.println("jdk ec sign:"+Base64.encodeBase64String(result));
		
		//验证签名
		X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(ecPublicKey.getEncoded());
		keyFactory = KeyFactory.getInstance("EC");
		PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
	    signature = Signature.getInstance("SHA1withECDSA");
		signature.initVerify(publicKey);
		signature.update(src.getBytes());
		boolean bool = signature.verify(result);
		System.out.println("jdk ec verify:"+ bool);	

		
	}
}

结果:

 

Public key:MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEel5iRZbnRpw3fqvXUw8kq/drRtTazENs5HWC9Jw4dmh4HaBCAD0DDyMN/jMoP7LWmYTzpx8rRSXiYR+p+DCsAw==
Private key:MEECAQAwEwYHKoZIzj0CAQYIKoZIzj0DAQcEJzAlAgEBBCCWdYJoPIbPFJNPWel78xoKzssf+4q4sE0x+YpQvaFC5A==
jdk ec sign:MEQCICmGmJgI5C5hlOOwn8u8pn1SvcYx3XFsPk+rYjiiHEmDAiA6j2M8ilw94RgY57SigfpzS/UOekCfEJpjyUXp6Oy/Bg==
jdk ec verify:true

最近在整理一些资源工具,放在网站分享 http://tools.maqway.com
欢迎关注公众号:麻雀唯伊 , 不定时更新资源文章,生活优惠,或许有你想看的

你可能感兴趣的:(java)