栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java javax.crypto.IllegalBlockSizeException: Data must not be longer than 245 bytes

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java javax.crypto.IllegalBlockSizeException: Data must not be longer than 245 bytes

问题: 1.RSA加密出现加密长度过长 java javax.crypto.IllegalBlockSizeException: Data must not be longer than 245 bytes 2.RSA解密出现解密长度过长 java javax.crypto.IllegalBlockSizeException: Data must not be longer than 256 bytes

采用分层加密方式进行加密、解密

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.ArrayUtils;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.*;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Enumeration;


public class RSAUtils {
    private static String KEY_ALIAS = null;
    private static final String CERT_TYPE = "X.509";
    // 最大的加密明文长度
    public static final int MAX_ENCRYPT_BLOCK = 245;

    // 最大的解密密文长度
    public static final int MAX_DECRYPT_BLOCK = 256;

    
    public static KeyStore getKeyStore(String digitalCertificateUrl,String password){
        try {
            KeyStore ks = KeyStore.getInstance("PKCS12");
            FileInputStream fis = new FileInputStream(digitalCertificateUrl);
            char[] nPassword = null;
            if ((password == null) || password.trim().equals("")) {
                nPassword = null;
            } else {
                nPassword = password.toCharArray();
            }
            ks.load(fis, nPassword);
            fis.close();
            Enumeration enumas = ks.aliases();
            if (enumas.hasMoreElements())
            {
                KEY_ALIAS = (String) enumas.nextElement();
            }
            return ks;
        } catch (KeyStoreException | NoSuchAlgorithmException
                | CertificateException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    
    public static PrivateKey getPrivateKey(KeyStore keyStore, String alias,String password){
        try {
            PrivateKey prikey = (PrivateKey) keyStore.getKey(alias, password.toCharArray());
            return prikey;
        }catch (UnrecoverableKeyException | KeyStoreException
                | NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    
    public static PublicKey getPublicKey(java.security.cert.Certificate certificate){
        return certificate.getPublicKey();
    }

    
    public static X509Certificate getCertificateByKeystore(KeyStore keyStore,
                                                           String alias) {
        try {
            return (X509Certificate) keyStore.getCertificate(alias);
        } catch (KeyStoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    
    public static X509Certificate getCertificateByCertPath(String path,
                                                           String certType) throws IOException {
        InputStream inputStream = null;
        try {
            // 实例化证书工厂
            CertificateFactory factory = CertificateFactory
                    .getInstance(certType);
            // 取得证书文件流
            inputStream = new FileInputStream(path);
            // 生成证书
            java.security.cert.Certificate certificate = factory.generateCertificate(inputStream);

            return (X509Certificate) certificate;
        } catch (CertificateException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (null != inputStream) {
                inputStream.close();
            }
        }
        return null;

    }

    
    public static byte[] sign(X509Certificate certificate,
                              PrivateKey privateKey, byte[] plainText) {
        
        try {
            Signature signature = Signature.getInstance(certificate
                    .getSigAlgName());
            signature.initSign(privateKey);
            signature.update(plainText);
            return signature.sign();
        } catch (NoSuchAlgorithmException | InvalidKeyException
                | SignatureException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    
    public static boolean verify(X509Certificate certificate, byte[] decodedText, final byte[] receivedignature) {
        try {
            Signature signature = Signature.getInstance(certificate
                    .getSigAlgName());
            
            signature.initVerify(certificate);
            signature.update(decodedText);
            return signature.verify(receivedignature);
        } catch (NoSuchAlgorithmException | InvalidKeyException
                | SignatureException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }
    
    public static byte[] encode(byte[] plainText, PrivateKey privateKey) {
        try {
            Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm());
            cipher.init(Cipher.ENCRYPT_MODE, privateKey);
//            return cipher.doFinal(plainText);
            //分段加密
            int inputLen = plainText.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(plainText, offSet,MAX_ENCRYPT_BLOCK);
                }else {
                    cache = cipher.doFinal(plainText,offSet,inputLen - offSet);
                }
                out.write(cache,0,cache.length);
                i++;
                offSet = i*MAX_ENCRYPT_BLOCK;
            }
            byte[] decryptedData = out.toByteArray();
            out.close();
            return decryptedData;
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }
    
    public static byte[] decode(byte[] encodedText, PublicKey publicKey) {
        try {
            Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());
            cipher.init(Cipher.DECRYPT_MODE, publicKey);
//            return cipher.doFinal(encodedText);
            //分段解密
            byte[] enBytes = null;
            for (int i = 0; i < encodedText.length; i += 256){
                //注意要使用2的倍数,否则会出现加密后的内容再解密时为乱码
                byte[] doFinal = cipher.doFinal(ArrayUtils.subarray(encodedText, i, i + 256));
                enBytes = ArrayUtils.addAll(enBytes, doFinal);
            }
            return enBytes;
        } catch (NoSuchAlgorithmException | NoSuchPaddingException
                | InvalidKeyException | IllegalBlockSizeException
                | BadPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    
    public static byte[] encode(byte[] plainText, PublicKey publicKey) {
        try {
            Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
//            return cipher.doFinal(plainText);
            //分段加密
            int inputLen = plainText.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(plainText, offSet,MAX_ENCRYPT_BLOCK);
                }else {
                    cache = cipher.doFinal(plainText,offSet,inputLen - offSet);
                }
                out.write(cache,0,cache.length);
                i++;
                offSet = i*MAX_ENCRYPT_BLOCK;
            }
            byte[] decryptedData = out.toByteArray();
            out.close();
            return decryptedData;
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    
    public static byte[] decode(byte[] encodedText, PrivateKey privateKey) {
        try {
            Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm());
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            //分段解密
            byte[] enBytes = null;
            for (int i = 0; i < encodedText.length; i += 256){
                //注意要使用2的倍数,否则会出现加密后的内容再解密时为乱码
                byte[] doFinal = cipher.doFinal(ArrayUtils.subarray(encodedText, i, i + 256));
                enBytes = ArrayUtils.addAll(enBytes, doFinal);
            }
            return enBytes;
//            return cipher.doFinal(encodedText);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException
                | InvalidKeyException | IllegalBlockSizeException
                | BadPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    
    public static String setPrivateSign(String url,String password,String info){
        KeyStore keyStore = getKeyStore(url,password);//加载密钥库
        PrivateKey prikey = getPrivateKey(keyStore,KEY_ALIAS, password);
        X509Certificate certificate = getCertificateByKeystore(keyStore, KEY_ALIAS);//通过密钥库获取数字证书,不需要密码,因为获取到Keystore实例
        byte[] signature = sign(certificate, prikey, info.getBytes());//从证书中获取加密算法,进行签名
        return Base64.encodeBase64String(signature);
    }

    
    public static Boolean verifySign(String url, String password, String signature, String content) {
        KeyStore keyStore = getKeyStore(url,password);//加载密钥库
        PrivateKey prikey = getPrivateKey(keyStore,KEY_ALIAS, password);
        X509Certificate certificate = getCertificateByKeystore(keyStore, KEY_ALIAS);//通过密钥库获取数字证书,不需要密码,因为获取到Keystore实例
        return verify(certificate, Base64.decodeBase64(content), signature.getBytes());
    }
    
    public static String setEncode(String url, String content){
        try {
            X509Certificate receivedCertificate = getCertificateByCertPath(url, CERT_TYPE);//获取证书
            PublicKey publicKey = getPublicKey(receivedCertificate);//获取公钥
            System.out.println("字节长度=="+content.getBytes("UTF-8").length);
            byte[] encodedText = encode(content.getBytes("UTF-8"), publicKey);//公钥加密
//            System.out.println("加密="+encodedText);
            return Base64.encodeBase64String(encodedText);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    
    public static  String getDecode(String url, String password, String content){
        try{
            KeyStore keyStore = getKeyStore(url,password );//加载密钥库
            PrivateKey privateKey = getPrivateKey(keyStore, KEY_ALIAS, password);//获取私钥
            byte[] decodedText = decode(Base64.decodeBase64(content), privateKey);//私钥解密
//            System.out.println("解密="+decodedText);
            return new String(decodedText, "UTF-8");
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    
    public static String setPriEncode(String url,String password, String content){
        try {
            KeyStore keyStore = getKeyStore(url,password );//加载密钥库
            PrivateKey privateKey = getPrivateKey(keyStore, KEY_ALIAS, password);//获取私钥
            byte[] encodedText = encode(content.getBytes("UTF-8"), privateKey);//私钥加密
//            System.out.println("加密="+encodedText);
            return Base64.encodeBase64String(encodedText);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    
    public static  String getPubDecode(String url, String content){
        try{
            X509Certificate receivedCertificate = getCertificateByCertPath(url, CERT_TYPE);//获取证书
            PublicKey publicKey = getPublicKey(receivedCertificate);//获取公钥
            byte[] decodedText = decode(Base64.decodeBase64(content), publicKey);//公钥解密
//            System.out.println("解密="+decodedText);
            return new String(decodedText, "UTF-8");
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/995725.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号