public class RSAUtil { /** * 填充方式 */ public static enum PADDING { NoPadding, PKCS1Padding }; /** * 算法 */ public static final String KEY_ALGORITHM = "RSA"; /** * 算法/工作模式 */ public final static String CHIPER_ALGORITHM = "RSA/ECB/"; /** * 密钥长度 */ public static final int KEY_SIZE = 1024; /** * 65537 or 0x010001 */ public static final byte[] PUBLIC_EXPONENT = { 1, 0, 1 }; /** * 生成密钥对 * * @return KeyPair */ public static KeyPair generateKeyPair() { try { KeyPairGenerator keyPairGen = KeyPairGenerator .getInstance(KEY_ALGORITHM); keyPairGen.initialize(KEY_SIZE, new SecureRandom()); KeyPair keyPair = keyPairGen.genKeyPair(); return keyPair; } catch (Exception e) { throw new RuntimeException("Error when init key pair, errmsg: " + e.getMessage(), e); } } /** * 生成公钥 * * @param modulus * @param publicEXPonent * @return RSAPublicKey */ private static RSAPublicKey generateRSAPublicKey(byte[] modulus, byte[] publicExponent) { try { RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger( 1, modulus), new BigInteger(1, publicExponent)); KeyFactory keyFac = KeyFactory.getInstance(KEY_ALGORITHM); return (RSAPublicKey) keyFac.generatePublic(pubKeySpec); } catch (Exception e) { throw new RuntimeException( "Error when generate rsaPubblicKey, errmsg: " + e.getMessage(), e); } } /** * 生成私钥 * * @param modulus * @param privateExponent * @return RSAPrivateKey */ private static RSAPrivateKey generateRSAPrivateKey(byte[] modulus, byte[] privateExponent) { try { KeyFactory keyFac = KeyFactory.getInstance(KEY_ALGORITHM); RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec( new BigInteger(1, modulus), new BigInteger(1, privateExponent)); return (RSAPrivateKey) keyFac.generatePrivate(priKeySpec); } catch (Exception e) { throw new RuntimeException( "Error when generate rsaPrivateKey, errmsg: " + e.getMessage(), e); } } /** * 加密 * * @param key * 加密的密钥 * @param data * 待加密的明文数据 * @return 加密后的数据 */ private static byte[] encrypt(Key key, byte[] data,PADDING padding) { try { Cipher cipher = Cipher.getInstance(CHIPER_ALGORITHM+(padding==null?PADDING.NoPadding:padding)); cipher.init(Cipher.ENCRYPT_MODE, key); return cipher.doFinal(data); } catch (Exception e) { throw new RuntimeException("Error when encrypt data, errmsg: " + e.getMessage(), e); } } /** * 公钥加密 * * @param publicKey * @param data * @return */ public static byte[] encryptByPublicKey(byte[] publicKey, byte[] data,PADDING padding) { // 得到公钥 RSAPublicKey key = generateRSAPublicKey(publicKey, PUBLIC_EXPONENT); // 加密 return encrypt(key, data,padding); } /** * 私钥加密 * * @param publicKey * @param privateKey * @param data * @return */ public static byte[] encryptByPrivateKey(byte[] publicKey, byte[] privateKey, byte[] data,PADDING padding) { // 得到私钥 RSAPrivateKey key = generateRSAPrivateKey(publicKey, privateKey); // 加密 return encrypt(key, data,padding); } /** * 解密 * * @param key * 解密的密钥 * @param data * 已经加密的数据 * @return 解密后的明文 */ private static byte[] decrypt(Key key, byte[] data,PADDING padding) { try { Cipher cipher = Cipher.getInstance(CHIPER_ALGORITHM+(padding==null?PADDING.NoPadding:padding)); cipher.init(Cipher.DECRYPT_MODE, key); return cipher.doFinal(data); } catch (Exception e) { throw new RuntimeException("Error when decrypt data, errmsg: " + e.getMessage(), e); } } /** * 公钥解密 * * @param publicKey * @param data * @ret公钥urn */ public static byte[] decryptByPublicKey(byte[] publicKey, byte[] data,PADDING padding) { // 得到公钥 RSAPublicKey key = generateRSAPublicKey(publicKey, PUBLIC_EXPONENT); // 解密 return decrypt(key, data,padding); } /** * 私钥解密 * * @param publicKey * @param privateKey * @param data * @return */ public static byte[] decryptByPrivateKey(byte[] publicKey, byte[] privateKey, byte[] data,PADDING padding) { // 得到私钥 RSAPrivateKey key = generateRSAPrivateKey(publicKey, privateKey); // 解密 return decrypt(key, data,padding); }
相关推荐
RSA工具类,囊括了 生成密钥对、私钥解密、公钥加密、私钥签名、公钥验证的方法,并且其中有例子,可直接运行。
集合百度大多案例,整理汇总
RSA 工具类。提供加密,解密,生成密钥对等方法。 * 需要到http://www.bouncycastle.org下载bcprov-jdk14-123.jar。 * RSA加密原理概述 * RSA的安全性依赖于大数的分解,公钥和私钥都是两个大素数(大于100的...
设计到的方法如下: DigitalSign init getInstance SignMsgByRelativePath SignMsgByInputStream signMsgByAbsolutePath verifyMsgByInputSteam verifyMsgByAbsolutePath verifyMsgByRelativePath ...
RSA为最常用的一种非对称方式的算法,这次封装的Utils类完成了公钥加密、私钥解密、私钥加签、公钥验签四种常用方法。
C#RSA加密解密工具,可以生成公钥和私钥,可以生成PKCS1公钥,PKCS8私钥。适用于js加密,C#解密,java解密。
JAVA中RSA加密解密工具类
java、android通用的RSA和AES工具类
是RSA工具类,包括分段加密、分段解密、公私钥对生成,是一份完整的工具类。
RSA非对称加密前端工具类 用于网络传输中数据加密
RSA加密工具类
微信小程序能用RSA分段加解密工具类。直接复制到你的工程下就能用
RSA工具(来源于网络),此工具用着比较顺手,详细情况及使用请看:http://blog.csdn.net/yxstars/article/details/38443937
非常实用的rsa加解密工具类,加签验签,拿过去就能用,包含测试代码
RSA签名工具类
微信支付工具类v3版 包含: 1. 微信支付v3版 2. 微信退款v3版 3. 微信交易状态查询 4. 企业打款到个人零钱(旧版) 本人在企业项目中使用,自己封装的的微信交易相关的一套工具类。 直接调用方法传入对应的参数即可...
C# RSA加密、支持JAVA格式公钥私钥,亲测很有用,真实有效,拒绝骗子,拒绝坑货,20分甩卖。
RSA加密解密验签工具类
适用于Android、Java、Web端使用的AES、RSA加密工具类