RsaHelper.java 10.2 KB
Newer Older
yanzg's avatar
yanzg committed
1 2
package com.yanzuoguang.util.helper;

gongzuquan's avatar
gongzuquan committed
3
import com.yanzuoguang.util.YzgError;
yanzg's avatar
yanzg committed
4 5 6 7
import com.yanzuoguang.util.contants.SystemContants;
import com.yanzuoguang.util.exception.CodeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
yanzg's avatar
yanzg committed
8
import org.springframework.util.Base64Utils;
yanzg's avatar
yanzg committed
9 10

import javax.crypto.Cipher;
yanzg's avatar
yanzg committed
11
import java.io.ByteArrayOutputStream;
yanzg's avatar
yanzg committed
12 13 14 15 16 17 18 19
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * RSA
yanzg's avatar
yanzg committed
20 21 22 23 24 25 26 27 28 29 30 31
 * # 知识点
 * 1. 从一个固定长度的字节数组转换为另外一个固定长度的字节数组。如117的加密对应128的解密
 * 2. RSA分为公钥+私钥
 * 3. 可以用公钥加密+私钥解密 或者 私钥加密+公钥解密
 * 4. 因为加密只有字节,所以需要转换为base64
 * <p>
 * # 流程
 * 1. 生成公钥私钥
 * 2. 将公钥转换为字节,然后转换为base64字符串
 * 3. 将私钥转换为字节,然后转换为base64字符串
 * 4. 将来源字符串转换为字节,并按照固定加密长度截断,依次用公钥字符串生成的公钥进行加密。将加密后的字节转换为base64字符串。
 * 5. 将需要解密的字符串转换为字节,按照固定解密长度阶段,依次用私钥字符串生成的私钥进行解密。
yanzg's avatar
yanzg committed
32 33 34 35 36 37 38 39 40 41 42 43
 *
 * @author 颜佐光
 */
public final class RsaHelper {

    private static final Logger logger = LoggerFactory.getLogger(RsaHelper.class);

    private static final String ALGORITHM_RSA = "RSA";

    private static final String ALGORITHM_SIGN = "MD5withRSA";

    private static final int KEYPAIR_LEN = 1024;
yanzg's avatar
yanzg committed
44 45 46 47 48 49 50 51 52
    /**
     * RSA最大加密明文大小
     */
    private static final int MAX_ENCRYPT_BLOCK = 117;

    /**
     * RSA最大解密密文大小
     */
    private static final int MAX_DECRYPT_BLOCK = 128;
yanzg's avatar
yanzg committed
53 54 55 56 57

    private RsaHelper() {
        super();
    }

yanzg's avatar
yanzg committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    private interface HandleBytes {
        byte[] handle(byte[] from, int offset, int len) throws Exception;
    }

