加密RSA加密

  • j7_769682
    了解作者
  • 3.5MB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-14 01:17
    上传日期
java 语音 RSA加密DEMO源代码,本人亲测 可以正常运行!
RSA加密Demo源码.rar
内容介绍
package com.test.utils; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.ObjectInputStream; import java.security.InvalidKeyException; import java.security.Key; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.HashMap; import java.util.Map; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import org.bouncycastle.jce.provider.BouncyCastleProvider; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; @SuppressWarnings("unused") public class RSASecurityUtil { public static final String KEY_ALGORITHM = "RSA"; public static final String SIGNATURE_ALGORITHM = "MD5withRSA"; private static final String PUBLIC_KEY = "RSAPublicKey"; private static final String PRIVATE_KEY = "RSAPrivateKey"; private static final int MAX_ENCRYPT_BLOCK = 117; private static final int MAX_DECRYPT_BLOCK = 128; public static void main(String[] args) { Map<String, Object> keyMap; try { keyMap = initKey(); String publicKey = getPublicKey(keyMap); System.out.println("公钥:"+publicKey); String privateKey = getPrivateKey(keyMap); System.out.println("秘钥:"+privateKey); String plainText = "你好,java"; System.out.println("原文:"+plainText); String cipher = encryptBASE64(encrypt(loadPublicKeyByStr(publicKey), plainText.getBytes())); System.out.println("公钥加密:"+cipher); String restr = new String(decrypt(loadPrivateKeyByStr(privateKey), decryptBASE64(cipher))); System.out.println("私钥解密:"+restr); plainText = "password=E10ADC3949BA59ABBE56E057F20F883E&order_id=S123456788&mobile=15980691111&product_code=100100101000010&effect_type=1&timestamp=1484894535218"; cipher = encryptBASE64(encrypt(loadPrivateKeyByStr(privateKey), plainText.getBytes())); System.out.println("私钥加密:"+cipher); restr = new String(decrypt(loadPublicKeyByStr(publicKey), decryptBASE64(cipher))); System.out.println("公钥解密:"+restr); } catch (Exception e) { e.printStackTrace(); } } /** * 生成公钥 * @param keyMap * @return * @throws Exception */ public static String getPublicKey(Map<String, Object> keyMap) throws Exception { Key key = (Key) keyMap.get(PUBLIC_KEY); byte[] publicKey = key.getEncoded(); return encryptBASE64(key.getEncoded()); } /** * 生成私钥 * @param keyMap * @return * @throws Exception */ public static String getPrivateKey(Map<String, Object> keyMap) throws Exception { Key key = (Key) keyMap.get(PRIVATE_KEY); byte[] privateKey = key.getEncoded(); return encryptBASE64(key.getEncoded()); } public static byte[] decryptBASE64(String key) throws Exception { return (new BASE64Decoder()).decodeBuffer(key); } public static String encryptBASE64(byte[] key) throws Exception { return (new BASE64Encoder()).encodeBuffer(key); } /** * 生成公私钥秘钥对 * @return * @throws Exception */ public static Map<String, Object> initKey() throws Exception { SecureRandom secureRandom = new SecureRandom(); KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM); keyPairGen.initialize(1024, secureRandom); KeyPair keyPair = keyPairGen.generateKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); Map<String, Object> keyMap = new HashMap<String, Object>(2); keyMap.put(PUBLIC_KEY, publicKey); keyMap.put(PRIVATE_KEY, privateKey); return keyMap; } /** * 从字符串中加载公钥 * * @param publicKeyStr * 公钥数据字符串 * @throws Exception * 加载公钥时产生的异常 */ public static RSAPublicKey loadPublicKeyByStr(String publicKeyStr) throws Exception { try { byte[] buffer = decryptBASE64(publicKeyStr); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer); return (RSAPublicKey) keyFactory.generatePublic(keySpec); } catch (NoSuchAlgorithmException e) { throw new Exception("无此算法"); } catch (InvalidKeySpecException e) { throw new Exception("公钥非法"); } catch (NullPointerException e) { throw new Exception("公钥数据为空"); } } /** * 从字符中加载私钥 * @param privateKeyStr * @return * @throws Exception */ public static RSAPrivateKey loadPrivateKeyByStr(String privateKeyStr) throws Exception { try { byte[] buffer = decryptBASE64(privateKeyStr); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return (RSAPrivateKey) keyFactory.generatePrivate(keySpec); } catch (NoSuchAlgorithmException e) { throw new Exception("无此算法"); } catch (InvalidKeySpecException e) { throw new Exception("私钥非法"); } catch (NullPointerException e) { throw new Exception("私钥数据为空"); } } /** * 公钥加密过程 * * @param publicKey * 公钥 * @param plainTextData * 明文数据 * @return * @throws Exception * 加密过程中的异常信息 */ public static byte[] encrypt(RSAPublicKey publicKey, byte[] plainTextData) throws Exception { if (publicKey == null) { throw new Exception("加密公钥为空, 请设置"); } Cipher cipher = null; try { // 使用默认RSA cipher = Cipher.getInstance("RSA", new BouncyCastleProvider()); cipher.init(Cipher.ENCRYPT_MODE, publicKey); int blockSize = cipher.getBlockSize();// 获得加密块大小,如:加密前数据为128个byte,而key_size=1024 // 加密块大小为127 // byte,加密后为128个byte;因此共有2个加密块,第一个127 // byte第二个为1个byte int outputSize = cipher.getOutputSize(plainTextData.length);// 获得加密块加密后块大小 int leavedSize = plainTextData.length % blockSize; int blocksSize = leavedSize != 0 ? plainTextData.length / blockSize + 1 : plainTextData.length / blockSize; byte[] raw = new byte[outputSize * blocksSize]; int i = 0; while (plainTextData.length - i * blockSize > 0) { if (plainTextData.length - i * blockSize > blockSize) cipher.doFinal(plainTextData, i * blockSize, blockSize, raw, i * outputSize); else cipher.doFinal(plainTextData, i * blockSize, plainTextData.length - i * blockSize, raw, i * outputSize); // 这里面doUpdate方法不可用,查看源代码后发现每次doUpd
评论
    相关推荐
    • Java
      Java
    • java
      Java
    • Java
      Java
    • Java
      Java项目
    • Java
      Java 对于Java练习
    • java
      Java 此回购包括我学习Java的旅程。
    • java
      Java Java基础
    • Java
      Java
    • Java
      Java 我创建的Java项目
    • Java
      Java 2021/02/19 .java Main.java 学生.java Bmi.java 。班级 Bmi类 主类 MyBmi.class 学生班 学生样本类 2021/02/20 .java DeleteFile.java WriteFile.java MyDataPrint.java 。班级 DeleteFile.class ...