首页 > 其他分享 >rsa加密过长数据工具类

rsa加密过长数据工具类

时间:2024-05-31 16:56:26浏览次数:13  
标签:加密 String 过长 rsa Cipher str import return

rsa默认最多只能加密密钥长度/8-11长度的明文,最多只能解密密钥长度/8长度的密文,如:

密钥长度为1024,则明文长度最长117,密文长度最长128。

可以采用分段加解密的方式,增加明文密文长度(同时加解密的效率也会按比例降低)。

工具类如下:

 

package com.kuandeng.common.common.util;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.security.*;
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;


@Slf4j
public class RSACipher {
    
    public static String AlgorithmName = "RSA";
    public static String Algorithm = "RSA/ECB/PKCS1Padding";
    public static int KeyLength = 2048;
    private static int MAX_ENCRYPT_BLOCK = 117;
    private static int MAX_DECRYPT_BLOCK = 128;
    static {
        MAX_ENCRYPT_BLOCK = KeyLength/8 - 11;
        MAX_DECRYPT_BLOCK = KeyLength/8;
    }

    @Getter
    private String publicKey;

    @Getter
    private String privateKey;

    //公钥加密
    private Cipher publicEncryptCipher = null;
    //私钥解密
    private Cipher privateDecryptCipher = null;
    //私钥加密
    private Cipher privateEncryptCipher = null;
    //公钥解密
    private Cipher publicDecryptCipher = null;

    public RSACipher(String publicKey, String privateKey) {
        this.init(publicKey, privateKey);
    }

    public void init(String publicKey, String privateKey){
        this.publicKey = publicKey;
        this.privateKey = privateKey;
        //公钥加密
        byte[] decoded = Base64.decodeBase64(publicKey);
        RSAPublicKey pubKey = null;
        try {
            pubKey = (RSAPublicKey) KeyFactory.getInstance(AlgorithmName).generatePublic(new X509EncodedKeySpec(decoded));
//            publicCipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
            publicEncryptCipher = Cipher.getInstance(Algorithm);
            publicEncryptCipher.init(Cipher.ENCRYPT_MODE, pubKey);
        } catch (InvalidKeySpecException | InvalidKeyException |
                NoSuchPaddingException | NoSuchAlgorithmException e) {
            log.warn(ExceptionHelper.getStackTraceString(e));
        }

        //私钥解密
        //64位解码加密后的字符串
        //base64编码的私钥
        byte[] decoded2 = Base64.decodeBase64(privateKey);
        RSAPrivateKey priKey = null;
        try {
            priKey = (RSAPrivateKey) KeyFactory.getInstance(AlgorithmName).generatePrivate(new PKCS8EncodedKeySpec(decoded2));
//            privateCipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
            privateDecryptCipher = Cipher.getInstance(Algorithm);
            privateDecryptCipher.init(Cipher.DECRYPT_MODE, priKey);
        } catch (InvalidKeySpecException | NoSuchAlgorithmException | NoSuchPaddingException |
                InvalidKeyException e) {
            log.warn(ExceptionHelper.getStackTraceString(e));
        }

        //私钥加密
        byte[] decoded3 = Base64.decodeBase64(privateKey);
        RSAPrivateKey pubKey2 = null;
        try {
            pubKey2 = (RSAPrivateKey) KeyFactory.getInstance(AlgorithmName).generatePrivate(new PKCS8EncodedKeySpec(decoded3));
//            publicCipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
            privateEncryptCipher = Cipher.getInstance(Algorithm);
            privateEncryptCipher.init(Cipher.ENCRYPT_MODE, pubKey2);
        } catch (InvalidKeySpecException | InvalidKeyException |
                NoSuchPaddingException | NoSuchAlgorithmException e) {
            log.warn(ExceptionHelper.getStackTraceString(e));
        }

        //公钥解密
        //64位解码加密后的字符串
        //base64编码的私钥
        byte[] decoded23 = Base64.decodeBase64(publicKey);
        RSAPublicKey priKey2 = null;
        try {
            priKey2 = (RSAPublicKey) KeyFactory.getInstance(AlgorithmName).generatePublic(new X509EncodedKeySpec(decoded23));
//            privateCipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
            publicDecryptCipher = Cipher.getInstance(Algorithm);
            publicDecryptCipher.init(Cipher.DECRYPT_MODE, priKey2);
        } catch (InvalidKeySpecException | NoSuchAlgorithmException | NoSuchPaddingException |
                InvalidKeyException e) {
            log.warn(ExceptionHelper.getStackTraceString(e));
        }
    }

