RSA加密

RSA加密用的是双方密钥进行加密的方式
1、输出方:用对方的公钥对参数加密,用本方的私钥对参数加签;

2、接收方:用本方的私钥解密,用对方的公钥对参数验签

不多说直接上代码,一看就懂!

RSAUtil类:

package com.wolf.pc.RSA;

import javax.crypto.Cipher;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class RSAUtil extends CoderUtil {

    public final static String UTF8 = "UTF-8";
    public final static String LOCAL_PUBLICKEY = "LOCAL_PUBLICKEY";  //替换成本方的公钥
    public final static String LOCAL_PRIVATEKEY = "LOCAL_PRIVATEKEY"; //替换成本方的私钥

    public final static String OTHER_PUBLICKEY = "";  //替换成对方的公钥
    public final static String OTHER_PRIVATEKEY = "OTHER_PRIVATEKEY"; //替换成对方的私钥

    /*************************************用本方的私钥解密,用对方的公钥解签start**********************************************/
    /**
     * 用本方私钥解密
     * @param data
     * @param privateKey
     * @param charset
     * @return
     * @throws Exception
     */
    public static String decrypt(String data, String privateKey, String charset) throws Exception {
        byte[] byte64 = Base64Util.base64ToByteArray(data);
        byte[] encryptedBytes = decryptByPrivateKey(byte64, privateKey);
        return new String(encryptedBytes, charset);
    }
    public static byte[] decryptByPrivateKey(byte[] data, String key) throws Exception {
        byte[] keyBytes = decryptBASE64(key);

        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        Key privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);

        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(2, privateKey);

        byte[] dataReturn = null;
        for (int i = 0; i < data.length; i += 128) {
            byte[] doFinal = cipher.doFinal(subarray(data, i, i + 128));
            dataReturn = addAll(dataReturn, doFinal);
        }
        return dataReturn;
    }

    /**
     * 用对方公钥解签
     * @param params    需要解签的字符串
     * @param publicKey 公钥
     * @param charset   编码
     * @param sign
     * @return
     * @throws Exception
     */
    public static boolean verifySign(String params, String publicKey, String charset,String sign) throws Exception {
        return verify(params.getBytes(charset), publicKey, sign);
    }

    public static boolean verify(byte[] data, String publicKey, String sign) throws Exception {
        byte[] keyBytes = decryptBASE64(publicKey);

        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes);

        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        PublicKey publicKey2 = keyFactory.generatePublic(x509EncodedKeySpec);

        Signature signature = Signature.getInstance("SHA1WithRSA");
        signature.initVerify(publicKey2);
        signature.update(data);

        return signature.verify(decryptBASE64(sign));
    }
    /*************************************用本方的私钥加密,用对方的公钥解签end**********************************************/

    /*************************************用本方的私钥加签,用对方的公钥加密start**********************************************/
    /**
    * 公钥加密
     * @param paramsString
     * @param charset
     * @param publicKey
     * @return
     * @throws Exception
     */
    public static String encrypt(String paramsString, String charset, String publicKey) throws Exception {
        byte[] encryptedResult = encryptByPublicKey(paramsString.getBytes(charset), publicKey);

        return Base64Util.byteArrayToBase64(encryptedResult);
    }

    public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception {
        byte[] keyBytes = decryptBASE64(publicKey);

        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        Key publicKeyObj = keyFactory.generatePublic(x509EncodedKeySpec);

        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(1, publicKeyObj);

        byte[] dataReturn = null;
        for (int i = 0; i < data.length; i += 117) {
            byte[] doFinal = cipher.doFinal(subarray(data, i, i + 117));
            dataReturn = addAll(dataReturn, doFinal);
        }

        return dataReturn;
    }

    /**
     * 私钥加签
     * @param data
     * @param charset
     * @param privateKey
     * @return
     * @throws Exception
     */
    public static String sign(String data, String charset, String privateKey) throws Exception {
        byte[] dataInBytes = data.getBytes(charset);
        String signParams = sign(dataInBytes, privateKey);
        return signParams;
    }

    public static String sign(byte[] data, String privateKey) throws Exception {
        byte[] keyBytes = decryptBASE64(privateKey);

        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes);

        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        PrivateKey privateKey2 = keyFactory.generatePrivate(pkcs8EncodedKeySpec);

        Signature signature = Signature.getInstance("SHA1withRSA");
        signature.initSign(privateKey2);
        signature.update(data);

        return encryptBASE64(signature.sign());
    }
    /*************************************用本方的私钥加签,用对方的公钥加密end**********************************************/

    public static byte[] subarray(byte[] array, int startIndexInclusive, int endIndexExclusive) {
        if (array == null) {
            return null;
        }
        if (startIndexInclusive < 0) {
            startIndexInclusive = 0;
        }
        if (endIndexExclusive > array.length) {
            endIndexExclusive = array.length;
        }
        int newSize = endIndexExclusive - startIndexInclusive;
        if (newSize <= 0) {
            return new byte[0];
        }

        byte[] subarray = new byte[newSize];
        System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
        return subarray;
    }

    public static byte[] addAll(byte[] array1, byte[] array2) {
        if (array1 == null)
            return clone(array2);
        if (array2 == null) {
            return clone(array1);
        }
        byte[] joinedArray = new byte[array1.length + array2.length];
        System.arraycopy(array1, 0, joinedArray, 0, array1.length);
        System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
        return joinedArray;
    }

    public static byte[] clone(byte[] array) {
        if (array == null) {
            return null;
        }
        return (byte[]) (byte[]) array.clone();
    }

    public static void main(String[] args) {
        try {
            String paramsEncoder= URLEncoder.encode("userId=1111111&userName=张三&idcardNo=4524021998542365",RSAUtil.UTF8);
            System.out.println("paramsEncoder : "+paramsEncoder);

            String enParams = RSAUtil.encrypt(paramsEncoder, RSAUtil.UTF8, OTHER_PUBLICKEY);
            System.out.println("enParams : " + enParams);
            String sign = RSAUtil.sign(paramsEncoder, RSAUtil.UTF8, LOCAL_PRIVATEKEY);
            System.out.println(sign);

            System.out.println("---------------------------------------------encrypt end------------------------------------------------");
            String deParams = RSAUtil.decrypt(enParams,RSAUtil.OTHER_PRIVATEKEY, RSAUtil.UTF8);
            System.out.println("deParams : "+deParams);
            boolean isSign = RSAUtil.verifySign(deParams, LOCAL_PUBLICKEY, UTF8, sign);
            System.out.println(isSign + "| sign : " + sign);
            System.out.println(URLDecoder.decode(deParams, RSAUtil.UTF8));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
CoderUtil.java

package com.wolf.pc.RSA;

import java.security.MessageDigest;

public class CoderUtil {
	public static final String KEY_SHA = "SHA";
	public static final String KEY_MD5 = "MD5";

	public static byte[] decryptBASE64(String key) {
		return Base64Util.base64ToByteArray(key);
	}

	public static String encryptBASE64(byte[] key) throws Exception {
		return Base64Util.byteArrayToBase64(key);
	}

	public static byte[] encryptMD5(byte[] data) throws Exception {
		MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
		md5.update(data);
		return md5.digest();
	}

	public static byte[] encryptSHA(byte[] data) throws Exception {
		MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
		sha.update(data);
		return sha.digest();
	}
}
Base64Util.javai
package com.wolf.pc.RSA;

public class Base64Util extends CoderUtil {
	private static final char[] intToBase64 = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
			'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };

	private static final char[] intToAltBase64 = { '!', '"', '#', '$', '%', '&', '\'', '(', ')', ',', '-', '.', ':', ';', '<', '>', '@', '[', ']', '^', '`', '_', '{', '|', '}', '~', 'a', 'b', 'c',
			'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '?' };

	private static final byte[] base64ToInt = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
			-1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
			22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 };

	private static final byte[] altBase64ToInt = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4,
			5, 6, 7, 8, -1, 62, 9, 10, 11, -1, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 12, 13, 14, -1, 15, 63, 16, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
			-1, -1, -1, -1, -1, 17, -1, 18, 19, 21, 20, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 22, 23, 24, 25 };

	public static String byteArrayToBase64(byte[] a) {
		return byteArrayToBase64(a, false);
	}

	public static String byteArrayToAltBase64(byte[] a) {
		return byteArrayToBase64(a, true);
	}

	private static String byteArrayToBase64(byte[] a, boolean alternate) {
		int aLen = a.length;
		int numFullGroups = aLen / 3;
		int numBytesInPartialGroup = aLen - 3 * numFullGroups;
		int resultLen = 4 * ((aLen + 2) / 3);
		StringBuffer result = new StringBuffer(resultLen);
		char[] intToAlpha = (alternate) ? intToAltBase64 : intToBase64;

		int inCursor = 0;
		for (int i = 0; i < numFullGroups; ++i) {
			int byte0 = a[(inCursor++)] & 0xFF;
			int byte1 = a[(inCursor++)] & 0xFF;
			int byte2 = a[(inCursor++)] & 0xFF;
			result.append(intToAlpha[(byte0 >> 2)]);
			result.append(intToAlpha[(byte0 << 4 & 0x3F | byte1 >> 4)]);
			result.append(intToAlpha[(byte1 << 2 & 0x3F | byte2 >> 6)]);
			result.append(intToAlpha[(byte2 & 0x3F)]);
		}

		if (numBytesInPartialGroup != 0) {
			int byte0 = a[(inCursor++)] & 0xFF;
			result.append(intToAlpha[(byte0 >> 2)]);
			if (numBytesInPartialGroup == 1) {
				result.append(intToAlpha[(byte0 << 4 & 0x3F)]);
				result.append("==");
			} else {
				int byte1 = a[(inCursor++)] & 0xFF;
				result.append(intToAlpha[(byte0 << 4 & 0x3F | byte1 >> 4)]);
				result.append(intToAlpha[(byte1 << 2 & 0x3F)]);
				result.append('=');
			}

		}

		return result.toString();
	}

	public static byte[] base64ToByteArray(String s) {
		return base64ToByteArray(s, false);
	}

	public static byte[] altBase64ToByteArray(String s) {
		return base64ToByteArray(s, true);
	}

	private static byte[] base64ToByteArray(String s, boolean alternate) {
		byte[] alphaToInt = (alternate) ? altBase64ToInt : base64ToInt;
		int sLen = s.length();
		int numGroups = sLen / 4;
		if (4 * numGroups != sLen) {
			throw new IllegalArgumentException("String length must be a multiple of four.");
		}
		int missingBytesInLastGroup = 0;
		int numFullGroups = numGroups;
		if (sLen != 0) {
			if (s.charAt(sLen - 1) == '=') {
				++missingBytesInLastGroup;
				--numFullGroups;
			}
			if (s.charAt(sLen - 2) == '=') {
				++missingBytesInLastGroup;
			}
		}
		byte[] result = new byte[3 * numGroups - missingBytesInLastGroup];

		int inCursor = 0;
		int outCursor = 0;
		for (int i = 0; i < numFullGroups; ++i) {
			int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt);
			int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt);
			int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt);
			int ch3 = base64toInt(s.charAt(inCursor++), alphaToInt);
			result[(outCursor++)] = (byte) (ch0 << 2 | ch1 >> 4);
			result[(outCursor++)] = (byte) (ch1 << 4 | ch2 >> 2);
			result[(outCursor++)] = (byte) (ch2 << 6 | ch3);
		}

		if (missingBytesInLastGroup != 0) {
			int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt);
			int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt);
			result[(outCursor++)] = (byte) (ch0 << 2 | ch1 >> 4);

			if (missingBytesInLastGroup == 1) {
				int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt);
				result[(outCursor++)] = (byte) (ch1 << 4 | ch2 >> 2);
			}

		}

		return result;
	}

	private static int base64toInt(char c, byte[] alphaToInt) {
		int result = alphaToInt[c];
		if (result < 0) {
			throw new IllegalArgumentException("Illegal character " + c);
		}
		return result;
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值