当前位置: 移动技术网 > IT编程>开发语言>Java > 常用数字签名算法RSA与DSA的Java程序内实现示例

常用数字签名算法RSA与DSA的Java程序内实现示例

2019年07月22日  | 移动技术网IT编程  | 我要评论

rsa加密算法
我们来回顾一下rsa的加密算法。我们从公钥加密算法和签名算法的定义出发,用比较规范的语言来描述这一算法。
rsa公钥加密体制包含如下3个算法:keygen(密钥生成算法),encrypt(加密算法)以及decrypt(解密算法)。
密钥生成算法以安全常数作为输入,输出一个公钥pk,和一个私钥sk。安全常数用于确定这个加密算法的安全性有多高,一般以加密算法使用的质数p的大小有关。越大,质数p一般越大,保证体制有更高的安全性。在rsa中,密钥生成算法如下:算法首先随机产生两个不同大质数p和q,计算n=pq。随后,算法计算欧拉函数。接下来,算法随机选择一个小于的整数e,并计算e关于的模反元素d。最后,公钥为pk=(n, e),私钥为sk=(n, d)。
加密算法以公钥pk和待加密的消息m作为输入,输出密文ct。在rsa中,加密算法如下:算法直接输出密文为
解密算法以私钥sk和密文ct作为输入,输出消息m。在rsa中,解密算法如下:算法直接输出明文为。由于e和d在下互逆,因此我们有:
所以,从算法描述中我们也可以看出:公钥用于对数据进行加密,私钥用于对数据进行解密。当然了,这个也可以很直观的理解:公钥就是公开的密钥,其公开了大家才能用它来加密数据。私钥是私有的密钥,谁有这个密钥才能够解密密文。否则大家都能看到私钥,就都能解密,那不就乱套了。
下面就来看一下java中的简单实现:

package com.stone.security; 
 
import java.security.keypair; 
import java.security.keypairgenerator; 
import java.security.privatekey; 
import java.security.publickey; 
import java.util.arrays; 
 
import javax.crypto.cipher; 
 
/** 
 * rsa算法 公钥加密 非对称加密 
 */ 
public class rsa { 
 public static final string key_algorithm = "rsa"; 
 public static final string cipher_algorithm_ecb1 = "rsa/ecb/pkcs1padding"; 
 public static final string cipher_algorithm_ecb2 = "rsa/ecb/oaepwithsha-1andmgf1padding"; //不能用 
 public static final string cipher_algorithm_ecb3 = "oaepwithsha-256andmgf1padding"; //不能用 
 
 static publickey publickey; 
 static privatekey privatekey; 
 static cipher cipher; 
 static keypair keypair; 
 
 public static void main(string[] args) throws exception { 
 method1("斯柯达u*(sfsad7f()*^%%$"); 
 method2("斯柯达u*(sfsad7f()*^%%$"); 
 method3("斯柯达u*(sfsad7f()*^%%$"); 
  
 } 
 
 /** 
 * 公钥加密,私钥解密 使用默认cipher_algorithm_ecb1 
 * @param str 
 * @throws exception 
 */ 
 static void method1(string str) throws exception { 
 keypairgenerator keygenerator = keypairgenerator.getinstance(key_algorithm); 
 keypair keypair = keygenerator.generatekeypair(); 
 publickey = keypair.getpublic(); 
 privatekey = keypair.getprivate(); 
 cipher = cipher.getinstance(key_algorithm); 
 cipher.init(cipher.encrypt_mode, publickey); //公钥加密 
 byte[] encrypt = cipher.dofinal(str.getbytes()); 
 system.out.println("公钥加密后1:" + arrays.tostring(encrypt)); 
  
 cipher.init(cipher.decrypt_mode, privatekey);//私钥解密 
 byte[] decrypt = cipher.dofinal(encrypt); 
 system.out.println("私钥解密后1:" + new string(decrypt)); 
 } 
 
