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 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 52 53 54 55 56 57 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
@Service public class HybridEncryptionService { private final HybridEncryptionProperties properties; private final KeyManagementService keyManagementService; private final DigitalSignatureService signatureService; public HybridEncryptionService(HybridEncryptionProperties properties) { this.properties = properties; this.keyManagementService = new KeyManagementService(properties); this.signatureService = new DigitalSignatureService(properties); }
public EncryptionResult encrypt(String data, String publicKey) { try { SecretKey aesKey = generateAESKey(); String encryptedData = encryptWithAES(data, aesKey); String encryptedKey = encryptAESKeyWithRSA(aesKey, publicKey); String signature = null; if (properties.isEnableSignature()) { signature = signatureService.sign(encryptedData); } return EncryptionResult.builder() .success(true) .encryptedData(encryptedData) .encryptedKey(encryptedKey) .signature(signature) .timestamp(LocalDateTime.now()) .build(); } catch (Exception e) { log.error("混合加密失败", e); return EncryptionResult.builder() .success(false) .errorMessage("加密失败: " + e.getMessage()) .timestamp(LocalDateTime.now()) .build(); } }
public DecryptionResult decrypt(String encryptedData, String encryptedKey, String signature, String privateKey) { try { SecretKey aesKey = decryptAESKeyWithRSA(encryptedKey, privateKey); String decryptedData = decryptWithAES(encryptedData, aesKey); boolean signatureValid = true; if (properties.isEnableSignature() && signature != null) { signatureValid = signatureService.verify(encryptedData, signature); } return DecryptionResult.builder() .success(true) .decryptedData(decryptedData) .signature(signature) .signatureValid(signatureValid) .timestamp(LocalDateTime.now()) .build(); } catch (Exception e) { log.error("混合解密失败", e); return DecryptionResult.builder() .success(false) .errorMessage("解密失败: " + e.getMessage()) .timestamp(LocalDateTime.now()) .build(); } }
public EncryptionResult encryptTransaction(TransactionData transactionData, String publicKey) { try { String jsonData = objectToJson(transactionData); String dataToEncrypt = jsonData; if (properties.isEnableCompression()) { dataToEncrypt = compressData(jsonData); } return encrypt(dataToEncrypt, publicKey); } catch (Exception e) { log.error("加密交易数据失败", e); return EncryptionResult.builder() .success(false) .errorMessage("加密交易数据失败: " + e.getMessage()) .timestamp(LocalDateTime.now()) .build(); } }
public DecryptionResult decryptTransaction(String encryptedData, String encryptedKey, String signature, String privateKey) { try { DecryptionResult result = decrypt(encryptedData, encryptedKey, signature, privateKey); if (!result.isSuccess()) { return result; } String jsonData = result.getDecryptedData(); if (properties.isEnableCompression()) { jsonData = decompressData(jsonData); } TransactionData transactionData = jsonToObject(jsonData, TransactionData.class); return DecryptionResult.builder() .success(true) .decryptedData(jsonData) .signature(result.getSignature()) .signatureValid(result.isSignatureValid()) .timestamp(LocalDateTime.now()) .build(); } catch (Exception e) { log.error("解密交易数据失败", e); return DecryptionResult.builder() .success(false) .errorMessage("解密交易数据失败: " + e.getMessage()) .timestamp(LocalDateTime.now()) .build(); } }
private SecretKey generateAESKey() throws NoSuchAlgorithmException { KeyGenerator keyGenerator = KeyGenerator.getInstance(properties.getAesAlgorithm()); keyGenerator.init(properties.getAesKeySize()); return keyGenerator.generateKey(); }
private String encryptWithAES(String data, SecretKey key) throws Exception { Cipher cipher = Cipher.getInstance(properties.getCipherTransformation()); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)); byte[] iv = cipher.getIV(); byte[] combined = new byte[iv.length + encryptedBytes.length]; System.arraycopy(iv, 0, combined, 0, iv.length); System.arraycopy(encryptedBytes, 0, combined, iv.length, encryptedBytes.length); return Base64.getEncoder().encodeToString(combined); }
private String decryptWithAES(String encryptedData, SecretKey key) throws Exception { byte[] combined = Base64.getDecoder().decode(encryptedData); byte[] iv = new byte[properties.getIvLength()]; byte[] encryptedBytes = new byte[combined.length - iv.length]; System.arraycopy(combined, 0, iv, 0, iv.length); System.arraycopy(combined, iv.length, encryptedBytes, 0, encryptedBytes.length); Cipher cipher = Cipher.getInstance(properties.getCipherTransformation()); IvParameterSpec ivSpec = new IvParameterSpec(iv); cipher.init(Cipher.DECRYPT_MODE, key, ivSpec); byte[] decryptedBytes = cipher.doFinal(encryptedBytes); return new String(decryptedBytes, StandardCharsets.UTF_8); }
private String encryptAESKeyWithRSA(SecretKey aesKey, String publicKey) throws Exception { PublicKey rsaPublicKey = keyManagementService.getPublicKeyFromString(publicKey); Cipher cipher = Cipher.getInstance(properties.getRsaTransformation()); cipher.init(Cipher.ENCRYPT_MODE, rsaPublicKey); byte[] encryptedKey = cipher.doFinal(aesKey.getEncoded()); return Base64.getEncoder().encodeToString(encryptedKey); }
private SecretKey decryptAESKeyWithRSA(String encryptedKey, String privateKey) throws Exception { PrivateKey rsaPrivateKey = keyManagementService.getPrivateKeyFromString(privateKey); Cipher cipher = Cipher.getInstance(properties.getRsaTransformation()); cipher.init(Cipher.DECRYPT_MODE, rsaPrivateKey); byte[] decryptedKey = cipher.doFinal(Base64.getDecoder().decode(encryptedKey)); return new SecretKeySpec(decryptedKey, properties.getAesAlgorithm()); }
private String objectToJson(Object obj) throws Exception { ObjectMapper mapper = new ObjectMapper(); return mapper.writeValueAsString(obj); }
private <T> T jsonToObject(String json, Class<T> clazz) throws Exception { ObjectMapper mapper = new ObjectMapper(); return mapper.readValue(json, clazz); }
private String compressData(String data) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gzos = new GZIPOutputStream(baos); gzos.write(data.getBytes(StandardCharsets.UTF_8)); gzos.close(); byte[] compressed = baos.toByteArray(); return Base64.getEncoder().encodeToString(compressed); }
private String decompressData(String compressedData) throws Exception { byte[] compressed = Base64.getDecoder().decode(compressedData); ByteArrayInputStream bais = new ByteArrayInputStream(compressed); GZIPInputStream gzis = new GZIPInputStream(bais); byte[] buffer = new byte[1024]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); int len; while ((len = gzis.read(buffer)) != -1) { baos.write(buffer, 0, len); } gzis.close(); return baos.toString(StandardCharsets.UTF_8.name()); } }
|