|
| 1 | +package com.fantasy.blogdemo.crypto.utils; |
| 2 | + |
| 3 | +import android.util.Base64; |
| 4 | + |
| 5 | +import com.fantasy.blogdemo.utils.ConvertUtils; |
| 6 | + |
| 7 | +import java.security.Key; |
| 8 | +import java.security.KeyFactory; |
| 9 | +import java.security.spec.PKCS8EncodedKeySpec; |
| 10 | +import java.security.spec.X509EncodedKeySpec; |
| 11 | + |
| 12 | +import javax.crypto.Cipher; |
| 13 | + |
| 14 | +/** |
| 15 | + * RSA加解密工具类 |
| 16 | + * <pre> |
| 17 | + * author : Fantasy |
| 18 | + * version : 1.0, 2019-08-25 |
| 19 | + * since : 1.0, 2019-08-25 |
| 20 | + * </pre> |
| 21 | + */ |
| 22 | +public class RSAUtils { |
| 23 | + private static final String CHARSET = "UTF-8"; |
| 24 | + |
| 25 | + /** |
| 26 | + * 加密,输出Base64字符串密文 |
| 27 | + * |
| 28 | + * @param data 明文 |
| 29 | + * @param publicKey 公钥 |
| 30 | + * @param keySize 公钥大小,举例:1024, 2048... |
| 31 | + * @param transformation 类型,格式为:加密算法/加密模式/填充方式,举例:<i>RSA/None/PKCS1Padding</i>。<br/> |
| 32 | + * 相关取值可以查看下列两个文档: |
| 33 | + * <ul> |
| 34 | + * <li><a href="https://docs.oracle.com/javase/8/docs/api">JavaSE 8 API</a> |
| 35 | + * 中的 javax.crypto.Cipher</li> |
| 36 | + * <li><a href="https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#Cipher"> |
| 37 | + * Standard Algorithm Name Documentation</a></li> |
| 38 | + * </ul> |
| 39 | + * @return 密文 |
| 40 | + */ |
| 41 | + public static String encryptBase64(String data, String publicKey, int keySize, String transformation) { |
| 42 | + try { |
| 43 | + return Base64.encodeToString(handle(data.getBytes(CHARSET), Base64.decode(publicKey, Base64.NO_WRAP), |
| 44 | + keySize, transformation, true), Base64.NO_WRAP); |
| 45 | + } catch (Exception e) { |
| 46 | + e.printStackTrace(); |
| 47 | + } |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * 解密,密文为Base64字符串 |
| 53 | + * |
| 54 | + * @param data 密文 |
| 55 | + * @param privateKey 私钥 |
| 56 | + * @param keySize 私钥大小,举例:1024, 2048... |
| 57 | + * @param transformation 类型,格式为:加密算法/加密模式/填充方式,举例:<i>RSA/None/PKCS1Padding</i>。<br/> |
| 58 | + * 相关取值可以查看下列两个文档: |
| 59 | + * <ul> |
| 60 | + * <li><a href="https://docs.oracle.com/javase/8/docs/api">JavaSE 8 API</a> |
| 61 | + * 中的 javax.crypto.Cipher</li> |
| 62 | + * <li><a href="https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#Cipher"> |
| 63 | + * Standard Algorithm Name Documentation</a></li> |
| 64 | + * </ul> |
| 65 | + * @return 明文 |
| 66 | + */ |
| 67 | + public static String decryptBase64(String data, String privateKey, int keySize, String transformation) { |
| 68 | + try { |
| 69 | + return new String(handle(Base64.decode(data, Base64.NO_WRAP), Base64.decode(privateKey, Base64.NO_WRAP), |
| 70 | + keySize, transformation, false), CHARSET); |
| 71 | + } catch (Exception e) { |
| 72 | + e.printStackTrace(); |
| 73 | + } |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * 加密,输出十六进制字符串密文 |
| 79 | + * |
| 80 | + * @param data 明文 |
| 81 | + * @param publicKey 公钥 |
| 82 | + * @param keySize 公钥大小,举例:1024, 2048... |
| 83 | + * @param transformation 类型,格式为:加密算法/加密模式/填充方式,举例:<i>RSA/None/PKCS1Padding</i>。<br/> |
| 84 | + * 相关取值可以查看下列两个文档: |
| 85 | + * <ul> |
| 86 | + * <li><a href="https://docs.oracle.com/javase/8/docs/api">JavaSE 8 API</a> |
| 87 | + * 中的 javax.crypto.Cipher</li> |
| 88 | + * <li><a href="https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#Cipher"> |
| 89 | + * Standard Algorithm Name Documentation</a></li> |
| 90 | + * </ul> |
| 91 | + * @return 密文 |
| 92 | + */ |
| 93 | + public static String encryptHex(String data, String publicKey, int keySize, String transformation) { |
| 94 | + try { |
| 95 | + return ConvertUtils.bytesToHexString(handle(data.getBytes(CHARSET), Base64.decode(publicKey, Base64.NO_WRAP), |
| 96 | + keySize, transformation, true)); |
| 97 | + } catch (Exception e) { |
| 98 | + e.printStackTrace(); |
| 99 | + } |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * 解密,密文为十六进制字符串 |
| 105 | + * |
| 106 | + * @param data 密文 |
| 107 | + * @param privateKey 私钥 |
| 108 | + * @param keySize 私钥大小,举例:1024, 2048... |
| 109 | + * @param transformation 类型,格式为:加密算法/加密模式/填充方式,举例:<i>RSA/None/PKCS1Padding</i>。<br/> |
| 110 | + * 相关取值可以查看下列两个文档: |
| 111 | + * <ul> |
| 112 | + * <li><a href="https://docs.oracle.com/javase/8/docs/api">JavaSE 8 API</a> |
| 113 | + * 中的 javax.crypto.Cipher</li> |
| 114 | + * <li><a href="https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#Cipher"> |
| 115 | + * Standard Algorithm Name Documentation</a></li> |
| 116 | + * </ul> |
| 117 | + * @return 明文 |
| 118 | + */ |
| 119 | + public static String decryptHex(String data, String privateKey, int keySize, String transformation) { |
| 120 | + try { |
| 121 | + return new String(handle(ConvertUtils.hexStringToBytes(data), Base64.decode(privateKey, Base64.NO_WRAP), |
| 122 | + keySize, transformation, false), CHARSET); |
| 123 | + } catch (Exception e) { |
| 124 | + e.printStackTrace(); |
| 125 | + } |
| 126 | + return null; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * 处理数据,加密或解密 |
| 131 | + * |
| 132 | + * @param data 数据 |
| 133 | + * @param key 密钥 |
| 134 | + * @param keySize 密钥大小,举例:1024, 2048... |
| 135 | + * @param transformation 类型,格式为:加密算法/加密模式/填充方式,举例:<i>RSA/None/PKCS1Padding</i>。<br/> |
| 136 | + * 相关取值可以查看下列两个文档: |
| 137 | + * <ul> |
| 138 | + * <li><a href="https://docs.oracle.com/javase/8/docs/api">JavaSE 8 API</a> |
| 139 | + * 中的 javax.crypto.Cipher</li> |
| 140 | + * <li><a href="https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#Cipher"> |
| 141 | + * Standard Algorithm Name Documentation</a></li> |
| 142 | + * </ul> |
| 143 | + * @param isEncrypt 如果是加密,则为true;如果为解密,则为false |
| 144 | + * @return 加密后或解密后的字节数组 |
| 145 | + * @throws Exception 异常 |
| 146 | + */ |
| 147 | + private static byte[] handle(byte[] data, byte[] key, int keySize, String transformation, |
| 148 | + boolean isEncrypt) throws Exception { |
| 149 | + Key rsaKey; |
| 150 | + if (isEncrypt) { |
| 151 | + X509EncodedKeySpec keySpec = new X509EncodedKeySpec(key); |
| 152 | + rsaKey = KeyFactory.getInstance("RSA").generatePublic(keySpec); |
| 153 | + } else { |
| 154 | + PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(key); |
| 155 | + rsaKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec); |
| 156 | + } |
| 157 | + Cipher cipher = Cipher.getInstance(transformation); |
| 158 | + cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, rsaKey); |
| 159 | + int len = data.length; |
| 160 | + int maxLen = keySize / 8; |
| 161 | + if (isEncrypt) { |
| 162 | + String lowerTrans = transformation.toLowerCase(); |
| 163 | + if (lowerTrans.endsWith("pkcs1padding")) { |
| 164 | + maxLen -= 11; |
| 165 | + } |
| 166 | + } |
| 167 | + int count = len / maxLen; |
| 168 | + if (count > 0) { |
| 169 | + byte[] ret = new byte[0]; |
| 170 | + byte[] buff = new byte[maxLen]; |
| 171 | + int index = 0; |
| 172 | + for (int i = 0; i < count; i++) { |
| 173 | + System.arraycopy(data, index, buff, 0, maxLen); |
| 174 | + ret = joins(ret, cipher.doFinal(buff)); |
| 175 | + index += maxLen; |
| 176 | + } |
| 177 | + if (index != len) { |
| 178 | + int restLen = len - index; |
| 179 | + buff = new byte[restLen]; |
| 180 | + System.arraycopy(data, index, buff, 0, restLen); |
| 181 | + ret = joins(ret, cipher.doFinal(buff)); |
| 182 | + } |
| 183 | + return ret; |
| 184 | + } else { |
| 185 | + return cipher.doFinal(data); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * 合并两个字节数组 |
| 191 | + * |
| 192 | + * @param prefix 前一个字节数组 |
| 193 | + * @param suffix 后一个字节数组 |
| 194 | + * @return 字节数组 |
| 195 | + */ |
| 196 | + private static byte[] joins(byte[] prefix, byte[] suffix) { |
| 197 | + byte[] ret = new byte[prefix.length + suffix.length]; |
| 198 | + System.arraycopy(prefix, 0, ret, 0, prefix.length); |
| 199 | + System.arraycopy(suffix, 0, ret, prefix.length, suffix.length); |
| 200 | + return ret; |
| 201 | + } |
| 202 | + |
| 203 | +} |
0 commit comments