    private static byte[] handle(byte[] froms, int size, HandleBytes handle) throws Exception {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int inputLen = froms.length;
        int offSet = 0;
        byte[] cache;
        int i = 0;
        // 对数据分段加密
        while (inputLen - offSet > 0) {
            if (inputLen - offSet > size) {
                cache = handle.handle(froms, offSet, size);
            } else {
                cache = handle.handle(froms, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * size;
        }
        byte[] to = out.toByteArray();
        out.close();
        return to;
    }

yanzg's avatar
yanzg committed
84 85 86 87 88
    /**
     * 生成密钥对
     *
     * @throws Exception
     */
yanzg's avatar
yanzg committed
89 90 91 92 93 94
    public static void generatorKeyPair() {
        try {
            KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM_RSA);
            keyPairGen.initialize(KEYPAIR_LEN);
            KeyPair keyPair = keyPairGen.generateKeyPair();
            RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
yanzg's avatar
yanzg committed
95 96
            RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();

yanzg's avatar
yanzg committed
97 98 99 100 101 102 103 104 105
            byte[] keyBs = rsaPublicKey.getEncoded();
            String publicKey = encodeBase64(keyBs);
            logger.info("生成的公钥:\t{}", publicKey);
            keyBs = rsaPrivateKey.getEncoded();
            String privateKey = encodeBase64(keyBs);
            logger.info("生成的私钥:\t{}", privateKey);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
yanzg's avatar
yanzg committed
106 107 108 109 110 111 112 113 114
    }

    /**
     * 获取公钥
     *
     * @return
     * @throws Exception
     */
    private static PublicKey getPublicKey(String publicKey) throws Exception {
yanzg's avatar
yanzg committed
115 116 117 118 119 120 121
        try {
            X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(decodeBase64(publicKey));
            KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA);
            return keyFactory.generatePublic(publicKeySpec);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
yanzg's avatar
yanzg committed
122 123 124 125 126 127 128 129
    }

    /**
     * 获取私钥
     *
     * @return
     * @throws Exception
     */
yanzg's avatar
yanzg committed
130 131 132 133 134 135 136 137
    private static PrivateKey getPrivateKey(String privateKey) {
        try {
            PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(decodeBase64(privateKey));
            KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA);
            return keyFactory.generatePrivate(privateKeySpec);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
yanzg's avatar
yanzg committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    }

    /**
     * 公钥加密
     *
     * @param source
     * @param publicKeyStr
     * @return
     * @throws Exception
     */
    public static String encryptionByPublicKey(String source, String publicKeyStr) {
        try {
            PublicKey publicKey = getPublicKey(publicKeyStr);
            Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
yanzg's avatar
yanzg committed
153

yanzg's avatar
yanzg committed
154 155 156 157 158
            byte[] bytes = source.getBytes(SystemContants.UTF8);
            byte[] to = handle(bytes, MAX_ENCRYPT_BLOCK, new HandleBytes() {
                @Override
                public byte[] handle(byte[] from, int offset, int len) throws Exception {
                    return cipher.doFinal(from, offset, len);
yanzg's avatar
yanzg committed
159
                }
yanzg's avatar
yanzg committed
160 161
            });
            return encodeBase64(to);
yanzg's avatar
yanzg committed
162 163 164 165 166 167 168 169 170 171 172
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 公钥解密
     *
     * @param target
     * @throws Exception
     */
yanzg's avatar
yanzg committed
173 174 175
    public static String decryptionByPublicKey(String target, String publicKeyStr) {
        try {
            byte[] bytes = decodeBase64(target);
yanzg's avatar
yanzg committed
176

yanzg's avatar
yanzg committed
177 178 179
            PublicKey publicKey = getPublicKey(publicKeyStr);
            Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());
            cipher.init(Cipher.DECRYPT_MODE, publicKey);
yanzg's avatar
yanzg committed
180

yanzg's avatar
yanzg committed
181
            byte[] to = handle(bytes, MAX_DECRYPT_BLOCK, new HandleBytes() {
yanzg's avatar
yanzg committed
182 183 184 185 186 187 188
                @Override
                public byte[] handle(byte[] from, int offset, int len) throws Exception {
                    return cipher.doFinal(from, offset, len);
                }
            });

            return new String(to, SystemContants.UTF8);
yanzg's avatar
yanzg committed
189 190 191
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
yanzg's avatar
yanzg committed
192 193 194 195 196 197 198 199
    }

    /**
     * 公钥验证签名
     *
     * @return
     * @throws Exception
     */
yanzg's avatar
yanzg committed
200 201 202 203 204 205 206 207 208 209 210 211 212
    public static void verifyByPublicKey(String target, String sign, String publicKeyStr) {
        try {
            PublicKey publicKey = getPublicKey(publicKeyStr);
            Signature signature = Signature.getInstance(ALGORITHM_SIGN);
            signature.initVerify(publicKey);
            signature.update(target.getBytes(SystemContants.UTF8));
            if (signature.verify(decodeBase64(sign))) {
                logger.info("sign true");
            } else {
                logger.info("sign false");
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
yanzg's avatar
yanzg committed
213 214 215 216 217 218 219 220 221 222
        }
    }

    /**
     * 私钥加密
     *
     * @param source
     * @return
     * @throws Exception
     */
yanzg's avatar
yanzg committed
223 224
    public static String encryptionByPrivateKey(String source, String privateKeyStr) {
        try {
yanzg's avatar
yanzg committed
225 226
            byte[] bytes = source.getBytes(SystemContants.UTF8);

yanzg's avatar
yanzg committed
227 228 229
            PrivateKey privateKey = getPrivateKey(privateKeyStr);
            Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm());
            cipher.init(Cipher.ENCRYPT_MODE, privateKey);
yanzg's avatar
yanzg committed
230 231 232 233 234 235 236 237 238

            byte[] to = handle(bytes, MAX_ENCRYPT_BLOCK, new HandleBytes() {
                @Override
                public byte[] handle(byte[] from, int offset, int len) throws Exception {
                    return cipher.doFinal(from, offset, len);
                }
            });

            String target = encodeBase64(to);
yanzg's avatar
yanzg committed
239 240 241 242
            return target;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
yanzg's avatar
yanzg committed
243 244 245 246 247 248 249 250 251 252
    }

    /**
     * 私钥解密
     *
     * @param target
     * @throws Exception
     */
    public static String decryptionByPrivateKey(String target, String privateKeyStr) {
        try {
yanzg's avatar
yanzg committed
253 254
            byte[] bytes = decodeBase64(target);

yanzg's avatar
yanzg committed
255 256 257
            PrivateKey privateKey = getPrivateKey(privateKeyStr);
            Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm());
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
yanzg's avatar
yanzg committed
258

yanzg's avatar
yanzg committed
259
            byte[] to = handle(bytes, MAX_DECRYPT_BLOCK, new HandleBytes() {
yanzg's avatar
yanzg committed
260 261 262 263 264 265 266
                @Override
                public byte[] handle(byte[] from, int offset, int len) throws Exception {
                    return cipher.doFinal(from, offset, len);
                }
            });

            return new String(to, SystemContants.UTF8);
yanzg's avatar
yanzg committed
267
        } catch (Exception ex) {
gongzuquan's avatar
gongzuquan committed
268
            throw YzgError.getRuntimeException(ex,"056",ex.getMessage());
yanzg's avatar
yanzg committed
269 270 271 272 273 274 275 276 277 278
        }
    }

    /**
     * 私钥签名
     *
     * @param target
     * @return
     * @throws Exception
     */
yanzg's avatar
yanzg committed
279 280 281 282 283 284 285 286 287 288 289
    public static String signByPrivateKey(String target, String privateKeyStr) {
        try {
            PrivateKey privateKey = getPrivateKey(privateKeyStr);
            Signature signature = Signature.getInstance(ALGORITHM_SIGN);
            signature.initSign(privateKey);
            signature.update(target.getBytes(SystemContants.UTF8));
            String sign = encodeBase64(signature.sign());
            return sign;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
yanzg's avatar
yanzg committed
290 291 292 293 294 295 296 297 298
    }

    /**
     * base64编码
     *
     * @param source
     * @return
     * @throws Exception
     */
yanzg's avatar
yanzg committed
299
    public static String encodeBase64(byte[] source) {
yanzg's avatar
yanzg committed
300 301 302 303 304 305
        try {
            byte[] to = Base64Utils.encode(source);
            return new String(to, SystemContants.UTF8);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
yanzg's avatar
yanzg committed
306 307 308 309 310 311 312 313 314
    }

    /**
     * Base64解码
     *
     * @param target
     * @return
     * @throws Exception
     */
yanzg's avatar
yanzg committed
315
    public static byte[] decodeBase64(String target) {
yanzg's avatar
yanzg committed
316 317 318 319 320 321
        try {
            byte[] from = target.getBytes(SystemContants.UTF8);
            return Base64Utils.decode(from);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
yanzg's avatar
yanzg committed
322 323
    }
}