一、历史背景

父辈或以前的前辈们经常有刻有姓名等证明身份的图案的印章,每当工作的时候,往往会盖这个印章,而不是亲笔签名。(大卫亚设)。

印戳

到后来这方方式逐渐被淘汰掉了,改成了手写签名。为什么会被淘汰?主要原因是这样的形式的戳比较容易被伪造,只要找到刻章工匠们就可以做出一模一样的戳且无法辨认真实性。使用手写签名之后安全性和不可抵赖性得到了提高。

手写签名

二、数字签名

定义:带有密钥(公钥、私钥)的消息摘要算法

作用:验证数据完整性、认证数据来源、不可抵赖

说明:遵循使用私钥进行签名,使用公钥对签名的数据进行验证的规则。常用的数字签名算法有:RSA、DSA、ECDSA()

三、算法例子

1、MD5withRSA

package com.yx.signature;

import java.security.*;
import java.;
import java.;
import java.;
import java.;
import java.u;

/**
* RMD5withRSA数字签名使用例子
* 签名,验签
*
* @author yx
*/
public class MD5withRSADemo {

private static final char[] HEXES = {
'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
'c', 'd', 'e', 'f'
};

/**
* byte数组 转换成 16进制小写字符串
*/
public static String bytes2Hex(byte[] bytes) {
if (bytes == null || by == 0) {
return null;
}
StringBuilder hex = new StringBuilder();
for (byte b : bytes) {
(HEXES[(b >> 4) & 0x0F]);
(HEXES[b & 0x0F]);
}
return ();
}

public static void main(String[] args) throws Exception {
// 加密字符串
String src = "EasyJava";

KeyPairGenerator keyPairGenerator = KeyPairGenera("RSA");
keyPairGenera(512);
KeyPair keyPair = keyPairGenera();

// 初始化公钥和私钥
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
Sy("公钥:" + Ba().encodeToString()));
Sy("私钥:" + Ba().encodeToString()));

// 私钥签名
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec());
KeyFactory keyFactory = KeyFac("RSA");
PrivateKey privateKey = keyFac(pkcs8EncodedKeySpec);
Signature signature = Signa("MD5withRSA");
(privateKey);
("utf-8"));
byte[] signResult = ();
Sy("签名结果:" + bytes2Hex(signResult));

// 公钥验证
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec());
keyFactory = KeyFac("RSA");
PublicKey publicKey = keyFac(x509EncodedKeySpec);
signature = Signa("MD5withRSA");
(publicKey);
());
boolean verifyResult = (signResult);
Sy("验证结果:" + verifyResult);
}
}

执行结果:

公钥:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAINcPA9dHlPQn17iMwTPJxU5Hyd/OecYjW8NLP8GH5e0hpfax51JWQV9C5sKBzahjR5/49hVv3GbGoqYg2NVGLcCAwEAAQ==
私钥:MIIBUwIBADANBgkqhkiG9w0BAQEFAASCAT0wggE5AgEAAkEAg1w8D10eU9CfXuIzBM8nFTkfJ3855xiNbw0s/wYfl7SGl9rHnUlZBX0LmwoHNqGNHn/j2FW/cZsaipiDY1UYtwIDAQABAkBT83J+7ygesMnpve6VOD7mFWePuoOq6coaGvWS1rsCHHbInY04kvZxlZBTf4Y69qAoVbjqGL0Dh6TouhWyyi6hAiEAwp88R0bHZlG3giz5iN5U1IgWdTk9NG3vcf9NkJSPv+0CIQCsyZQGsZn8pkBnOFPc675D4HqvYveLA0A6pVxqD+C+swIgDncVXNrW4TT7pbJADbswpOdEfAv4D5iILnniQve3w3UCIEeq6KIKIlO/5XCq1WYJxgZDYr/CbeiT9Z0dn4JCibavAiBJ+rwnhiHglwmh0jrpt9PgGkZ+6iy1gr6aHAxlu6jxpw==
签名结果:193cc28101b94bfd13564272d39d1eb53509d6bd621d2eae8a2be20b130663d4c7b1bab92ee0e7ebf7a1e5c2d4cf4ceb09ff7eb5ecc30adf8d51c9bc3909081f
验证结果:true

2、SHA1withDSA

package com.yx.signature;

import java.security.*;
import java.;
import java.;
import java.;
import java.;
import java.u;

/**
* SHA1withDSA数字签名使用例子
* 签名,验签
*
* @author yx
*/
public class SHA1withDSADemo {

private static final char[] HEXES = {
'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
'c', 'd', 'e', 'f'
};

/**
* byte数组 转换成 16进制小写字符串
*/
public static String bytes2Hex(byte[] bytes) {
if (bytes == null || by == 0) {
return null;
}
StringBuilder hex = new StringBuilder();
for (byte b : bytes) {
(HEXES[(b >> 4) & 0x0F]);
(HEXES[b & 0x0F]);
}
return ();
}

public static void main(String[] args) throws Exception {
// 加密字符串
String src = "EasyJava";

KeyPairGenerator keyPairGenerator = KeyPairGenera("DSA");
keyPairGenera(512);
KeyPair keyPair = keyPairGenera();

// 初始化公钥和私钥
DSAPublicKey dsaPublicKey = (DSAPublicKey) keyPair.getPublic();
DSAPrivateKey dsaPrivateKey = (DSAPrivateKey) keyPair.getPrivate();
Sy("公钥:" + Ba().encodeToString()));
Sy("私钥:" + Ba().encodeToString()));