    //随机生成密钥对
    public static String[] genKeyPair() {
        // KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
        KeyPairGenerator keyPairGen = null;
        try {
            keyPairGen = KeyPairGenerator.getInstance(AlgorithmName);
        } catch (NoSuchAlgorithmException e) {
            log.warn(ExceptionHelper.getStackTraceString(e));
        }
        // 初始化密钥对生成器,密钥大小为96-1024位
        assert keyPairGen != null;
        keyPairGen.initialize(KeyLength, new SecureRandom());
        // 生成一个密钥对,保存在keyPair中
        KeyPair keyPair = keyPairGen.generateKeyPair();
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();   // 得到私钥
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();  // 得到公钥
        String publicKeyString = new String(Base64.encodeBase64(publicKey.getEncoded()));
        String privateKeyString = new String(Base64.encodeBase64((privateKey.getEncoded())));
        log.info("随机生成的公钥为:" + publicKeyString);
        log.info("随机生成的私钥为:" + privateKeyString);
        return new String[]{publicKeyString, privateKeyString};
    }


    private byte[] crypt(byte[] data, Cipher cipher, int maxLength) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int inputLen = data.length;
        int offSet = 0;
        byte[] cache;
        // 对数据分段解密
        try {
            while (inputLen - offSet > 0) {
                if (inputLen - offSet > maxLength) {
                    cache = cipher.doFinal(data, offSet, maxLength);
                } else {
                    cache = cipher.doFinal(data, offSet, inputLen - offSet);
                }
                out.write(cache, 0, cache.length);
                offSet += maxLength;
            }
            return out.toByteArray();
        } catch (BadPaddingException  | IllegalBlockSizeException e) {
            log.warn(ExceptionHelper.getStackTraceString(e));
        }
        finally {
            try{
                out.close();
            }catch (Exception e){
                log.warn(ExceptionHelper.getStackTraceString(e));
            }
        }
        return null;
    }

    /**
     * RSA分段加密
     *
     * @param str       加密字符串
     * @return 密文
     */
    private String encrypt(String str, Cipher cipher) {
        //只能处理MAX_ENCRYPT_BLOCK长度的数据,故用下面的代码改写
//        try {
//            return Base64.encodeBase64String(cipher.doFinal(str.getBytes(StandardCharsets.UTF_8)));
//        } catch (IllegalBlockSizeException e) {
//            e.printStackTrace();
//        } catch (BadPaddingException e) {
//            e.printStackTrace();
//        }

        byte[] data = str.getBytes(StandardCharsets.UTF_8);
        return Base64.encodeBase64String(crypt(data, cipher, MAX_ENCRYPT_BLOCK));
    }


    /**
     * RSA分段解密
     *
     * @param str        加密字符串
     * @return 明文
     */
    private String decrypt(String str, Cipher cipher) {
        //只能处理MAX_DECRYPT_BLOCK长度的数据,故用下面的代码改写
//        byte[] data = Base64.decodeBase64(str.getBytes(StandardCharsets.UTF_8));
//        try {
//            return new String(privateDecryptCipher.doFinal(data));
//        } catch (BadPaddingException |
//                IllegalBlockSizeException e) {
//            e.printStackTrace();
//        }

        byte[] data = Base64.decodeBase64(str.getBytes(StandardCharsets.UTF_8));
        byte[] result = crypt(data, cipher, MAX_DECRYPT_BLOCK);
        if(result == null){
            return null;
        }
        return new String(result, StandardCharsets.UTF_8);
    }



    /**
     * RSA公钥加密
     *
     * @param str       加密字符串
     * @return 密文
     */
    public String publicEncrypt(String str) {
        return encrypt(str, publicEncryptCipher);
    }


    /**
     * RSA私钥解密
     *
     * @param str        加密字符串
     * @return 明文
     */
    public String privateDecrypt(String str) {
        return decrypt(str, privateDecryptCipher);
    }

    /**
     * RSA私钥加密
     *
     * @param str       加密字符串
     * @return 密文
     */
    public String privateEncrypt(String str) {
        return encrypt(str, privateEncryptCipher);
    }


    /**
     * RSA公钥解密
     *
     * @param str        加密字符串
     * @return 明文
     */
    public String publicDecrypt(String str) {
        return decrypt(str, publicDecryptCipher);
    }
}

  

 

