All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.founder.sdk.utils.RSAEncodeUtil Maven / Gradle / Ivy

There is a newer version: 3.6.1.9
Show newest version
package com.founder.sdk.utils;

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

import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class RSAEncodeUtil {


    /**
     * RSA最大加密明文大小
     */
    private static final int MAX_ENCRYPT_BLOCK = 117;

    /**
     * RSA最大解密密文大小
     */
    private static final int MAX_DECRYPT_BLOCK = 128;

    //public static String privateKey = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAKx1bpyh6A12DukVrQxhZbmaSmeaS335leLTY2xXRTGfshh0X6f9roPu+oSQFNq0ITMAyeNQ6XBhrNOmvwV+WZAVreaRTxwD91/05VObQVbNBXhd5ebSws5CUDd/oUWMejrfjexOYo2XjIpO4goBZaSjdEH+DtlBbdqkM/OtwypjAgMBAAECgYA7PrRbFrvTNAV3ST0DjcoWcsywvq7EfiNhouD67RgWLhhOklvyKBH1vPlO5PhlEZB+Jv00HC21r7hhlVz2FCvL5US6itKUE0U6rjV1XoUTF8a/4/sawYRBSYWeln0GvCbvsEN4qNSVdjjhGHt+N7nf1BcIaF9PJRPOhjnqWuMU0QJBANuhmRhpjv6YxQjfJJI/dBfFv+qvZ6PMY7OSRv+UeSbzaQpRdSLOno8Dti72R959VEf2idZ10gP67njKS/Yl/80CQQDJBCP/PXtOoKWSQyKv1VEbnBmR/lExebF3w0hNtHT3GR3vTrGRFNQk0ieXyMBZQiXDXTGIRlLChVMqP3aWMMLvAkEAt4IQDsu0BJnUl5MKVX/bGjnKHuar67pM268uz0FY8OiULWDeRFTrOodZY2e3qPiCwRYHGT+cWGMDeb1dNq+NSQJBAJcnzu3CzLNOZ9K5Ox8feDMbybqXk3RxvvqA1SDhXsbkkzb9ZNbk47WgdfdUFRfJft4OeQ2xW9A8M1JkkIbVue8CQQCeMWH5CZ3fbfTJCRAlZ/08K9mirqUShAs0x3tkC31FQOOQY6qdrnmzQKsihhb++9Vtrjkjnr5YgWnwMcKBlknb";
    //private static String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCsdW6coegNdg7pFa0MYWW5mkpnmkt9+ZXi02NsV0Uxn7IYdF+n/a6D7vqEkBTatCEzAMnjUOlwYazTpr8FflmQFa3mkU8cA/df9OVTm0FWzQV4XeXm0sLOQlA3f6FFjHo6343sTmKNl4yKTuIKAWWko3RB/g7ZQW3apDPzrcMqYwIDAQAB";

    /**
     * 获取密钥对
     *
     * @return 密钥对
     */
    public static KeyPair getKeyPair() throws Exception {
        KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
        generator.initialize(1024);
        return generator.generateKeyPair();
    }

    /**
     * 获取私钥
     *
     * @param privateKey 私钥字符串
     * @return
     */
    public static PrivateKey getPrivateKey(String privateKey) throws Exception {
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        byte[] decodedKey = Base64.decodeBase64(privateKey);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decodedKey);
        return keyFactory.generatePrivate(keySpec);
    }

    /**
     * 获取公钥
     *
     * @param publicKey 公钥字符串
     * @return
     */
    public static PublicKey getPublicKey(String publicKey) throws Exception {
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        byte[] decodedKey = Base64.decodeBase64(publicKey.getBytes());
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(decodedKey);
        return keyFactory.generatePublic(keySpec);
    }

    /**
     * RSA加密
     *
     * @param data 待加密数据
     * @param publicKey 公钥
     * @return
     */
    public static String encrypt(String data, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        int inputLen = data.getBytes().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.getBytes(), offset, MAX_ENCRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(data.getBytes(), offset, inputLen - offset);
            }
            out.write(cache, 0, cache.length);
            i++;
            offset = i * MAX_ENCRYPT_BLOCK;
        }
        byte[] encryptedData = out.toByteArray();
        out.close();
        // 获取加密内容使用base64进行编码,并以UTF-8为标准转化成字符串
        // 加密后的字符串
        return new String(Base64.encodeBase64String(encryptedData));
    }


    /**
     * RSA解密
     *
     * @param data 待解密数据
     * @param privateKey 私钥
     * @return
     */
    public static String decrypt(String data, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] dataBytes = Base64.decodeBase64(data);
        int inputLen = dataBytes.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(dataBytes, offset, MAX_DECRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(dataBytes, offset, inputLen - offset);
            }
            out.write(cache, 0, cache.length);
            i++;
            offset = i * MAX_DECRYPT_BLOCK;
        }
        byte[] decryptedData = out.toByteArray();
        out.close();
        // 解密后的内容
        return new String(decryptedData, "UTF-8");
    }

    public static void main(String args[])
    {
        int a=10;
        int b[][];
        b=new int[a][];
        for(int i=1;i<=10;i++)
        {
            b[i-1]=new int[i];
        }
        for(int j=0;j<10;j++)
        {
            for(int k=0;k<=j;k++)
            {
                if(j==0||k==0||k==j)
                {
                    b[j][k]=1;
                    continue;
                }
                else
                {
                    b[j][k]=b[j-1][k-1]+b[j-1][k];
                }
            }
        }
        for(int m=0;m<10;m++)
        {
            for(int n=0;n<=m;n++)
            {
                System.out.print(b[m][n]+" ");
            }
            System.out.println(" ");
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy