|
| 1 | +/* |
| 2 | + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. |
| 3 | + * |
| 4 | + * Copyright (C) 2021 Tencent. All rights reserved. |
| 5 | + * |
| 6 | + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. |
| 7 | + * |
| 8 | + * License for BK-JOB蓝鲸智云作业平台: |
| 9 | + * -------------------------------------------------------------------- |
| 10 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 11 | + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
| 12 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and |
| 13 | + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of |
| 16 | + * the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO |
| 19 | + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 21 | + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 22 | + * IN THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package com.tencent.bk.job.common.crypto; |
| 26 | + |
| 27 | +import com.tencent.bk.sdk.crypto.cryptor.ASymmetricCryptor; |
| 28 | +import com.tencent.bk.sdk.crypto.cryptor.ASymmetricCryptorFactory; |
| 29 | +import lombok.extern.slf4j.Slf4j; |
| 30 | + |
| 31 | +import java.security.PrivateKey; |
| 32 | +import java.security.PublicKey; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.concurrent.ConcurrentHashMap; |
| 35 | + |
| 36 | +/** |
| 37 | + * 非对称加解密服务 |
| 38 | + */ |
| 39 | +@SuppressWarnings("unused") |
| 40 | +@Slf4j |
| 41 | +public class ASymmetricCryptoService { |
| 42 | + |
| 43 | + private final CryptoConfigService cryptoConfigService; |
| 44 | + private final Map<String, ASymmetricCryptor> cryptorMap = new ConcurrentHashMap<>(); |
| 45 | + |
| 46 | + public ASymmetricCryptoService(CryptoConfigService cryptoConfigService) { |
| 47 | + this.cryptoConfigService = cryptoConfigService; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * 对明文信息非对称加密,返回Base64编码后的密文信息 |
| 52 | + * |
| 53 | + * @param publicKey 公钥 |
| 54 | + * @param message 要加密的明文信息,不可为空 |
| 55 | + * @param cryptoScenarioEnum 加密场景 |
| 56 | + * @return Base64编码的加密后的密文信息 |
| 57 | + */ |
| 58 | + public String encrypt(PublicKey publicKey, |
| 59 | + String message, |
| 60 | + CryptoScenarioEnum cryptoScenarioEnum) { |
| 61 | + String algorithm = cryptoConfigService.getSymmetricAlgorithmByScenario(cryptoScenarioEnum); |
| 62 | + ASymmetricCryptor cryptor = cryptorMap.computeIfAbsent(algorithm, ASymmetricCryptorFactory::getCryptor); |
| 63 | + return cryptor.encrypt(publicKey, message); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * 对Base64编码的加密后的密文信息解密,返回解密后的明文 |
| 68 | + * |
| 69 | + * @param privateKey 私钥 |
| 70 | + * @param base64EncryptedMessage Base64编码的加密后的密文信息,不可为空 |
| 71 | + * @param cryptoScenarioEnum 解密场景 |
| 72 | + * @return 解密后的明文信息 |
| 73 | + */ |
| 74 | + public String decrypt(PrivateKey privateKey, |
| 75 | + String base64EncryptedMessage, |
| 76 | + CryptoScenarioEnum cryptoScenarioEnum) { |
| 77 | + String algorithm = cryptoConfigService.getSymmetricAlgorithmByScenario(cryptoScenarioEnum); |
| 78 | + ASymmetricCryptor cryptor = cryptorMap.computeIfAbsent(algorithm, ASymmetricCryptorFactory::getCryptor); |
| 79 | + return cryptor.decrypt(privateKey, base64EncryptedMessage); |
| 80 | + } |
| 81 | +} |
0 commit comments