标签:加密,String,过长,rsa,Cipher,str,import,return
From: https://www.cnblogs.com/zycjwdss/p/18224847

相关文章

  • RSAUtils 非对称加密hutool
    1、使用hutool的rsa加解密工具,自定义公钥私钥字符串2、importcn.hutool.core.codec.Base64Decoder;importcn.hutool.crypto.asymmetric.KeyType;importcn.hutool.crypto.asymmetric.RSA;importlombok.extern.slf4j.Slf4j;importorg.jeecg.common.util.CN;importjav......
  • 文件加密软件哪个好用?2024最值得收藏的文件加密软件
    在数字化时代,数据安全已经成为企业和个人不可忽视的重要问题。文件加密软件作为保障数据安全的重要工具,其选择和使用显得尤为关键。那么,在众多的文件加密软件中,哪个最好用呢?2024年,以下几款文件加密软件最推荐收藏。首先,我们推荐的是“安秉网盾加密软件”。这款软件以其高效、......
  • 企业防泄密软件有哪些?这三款企业加密软件不容错过
    在数字化浪潮下,企业数据的保密工作变得尤为重要。为了有效防止数据泄露,众多企业纷纷选择部署防泄密软件。本文将为您介绍三款且备受好评的企业加密软件,它们在市场上具有广泛的应用和良好的口碑。首先,让我们来了解一下安秉网盾加密软件。作为国内领先的加密软件提供商,安秉网盾......
  • 面试官:前端加密怎么做?这,这,这不是后端的活儿吗?
    目录 一、Base64编码二、哈希算法三、对称加密(AES/DES) 四、非对称加密(RSA) 五、加盐六、WebCryptographtAPI七、总结    随着信息和数据安全重要性的日益凸显,如何保证信息数据在传输的过程中的安全成为开发者重点关注的内容。前端加密通常是指在浏览器中......
  • 全国大江大河及大型水库水文数据字体加密数据的解密
    从2024年3月7日晚上开始,水利部水文局升级了全国大江大河及大型水库日报数据查看网站,对返回的数据使用动态字体文件名的方式对部分重要指标进行了字体加密,无法拷贝和直接使用数据,必须手工输入或者通过第三方图片转文字或图片转表格技术进行收集,但正确率得不到保证。通过分析数据规......
  • Towards Universal Sequence Representation Learning for Recommender Systems
    目录概符号说明UniSRec统一的文本表示统一的序列表示Parameter-EfficientFine-tuning代码HouY.,MuS.,ZhaoW.X.,LiY.,DingB.andWenJ.TowardsUniversalSequenceRepresentationLearningforRecommenderSystems.KDD,2022.概本文提出了一个用text替代ID......
  • 企业文件加密实现数据泄露防护
    在数字化时代,数据成为企业最宝贵的资产之一。然而,数据泄露事件频发,给企业带来了巨大的经济损失和声誉风险。为了保护企业的核心利益,实现数据泄露防护,企业必须采取有效的文件加密措施。一、数据泄露的严重性数据泄露不仅会导致商业机密的外泄,还可能引发以下严重后果:经济损失:重......
  • 企业级OV SSL证书的应用场景和加密手段
    为了保护数据传输的安全性与用户隐私,企业级OVSSL(OrganizationValidationSSL)证书成为众多企业的首选安全解决方案。本文将深入探讨OVSSL证书的应用场景及其实现数据加密的核心手段,为企业构建坚不可摧的在线信任桥梁提供指南。一、企业级OVSSL证书概述OVSSL证书,即组织验证型......
  • you will hear two long conversations. At the end of each conversation, you will
    Directions:inthissection,youwillheartwolongconversations.Attheendofeachconversation,youwillhearfourquestions.Boththeconversationandthequestionswillbespokenonlyonce.Afteryouhearaquestion,youmustchoosethebestanswer......
  • 展示字符串信息加密与解密的过程
    声明:该内容皆为原创,仅供业内人士相互学习交流经验,任何未经授权复制、转载、传播或使用本网站(或应用程序)内容的行为,将受到法律的制裁。如因侵权行为给本网站(或应用程序)或任何第三方造成损失的,侵权人应当承担相应的法律责任)实现编译器:vs2022   编译器建议使用13、19、22等......