 /** 
 * 私钥加密,公钥解密 使用默认cipher_algorithm_ecb1 
 * @param str 
 * @throws exception 
 */ 
 static void method2(string str) throws exception { 
 keypairgenerator keygenerator = keypairgenerator.getinstance(key_algorithm); 
 keypair keypair = keygenerator.generatekeypair(); 
 publickey = keypair.getpublic(); 
 privatekey = keypair.getprivate(); 
 cipher = cipher.getinstance(key_algorithm); 
 cipher.init(cipher.encrypt_mode, privatekey); //私钥加密 
 byte[] encrypt = cipher.dofinal(str.getbytes()); 
 system.out.println("私钥加密后2:" + arrays.tostring(encrypt)); 
  
 cipher.init(cipher.decrypt_mode, publickey);//公钥解密 
 byte[] decrypt = cipher.dofinal(encrypt); 
 system.out.println("公钥解密后2:" + new string(decrypt)); 
 } 
 
 /** 
 * 私钥加密,公钥解密 使用cipher_algorithm_ecb1 = rsa/ecb/pkcs1padding 
 * @param str 
 * @throws exception 
 */ 
 static void method3(string str) throws exception { 
 keypairgenerator keygenerator = keypairgenerator.getinstance(key_algorithm); 
 keypair keypair = keygenerator.generatekeypair(); 
 publickey = keypair.getpublic(); 
 privatekey = keypair.getprivate(); 
 cipher = cipher.getinstance(cipher_algorithm_ecb1); 
 cipher.init(cipher.encrypt_mode, privatekey); //私钥加密 
 byte[] encrypt = cipher.dofinal(str.getbytes()); 
 system.out.println("私钥加密后3:" + arrays.tostring(encrypt)); 
  
 cipher.init(cipher.decrypt_mode, publickey);//公钥解密 
 byte[] decrypt = cipher.dofinal(encrypt); 
 system.out.println("公钥解密后3:" + new string(decrypt)); 
 } 
} 

dsa算法和数字签名
dsa 一般用于数字签名和认证。
dsa是schnorr和elgamal签名算法的变种,被美国nist作为dss(digitalsignature standard)。
dsa是基于整数有限域离散对数难题的,其安全性与rsa相比差不多。
在dsa数字签名和认证中,发送者使用自己的私钥对文件或消息进行签名,接受者收到消息后使用发送者的公钥
来验证签名的真实性。dsa只是一种算法,和rsa不同之处在于它不能用作加密和解密,也不能进行密钥交换,
只用于签名,它比rsa要快很多.

package com.stone.security; 
 
import java.security.key; 
import java.security.keyfactory; 
import java.security.keypair; 
import java.security.keypairgenerator; 
import java.security.privatekey; 
import java.security.publickey; 
import java.security.securerandom; 
import java.security.signature; 
import java.security.spec.pkcs8encodedkeyspec; 
import java.security.spec.x509encodedkeyspec; 
import java.util.hashmap; 
import java.util.map; 
 
import sun.misc.base64decoder; 
import sun.misc.base64encoder; 
 
/** 
 * dsa-digital signature algorithm 是schnorr和elgamal签名算法的变种,被美国nist作为dss(digitalsignature standard)。 
 * 简单的说,这是一种更高级的验证方式,用作数字签名。不单单只有公钥、私钥,还有数字签名。私钥加密生成数字签名,公钥验证数据及签名。 
 * 如果数据和签名不匹配则认为验证失败!即 传输中的数据 可以不再加密,接收方获得数据后,拿到公钥与签名 验证数据是否有效 
 */ 
public class dsa { 
 /**
 *不仅可以使用dsa算法,同样也可以使用rsa算法做数字签名
 */ 
 public static final string key_algorithm = "rsa"; 
 public static final string signature_algorithm = "md5withrsa";*/ 
 
 public static final string key_algorithm = "dsa"; 
 public static final string signature_algorithm = "dsa"; 
 
 public static final string default_seed = "$%^*%^()(hjg8awfjas7"; //默认种子 
 public static final string public_key = "dsapublickey"; 
 public static final string private_key = "dsaprivatekey"; 
 
 public static void main(string[] args) throws exception { 
 string str = "!@#$!#^$#&zxvdf呆军工路爱着你*()_+"; 
 byte[] data = str.getbytes(); 
  
 map<string, object> keymap = initkey();// 构建密钥 
 publickey publickey = (publickey) keymap.get(public_key); 
 privatekey privatekey = (privatekey) keymap.get(private_key); 
 system.out.println("私钥format:" + privatekey.getformat()); 
 system.out.println("公钥format:" + publickey.getformat()); 
  
  
 // 产生签名 
 string sign = sign(data, getprivatekey(keymap)); 
  
 // 验证签名 
 boolean verify1 = verify("aaa".getbytes(), getpublickey(keymap), sign); 
 system.err.println("经验证 数据和签名匹配:" + verify1); 
  
 boolean verify = verify(data, getpublickey(keymap), sign); 
 system.err.println("经验证 数据和签名匹配:" + verify); 
 } 
 
