Спецификации сгенерированного мной ключа выглядят следующим образом -< /p>
введите здесь описание изображения
Код: Выделить всё
Key type:Asymmetric
Origin:AWS KMS
Key spec:RSA_2048
Key usage:Encrypt and decrypt
Encryption algorithms:RSAES_OAEP_SHA_1,RSAES_OAEP_SHA_256
Код: Выделить всё
// Convert a Base64-encoded public key string into a PublicKey object
public static PublicKey getPublicKeyFromString(String base64PublicKey) throws Exception {
byte[] keyBytes = Base64.getDecoder().decode(base64PublicKey);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePublic(keySpec);
}
// Encrypt the plaintext using the public key
public static String encryptData(PublicKey publicKey, String plaintext) throws Exception {
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
OAEPParameterSpec oaepParams = new OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT);
cipher.init(Cipher.ENCRYPT_MODE, publicKey, oaepParams);
byte[] ciphertext = cipher.doFinal(plaintext.getBytes());
return Base64.getEncoder().encodeToString(ciphertext);
}
public static void main(String[] args) throws Exception {
String base64PublicKey = "my_public_key";
PublicKey publicKey = getPublicKeyFromString(base64PublicKey);
String plaintext = "Sensitive Data";
String encryptedData = encryptData(publicKey, plaintext);
System.out.println(encryptedData);
}
Но проблема в том, что когда я пытаюсь расшифровать его с помощью службы aws kms, это бросок
Код: Выделить всё
InvalidCiphertextException('An error occurred (InvalidCiphertextException) when calling the Decrypt operation: ')
Код: Выделить всё
def decrypt_kms(cipher_text: str, key_id: str) -> str:
session = boto3.session.Session()
kms_client = session.client(service_name="kms", region_name=AWS_REGION_NAME)
try:
encrypted_data = base64.b64decode(cipher_text)
response = kms_client.decrypt(
CiphertextBlob=encrypted_data,
KeyId=key_id,
EncryptionAlgorithm="RSAES_OAEP_SHA_256",
)
# The decrypted plaintext (as bytes)
decrypted_data = response["Plaintext"]
# Convert decrypted data to string (assuming UTF-8 encoding)
return decrypted_data.decode("utf-8")
except (BotoCoreError, ClientError) as error:
rep = repr(error)
logger.error(rep)
return None
Шифрование на стороне Java работает нормально и генерирует вывод зашифрованного текста в формате base64. Но сгенерированный вывод не может быть успешно зашифрован с помощью службы kms на стороне Python.
Когда я пытаюсь расшифровать его с помощью службы aws kms на стороне Python, он выдает:
Код: Выделить всё
InvalidCiphertextException('An error occurred (InvalidCiphertextException) when calling the Decrypt operation: ')
Изменить: я не могу использовать awssdk, потому что служба, в которую я добавляю часть шифрования, делает это. не разрешать добавление внешних зависимостей.
Подробнее здесь: https://stackoverflow.com/questions/790 ... out-awssdk