es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

Cómo descifrar una clave AES cifrada usando una clave privada RSA

Tengo una cadena encriptada (clave AES) que ya está encriptada usando RSA y me gustaría descifrarla usando la Clave Privada que tengo.

He implementado el siguiente código en Java Eclipse:

StringBuilder pkcs8Lines = new StringBuilder();
BufferedReader rdr = new BufferedReader(new StringReader(P_K));
String line;
while ((line = rdr.readLine()) != null) {
    pkcs8Lines.append(line);
}

// Remove the "BEGIN" and "END" lines, as well as any whitespace

String pkcs8Pem = pkcs8Lines.toString();
pkcs8Pem = pkcs8Pem.replace("-----BEGIN PRIVATE KEY-----", "");
pkcs8Pem = pkcs8Pem.replace("-----END PRIVATE KEY-----", "");
pkcs8Pem = pkcs8Pem.replaceAll("\\s+","");

// Base64 decode the result

byte [] pkcs8EncodedBytes = Base64.getDecoder().decode(pkcs8Pem);

// extract the private key

PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pkcs8EncodedBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey privKey = kf.generatePrivate(keySpec);

String encryptedtxt = "AprF9N/CWpReruoPb798Ncwsb5KFYO6BCH0BxeeB47dKXLMd50LSS9f07vpgw4TJRZAKfQxZgUse+laoxa0F8L6EL491ZHu50HBdONmo8iWyX1B7jYdzvlQTHxYHCUQTMqy9sCDEI0ihNqcIZFPQN3NtYyjALZ5pHHmkUvZc5E4tRRBvwSpoL2ilZdsMa6CotHtGmyB+anBXIkoJE5XfpxvOd1HtfcdEh5I+cl8fBXNq+hD3LvjMlIZzpCW5blc4dHIaMuCfdE5EV+BHW9ZK8O+3HMfnONyiFPJ+W5i8YRZ0BbbhPqJJYmyqiZLcFFAs8r4ulSE6Mf9Rcb+TanTnMQ==";

String plainText = null;

final Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] decoded = Base64.getDecoder().decode(encryptedtxt);
byte[] decrypted = cipher.doFinal(decoded);
plainText = new String(decrypted);

System.out.println("Decrypted plaintext: \n\n" + plainText);

Al ejecutar esto, obtuve este texto sin sentido:

4§ˆZ<ïïø?žç,x\?`@fˆhíáxöšï†˜=

intenté herramientas en línea y obtuve otro texto sin sentido:

4��z<��ؐ��,x\�`@f�h���x֚=

¿dónde está el problema? ="" intenté="" herramientas="" en="" línea="" y="" obtuve="" otro="" texto="" sin="" sentido:=""=”” 4��z<��ؐ��,x\�@f�h���x֚="```" ¿dónde="" está="" el=""></ïïø?žç,x\?@fˆhíáxöšï†˜=


intenté herramientas en línea y obtuve otro texto sin sentido:

4��z<��ؐ��,x\�`@f�h���x֚=
“`

¿dónde está el problema?>

Tags:  , , ,

Answer

  1. Avatar for davy.ai

    Es difícil determinar dónde está el problema sin más información, como el texto original sin formato y el resultado descifrado esperado. No obstante, algunas áreas de posible preocupación podrían ser el formato de codificación del texto sin formato original, los algoritmos de cifrado utilizados o cualquier paso adicional de codificación/descodificación aplicado a la cadena cifrada. Puede ser útil comprobar el valor clave y los parámetros de cifrado para asegurarse de que coincidan y tratar de depurar el proceso de descifrado paso a paso para identificar cualquier error.

Comments are closed.