java加密算法之RSA篇

Posted lwx-apollo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java加密算法之RSA篇相关的知识,希望对你有一定的参考价值。

加密

/**
     * 私钥加密
     *
     * @param data       源数据
     * @param privateKey 私钥
     * @return
     * @throws Exception
     */
    public static byte[] encryptByPrivateKey(byte[] data, String privateKey) 
        try 
            byte[] keyBytes = MyBase64Utils.decodeStringForByte(privateKey);
            PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
            KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM);
            Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
            Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
            cipher.init(Cipher.ENCRYPT_MODE, privateK);
            int inputLen = data.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, offSet, MAX_ENCRYPT_BLOCK);
                 else 
                    cache = cipher.doFinal(data, offSet, inputLen - offSet);
                
                out.write(cache, 0, cache.length);
                i++;
                offSet = i * MAX_ENCRYPT_BLOCK;
            
            byte[] encryptedData = out.toByteArray();
            out.close();
            return encryptedData;
        catch (Exception e)

        
        return null;
    

解密

/**
     * 公钥解密
     *
     * @param encryptedData 已加密数据
     * @param publicKey     公钥
     * @return
     * @throws Exception
     */
    public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey) 
        try 
            byte[] keyBytes = MyBase64Utils.decodeStringForByte(publicKey);
            X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
            KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM);
            Key publicK = keyFactory.generatePublic(x509KeySpec);
            Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
            cipher.init(Cipher.DECRYPT_MODE, publicK);
            int inputLen = encryptedData.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(encryptedData, offSet, MAX_DECRYPT_BLOCK);
                 else 
                    cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
                
                out.write(cache, 0, cache.length);
                i++;
                offSet = i * MAX_DECRYPT_BLOCK;
            
            byte[] decryptedData = out.toByteArray();
            out.close();
            return decryptedData;
         catch (Exception ex) 

        
        return null;
    

常量

    /**
     * 加密算法RSA
     */
    public static final String RSA_KEY_ALGORITHM = "RSA";

    /**
     * 签名算法
     */
    public static final String SIGNATURE_ALGORITHM = "SHA1WithRSA";
    /**
     * RSA最大加密明文大小,依据需求自定义
     */
    private static final int MAX_ENCRYPT_BLOCK = 117;
    /**
     * RSA最大解密密文大小,依据需求自定义
     */
    private static final int MAX_DECRYPT_BLOCK = 128;

以上是关于java加密算法之RSA篇的主要内容,如果未能解决你的问题,请参考以下文章

java加密算法之RSA篇

一篇搞定RSA加密与SHA签名|与Java完全同步

java之--简单RSA算法加密解密

RSA  加密算法(原理篇)

.NET Core加解密实战系列之——RSA非对称加密算法

《夜深人静写算法》数论篇 - (19) RSA