 /** 
 * 生成密钥 
 * 
 * @param seed 种子 
 * @return 密钥对象 
 * @throws exception 
 */ 
 public static map<string, object> initkey(string seed) throws exception { 
 system.out.println("生成密钥"); 
  
 keypairgenerator keygen = keypairgenerator.getinstance(key_algorithm); 
 securerandom securerandom = new securerandom(); 
 securerandom.setseed(seed.getbytes()); 
 //modulus size must range from 512 to 1024 and be a multiple of 64 
 keygen.initialize(640, securerandom); 
  
 keypair keys = keygen.genkeypair(); 
 privatekey privatekey = keys.getprivate(); 
 publickey publickey = keys.getpublic(); 
  
 map<string, object> map = new hashmap<string, object>(2); 
 map.put(public_key, publickey); 
 map.put(private_key, privatekey); 
 return map; 
 } 
 
 /** 
 * 生成默认密钥 
 * 
 * @return 密钥对象 
 * @throws exception 
 */ 
 public static map<string, object> initkey() throws exception { 
 return initkey(default_seed); 
 } 
 
 /** 
 * 取得私钥 
 * 
 * @param keymap 
 * @return 
 * @throws exception 
 */ 
 public static string getprivatekey(map<string, object> keymap) throws exception { 
 key key = (key) keymap.get(private_key); 
 return encryptbase64(key.getencoded()); //base64加密私钥 
 } 
 
 /** 
 * 取得公钥 
 * 
 * @param keymap 
 * @return 
 * @throws exception 
 */ 
 public static string getpublickey(map<string, object> keymap) throws exception { 
 key key = (key) keymap.get(public_key); 
 return encryptbase64(key.getencoded()); //base64加密公钥 
 } 
 
 /** 
 * 用私钥对信息进行数字签名 
 * @param data 加密数据 
 * @param privatekey 私钥-base64加密的 
 * @return 
 * @throws exception 
 */ 
 public static string sign(byte[] data, string privatekey) throws exception { 
 system.out.println("用私钥对信息进行数字签名"); 
  
 byte[] keybytes = decryptbase64(privatekey); 
 pkcs8encodedkeyspec keyspec = new pkcs8encodedkeyspec(keybytes); 
 keyfactory factory = keyfactory.getinstance(key_algorithm); 
 privatekey prikey = factory.generateprivate(keyspec);//生成 私钥 
  
 //用私钥对信息进行数字签名 
 signature signature = signature.getinstance(signature_algorithm); 
 signature.initsign(prikey); 
 signature.update(data); 
 return encryptbase64(signature.sign()); 
 } 
 
 /** 
 * base64encoder 加密 
 * @param data 要加密的数据 
 * @return 加密后的字符串 
 */ 
 private static string encryptbase64(byte[] data) { 
 base64encoder encoder = new base64encoder(); 
 string encode = encoder.encode(data); 
 return encode; 
 } 
 
 /** 
 * base64decoder 解密 
 * @param data 要解密的字符串 
 * @return 解密后的byte[] 
 * @throws exception 
 */ 
 private static byte[] decryptbase64(string data) throws exception { 
 base64decoder decoder = new base64decoder(); 
 byte[] buffer = decoder.decodebuffer(data); 
 return buffer; 
 } 
 
 /** 
 * 校验数字签名 
 * @param data 加密数据 
 * @param publickey 
 * @param sign 数字签名 
 * @return 
 * @throws exception 
 */ 
 public static boolean verify(byte[] data, string publickey, string sign) throws exception { 
 byte[] keybytes = decryptbase64(publickey); 
 x509encodedkeyspec keyspec = new x509encodedkeyspec(keybytes); 
 keyfactory keyfactory = keyfactory.getinstance(key_algorithm); 
 publickey pubkey = keyfactory.generatepublic(keyspec); 
  
 signature signature = signature.getinstance(signature_algorithm); 
 signature.initverify(pubkey); 
 signature.update(data); 
  
 return signature.verify(decryptbase64(sign)); //验证签名 
 } 
 
} 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网