赞
踩
非对称加密算法与对称加密算法的主要差别在于非对称加密算法用于加密和解密的密钥不相同,一个公开,称为公钥;一个保密,称为私钥。因此,非对称密码算法也称为双钥或公钥加密算法。非对称加密算法解决了对称加密算法密钥分配问题,并极大地提高了算法安全性。多种B2C或B2B应用均使用非对称加密算法作为数据加密的核心算法。
非对称加密算法源于DH算法(Diffie-Hellman,密钥交换算法),DH算法提出后,国际上相继出现了各种实用性更强的非对称加密算法,其构成主要是基于数学问题的求解,主要分为两类:
1、基于因子分解难题。RSA算法是最为典型的非对称加密算法
2、基于离散对数难题。ElGamal算法。
RSA算法实现易于理解,对于RSA算法的测试只需要注意经公钥加密的数据是否可以通过私钥将其解密,反之,经私钥加密的数据是否可以通过公钥将其解密。
就是说公钥和私钥都可用于加密和解密,但是应用场景有些区别。
私钥签名,公钥验签,主要用于签名验签的业务场景。
公钥加密,私钥解密,真正进行加密的业务场景。
与DH算法不同,RSA算法仅需要一套密钥即可完成加密/解密操作,并且公钥的密钥长度明显小于私钥的密钥长度,更便于发送和携带。
抽象类
- import org.apache.shiro.codec.Base64;
- import org.junit.Test;
-
- import javax.crypto.Cipher;
- import java.security.*;
- import java.security.interfaces.RSAPrivateKey;
- import java.security.interfaces.RSAPublicKey;
- import java.security.spec.PKCS8EncodedKeySpec;
- import java.security.spec.X509EncodedKeySpec;
- import java.util.HashMap;
- import java.util.Map;
-
-
- /***
- * RSA抽象类
- */
- public abstract class RSACoder {
- /**
- * 算法
- */
- public static final String KEY_ALGORITHM = "RSA";
- /**
- * 公钥
- */
- private static final String PUBLIC_KEY = "RSAPublicKey";
- /**
- * 私钥
- */
- private static final String PRIVATE_KEY = "RSAPrivateKey";
- /**
- * RSA密钥长度
- * 默认1024位
- * 密钥长度必须位64的倍数
- * 范围在512-65536之间
- */
- private static final int KEY_SIZE = 512;
-
- /**
- * 初始化密钥
- * @return 密钥Map
- */
- public static Map<String, Object> initKey()
- {
- try
- {
- // 实例化密钥对生成器
- KeyPairGenerator gen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
- // 初始化密钥对生成器
- gen.initialize(KEY_SIZE);
- // 生成密钥对
- KeyPair keyPair = gen.generateKeyPair();
- // 公钥
- RSAPublicKey rsaPublicKey = (RSAPublicKey)keyPair.getPublic();
- // 私钥
- RSAPrivateKey rsaPrivateKey = (RSAPrivateKey)keyPair.getPrivate();
- // 封装密钥
- Map<String, Object> map = new HashMap<String, Object>(2);
- map.put(PUBLIC_KEY, rsaPublicKey);
- map.put(PRIVATE_KEY, rsaPrivateKey);
- return map;
- }
- catch(Exception e)
- {
-
- }
- return null;
- }
-
- /**
- * 取得公钥
- * @param map
- * @return
- */
- public static byte[] getPublicKey(Map<String, Object> map)
- {
- Key key = (Key)map.get(PUBLIC_KEY);
- return key.getEncoded();
- }
- /**
- * 取得公钥
- * @param map
- * @return
- */
- public static byte[] getPrivateKey(Map<String, Object> map)
- {
- Key key = (Key)map.get(PRIVATE_KEY);
- return key.getEncoded();
- }
- /**
- * 私钥解密
- * @param data 待解密数据
- * @param key 私钥
- * @return byte[] 解密数据
- */
- public static byte[] decryptByPrivateKey(byte[] data, byte[] key)
- {
- try
- {
- //取得私钥
- PKCS8EncodedKeySpec pkcs = new PKCS8EncodedKeySpec(key);
- KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
- //生成私钥
- PrivateKey privateKey = keyFactory.generatePrivate(pkcs);
- //对数据解密
- Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
- cipher.init(Cipher.DECRYPT_MODE, privateKey);
- return cipher.doFinal(data);
- }
- catch(Exception e)
- {
-
- }
- return null;
- }
-
- /**
- * 公钥解密
- * @param data
- * @param key
- * @return
- */
- public static byte[] decryptByPublicKey(byte[] data, byte[] key)
- {
- try
- {
- //取得公钥
- X509EncodedKeySpec pkcs = new X509EncodedKeySpec(key);
- KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
- //生成公钥
- PublicKey publicKey = keyFactory.generatePublic(pkcs);
- //对数据解密
- Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
- cipher.init(Cipher.DECRYPT_MODE, publicKey);
- return cipher.doFinal(data);
- }
- catch(Exception e)
- {
-
- }
- return null;
- }
-
- /**
- * 公钥加密
- * @param data
- * @param key
- * @return
- */
- public static byte[] encryptByPublicKey(byte[] data, byte[] key)
- {
- try
- {
- //取得公钥
- X509EncodedKeySpec pkcs = new X509EncodedKeySpec(key);
- KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
- //生成公钥
- PublicKey publicKey = keyFactory.generatePublic(pkcs);
- //对数据加密
- Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
- cipher.init(Cipher.ENCRYPT_MODE, publicKey);
- return cipher.doFinal(data);
- }
- catch(Exception e)
- {
-
- }
- return null;
- }
-
- /**
- * 私钥加密
- * @param data
- * @param key
- * @return
- */
- public static byte[] encryptByPrivateKey(byte[] data, byte[] key)
- {
- try
- {
- //取得私钥
- PKCS8EncodedKeySpec pkcs = new PKCS8EncodedKeySpec(key);
- KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
- //生成公钥
- PrivateKey privateKey = keyFactory.generatePrivate(pkcs);
- //对数据加密
- Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
- cipher.init(Cipher.ENCRYPT_MODE, privateKey);
- return cipher.doFinal(data);
- }
- catch(Exception e)
- {
-
- }
- return null;
- }
- }
测试代码
- @Test
- public void digest_RSA()
- {
- byte[] publicKey;
- byte[] privateKey;
-
- try
- {
- Map<String, Object> map = RSACoder.initKey();
- publicKey = RSACoder.getPublicKey(map);
- privateKey = RSACoder.getPrivateKey(map);
- System.out.println("公钥;"+ org.apache.shiro.codec.Base64.encodeToString(publicKey));
- System.out.println("私钥;"+ org.apache.shiro.codec.Base64.encodeToString(privateKey));
-
- String input = "这是待加密的数据123456";
- byte[] data = input.getBytes();
-
- //私钥加密,公钥解密
- byte[] edata1 = RSACoder.encryptByPrivateKey(data, privateKey);
- System.out.println("私钥加密;"+ org.apache.shiro.codec.Base64.encodeToString(edata1));
- byte[] edata2 = RSACoder.decryptByPublicKey(edata1, publicKey);
- System.out.println("公钥解密;"+ new String(edata2));
-
- //公钥加密,私钥解密
- byte[] edata3 = RSACoder.encryptByPublicKey(data, publicKey);
- System.out.println("公钥加密;"+ org.apache.shiro.codec.Base64.encodeToString(edata3));
- byte[] edata4 = RSACoder.decryptByPrivateKey(edata3, privateKey);
- System.out.println("私钥解密;"+ new String(edata4));
-
- }
- catch(Exception e)
- {
-
- }
- }
输出如下
公钥;MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMgmYnp01ghihTnr0F3tHo4Q1HY0TDWr6CStK8HdnKYbnwA09TwQrmT7m0GPgU4Sieh7T0dqTT6oQvkCsVWD9y0CAwEAAQ==
私钥;MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAyCZienTWCGKFOevQXe0ejhDUdjRMNavoJK0rwd2cphufADT1PBCuZPubQY+BThKJ6HtPR2pNPqhC+QKxVYP3LQIDAQABAkBXE1wEqo/zVelgKZedRWvloqzMQDRnpUJ5sl03GpLsMJMcyGPFY9RvlYjMBdYOBqCb8PJtNtf1kHNuaMpSA3wBAiEA5uNuwy3m6mo+5mcJ4EKjkb5VOWinNFiQCMwaidUt+KkCIQDd6xvPq0H0pi0dtea5Sc+p/b6FpzoAE/1tCnZxGe1I5QIgQovgm766JGw07uFZ1lo+nL4YADDqqEqyUObK61kXI+kCIQDRveVyOIXE6c6YVxTzKNz56JZKkgwSiNIsXggly4BmNQIgEihE8cbNJCwcVxOBgYvIDMiaP6j8Pewmre3fXJZ4n7Y=私钥加密;MCf+L8qCY05j1GLUkkGt65vj2JJg1wxSb2xgUQzwk+RvUQgZaFV4W4HNBKO3jIJ7t6cBz/1FUWlAkwaFBIl/SQ==
公钥解密;这是待加密的数据123456公钥加密;n+vp/smgaUNzNG8kpc9BerCwa6pmIJUVYkTjRdGdjk7tXn3YEk3Rpa10gm4v5TDeNQ6y88n/cdiQ7D2l0Ox3aA==
私钥解密;这是待加密的数据123456
上面仅有关于RSA的用法。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。