// 私钥签名
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec());
KeyFactory keyFactory = KeyFac("DSA");
PrivateKey privateKey = keyFac(pkcs8EncodedKeySpec);
Signature signature = Signa("SHA1withDSA");
(privateKey);
());
byte[] signResult = ();
Sy("签名结果:" + bytes2Hex(signResult));

// 公钥验证
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec());
keyFactory = KeyFac("DSA");
PublicKey publicKey = keyFac(x509EncodedKeySpec);
signature = Signa("SHA1withDSA");
(publicKey);
());
boolean verifyResult = (signResult);
Sy("验证结果:" + verifyResult);
}
}

执行结果:

公钥:MIHxMIGoBgcqhkjOOAQBMIGcAkEA/KaCzo4Syrom78z3EQ5SbbB4sF7ey80etKII864WF64B81uRpH5t9jQTxeEu0ImbzRMqzVDZkVG9xD7nN1kuFwIVAJYu3cw2nLqOuyYO5rahJtk0bjjFAkBnhHGyepz0TukaScUUfbGpqvJE8FpDTWSGkx0tFCcbnjUDC3H9c9oXkGmzLik1Yw4cIGI1TQ2iCmxBblC+eUykA0QAAkEAztIwIQWBN9CKDdDaf2o/nQxgcfnTXMKZnzCbWpPS0SVoJ4PPVm2KivFqv/vbJYQuQpg3rNlaMbXmf0mtodI4iQ==
私钥:MIHGAgEAMIGoBgcqhkjOOAQBMIGcAkEA/KaCzo4Syrom78z3EQ5SbbB4sF7ey80etKII864WF64B81uRpH5t9jQTxeEu0ImbzRMqzVDZkVG9xD7nN1kuFwIVAJYu3cw2nLqOuyYO5rahJtk0bjjFAkBnhHGyepz0TukaScUUfbGpqvJE8FpDTWSGkx0tFCcbnjUDC3H9c9oXkGmzLik1Yw4cIGI1TQ2iCmxBblC+eUykBBYCFGfMn8teKDz6Tvj356ARoniYW4RB
签名结果:302c0214110c199ba3e2aeb580e7b83c54c7b658601f34060214155e9e33c96d93e701639dbbd9247adc949aeb7e
验证结果:true

3、SHA1withECDSA

package com.yx.signature;

import java.security.*;
import java.;
import java.;
import java.;
import java.;
import java.u;

/**
* SHA1withECDSA数字签名使用例子
* 签名,验签
*
* @author yx
*/
public class SHA1withECDSADemo {

private static final char[] HEXES = {
'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
'c', 'd', 'e', 'f'
};

/**
* byte数组 转换成 16进制小写字符串
*/
public static String bytes2Hex(byte[] bytes) {
if (bytes == null || by == 0) {
return null;
}
StringBuilder hex = new StringBuilder();
for (byte b : bytes) {
(HEXES[(b >> 4) & 0x0F]);
(HEXES[b & 0x0F]);
}
return ();
}

public static void main(String[] args) throws Exception {
// 加密字符串
String src = "EasyJava";

KeyPairGenerator keyPairGenerator = KeyPairGenera("EC");
keyPairGenera(256);
KeyPair keyPair = keyPairGenera();

// 初始化公钥和私钥
ECPublicKey ecPublicKey = (ECPublicKey) keyPair.getPublic();
ECPrivateKey ecPrivateKey = (ECPrivateKey) keyPair.getPrivate();
Sy("公钥:" + Ba().encodeToString()));
Sy("私钥:" + Ba().encodeToString()));

// 私钥签名
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec());
KeyFactory keyFactory = KeyFac("EC");
PrivateKey privateKey = keyFac(pkcs8EncodedKeySpec);
Signature signature = Signa("SHA1withECDSA");
(privateKey);
());
byte[] signResult = ();
Sy("签名结果:" + bytes2Hex(signResult));

// 公钥验证
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec());
keyFactory = KeyFac("EC");
PublicKey publicKey = keyFac(x509EncodedKeySpec);
signature = Signa("SHA1withECDSA");
(publicKey);
());
boolean verifyResult = (signResult);
Sy("验证结果:" + verifyResult);
}
}

执行结果:

公钥:MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEMz4FvvFpr8sIOVJE6BbloaLAMMBfUppeTEuH4KNjkAwV5I5yrkSRz9c4AU6LhqSiKn5teMzOfFqDzI+0Jkx4qA==
私钥:MEECAQAwEwYHKoZIzj0CAQYIKoZIzj0DAQcEJzAlAgEBBCDwwpzQDrwSTAsAtvDI0RnRpWXGu+MzMPFMkoHpD0EDsA==
签名结果:3045022100bdd946f1748a90c9e0241a804b1f75f065d06426d1a7e374003691128c6924db022013eb70b7e0b4fb8d5e104ba954305c339c59234d9e0acc6c27cb7010b384416e
验证结果:true

1.《【ff7cc】Java编程实现数字签名》援引自互联网,旨在传递更多网络信息知识,仅代表作者本人观点,与本网站无关,侵删请联系页脚下方联系方式。

2.《【ff7cc】Java编程实现数字签名》仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证。

3.文章转载时请保留本站内容来源地址,https://www.cxvn.com/gl/djyxgl/189417.html