一、RSA加密简介 RSA加密是一种非对称加密。可以在不直接传递密钥的情况下,完成解密;是由一对密钥来进行加解密的过程,分别称为公钥和私钥。两者之间有数学相关,该加密算法的原理就是对一极大整数做因数分解的困难性来保证安全性。通常个人保存私钥,公钥是公开的(可能同时多人持有)。 二、对签名和验签过程详细理解: 签名过程: - A计算消息m的消息摘要,记为 h(m)
- A使用私钥(n,d)对h(m)加密,生成签名s, s满足:s=(h(m))^d mod n;
由于A是用自己的私钥对消息摘要加密,所以只用使用s的公钥才能解密该消息摘要,这样A就不可否认自己发送了该消息给B. - A发送消息和签名(m,s)给B
验签过程: - B计算消息m的消息摘要(计算方式和A相同),记为h(m)
- B使用A的公钥(n,e)解密s,得到 H(m), H(m) = s^e mod n
- B比较H(m)与h(m),相同才能证明验签成功
三、对加密/解密和签名/验签完整过程详细理解:
A->B:
1.A提取消息m的消息摘要h(m),并使用自己的私钥对摘要h(m)进行加密,生成签名s
2.A将签名s和消息m一起,使用B的公钥进行加密,生成密文c,发送给B B:
1.B接收到密文c,使用自己的私钥解密c得到明文m和数字签名s
2.B使用A的公钥解密数字签名s解密得到H(m)
3.B使用相同的方法提取消息m的消息摘要h(m)
4.B比较两个消息摘要。相同则验证成功;不同则验证失败 四、代码 - import java.io.ByteArrayOutputStream;
- 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.spec.PKCS8EncodedKeySpec;
- import java.security.spec.X509EncodedKeySpec;
- import javax.crypto.Cipher;
- import org.apache.commons.codec.binary.Base64;
- public class TestRSA {
- /**
- * RSA最大加密明文大小
- */
- private static final int MAX_ENCRYPT_BLOCK = 64;
- /**
- * RSA最大解密密文大小
- */
- private static final int MAX_DECRYPT_BLOCK = 75;
- /**
- * 获取密钥对
- *
- * @return 密钥对
- */
- public static KeyPair getKeyPair() throws Exception {
- KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
- generator.initialize(512);
- return generator.generateKeyPair();
- }
- /**
- * 获取私钥
- *
- * @param privateKey 私钥字符串
- * @return
- */
- public static PrivateKey getPrivateKey(String privateKey) throws Exception {
- KeyFactory keyFactory = KeyFactory.getInstance("RSA");
- byte[] decodedKey = Base64.decodeBase64(privateKey.getBytes());
- PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decodedKey);
- return keyFactory.generatePrivate(keySpec);
- }
- /**
- * 获取公钥
- *
- * @param publicKey 公钥字符串
- * @return
- */
- public static PublicKey getPublicKey(String publicKey) throws Exception {
- KeyFactory keyFactory = KeyFactory.getInstance("RSA");
- byte[] decodedKey = Base64.decodeBase64(publicKey.getBytes());
- X509EncodedKeySpec keySpec = new X509EncodedKeySpec(decodedKey);
- return keyFactory.generatePublic(keySpec);
- }
- /**
- * RSA加密
- *
- * @param data 待加密数据
- * @param publicKey 公钥
- * @return
- */
- public static String encrypt(String data, PublicKey publicKey) throws Exception {
- Cipher cipher = Cipher.getInstance("RSA");
- cipher.init(Cipher.ENCRYPT_MODE, publicKey);
- int inputLen = data.getBytes().length;
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- int offset = 0;
- byte[] cache;
- int i = 0;
- // 对数据分段加密
- while (inputLen - offset > 0) {
- if (inputLen - offset > MAX_ENCRYPT_BLOCK) {
- cache = cipher.doFinal(data.getBytes(), offset, MAX_ENCRYPT_BLOCK);
- } else {
- cache = cipher.doFinal(data.getBytes(), offset, inputLen - offset);
- }
- out.write(cache, 0, cache.length);
- i++;
- offset = i * MAX_ENCRYPT_BLOCK;
- }
- byte[] encryptedData = out.toByteArray();
- out.close();
- // 获取加密内容使用base64进行编码,并以UTF-8为标准转化成字符串
- // 加密后的字符串
- return new String(Base64.encodeBase64String(encryptedData));
- }
- /**
- * RSA解密
- *
- * @param data 待解密数据
- * @param privateKey 私钥
- * @return
- */
- public static String decrypt(String data, PrivateKey privateKey) throws Exception {
- Cipher cipher = Cipher.getInstance("RSA");
- cipher.init(Cipher.DECRYPT_MODE, privateKey);
- byte[] dataBytes = Base64.decodeBase64(data);
- int inputLen = dataBytes.length;
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- int offset = 0;
- byte[] cache;
- int i = 0;
- // 对数据分段解密
- while (inputLen - offset > 0) {
- if (inputLen - offset > MAX_DECRYPT_BLOCK) {
- cache = cipher.doFinal(dataBytes, offset, MAX_DECRYPT_BLOCK);
- } else {
- cache = cipher.doFinal(dataBytes, offset, inputLen - offset);
- }
- out.write(cache, 0, cache.length);
- i++;
- offset = i * MAX_DECRYPT_BLOCK;
- }
- byte[] decryptedData = out.toByteArray();
- out.close();
- // 解密后的内容
- return new String(decryptedData, "UTF-8");
- }
- /**
- * 签名
- *
- * @param data 待签名数据
- * @param privateKey 私钥
- * @return 签名
- */
- public static String sign(String data, PrivateKey privateKey) throws Exception {
- byte[] keyBytes = privateKey.getEncoded();
- PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
- KeyFactory keyFactory = KeyFactory.getInstance("RSA");
- PrivateKey key = keyFactory.generatePrivate(keySpec);
- Signature signature = Signature.getInstance("MD5withRSA");
- signature.initSign(key);
- signature.update(data.getBytes());
- return new String(Base64.encodeBase64(signature.sign()));
- }
- /**
- * 验签
- *
- * @param srcData 原始字符串
- * @param publicKey 公钥
- * @param sign 签名
- * @return 是否验签通过
- */
- public static boolean verify(String srcData, PublicKey publicKey, String sign) throws Exception {
- byte[] keyBytes = publicKey.getEncoded();
- X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
- KeyFactory keyFactory = KeyFactory.getInstance("RSA");
- PublicKey key = keyFactory.generatePublic(keySpec);
- Signature signature = Signature.getInstance("MD5withRSA");
- signature.initVerify(key);
- signature.update(srcData.getBytes());
- return signature.verify(Base64.decodeBase64(sign.getBytes()));
- }
- }
- public static void main(String[] args) {
- try {
- // 生成密钥对
- KeyPair keyPair = getKeyPair();
- String privateKey = new String(Base64.encodeBase64(keyPair.getPrivate().getEncoded()));
- String publicKey = new String(Base64.encodeBase64(keyPair.getPublic().getEncoded()));
- System.out.println("私钥:" + privateKey);
- System.out.println("公钥:" + publicKey);
- // RSA加密 待加密的文字内容
- String data = "123456";
- String encryptData = encrypt(data, getPublicKey(publicKey));
- System.out.println("加密前内容:" + data);
- System.out.println("加密后内容:" + encryptData);
- // RSA解密
- String decryptData = decrypt(encryptData, getPrivateKey(privateKey));
- System.out.println("解密后内容:" + decryptData);
- // RSA签名
- String sign = sign(data, getPrivateKey(privateKey));
- // RSA验签
- boolean result = verify(data, getPublicKey(publicKey), sign);
- System.out.print("验签结果:" + result);
- } catch (Exception e) {
- e.printStackTrace();
- System.out.print("加解密异常");
- }
- }
复制代码五、运行结果
|