Pregunta

Quiero enviar un mensaje original entre dos personas. dejemos decir alicia y bob, y Quiero saber si estos pasos son correctos para la verificación de la firma o no

  1. Alice Hash The Original_Message con su horario privado -> H (M)
  2. Alice cifrado el mensaje hashed -> C (h (m))
  3. Alice firma el mensaje con su LISTRICKEY -> S (C (H (M)))
  4. Alice Envíe el mensaje Final firmado con ella (PUBLICKEY) y (The Original_Message) a Bob. en Bob Side:

    1. bob hash el original_message -> h (m)
    2. Bob Descifre el mensaje firmado con la clave pública de Alice -> D (S (C (H (M))))
    3. Bob Comprobar mensaje descifrado con mensaje hashed si son iguales o no? if (h (m)== d (s (c (h (m)))))
    4. Sé que estoy haciendo un error. ¿Alguien sabe cuál es el buen orden en ambos lados?

      Aquí usé Java.Security por hacer esto, pero cuando reviso los hashes en el paso final, ¡me da FALSO!

      en Alice Pieza:

      public byte[] Sign(byte[] aMessage) {
      
              try {
                  // get an instance of a cipher with RSA with ENCRYPT_MODE
                  // Init the signature with the private key
                  // Compute signature
                  Cipher cipher = Cipher.getInstance("RSA");
                  cipher.init(Cipher.ENCRYPT_MODE, thePrivateKey);
      
                  Signature instance = Signature.getInstance("MD5withRSA");
                  instance.initSign(thePrivateKey);
      
                  // get an instance of the java.security.MessageDigest with MD5
                  // process the digest
                  MessageDigest md5_digest = MessageDigest.getInstance("MD5");
                  md5_digest.update(aMessage);
                  byte[] digest = md5_digest.digest();
      
                  // return the encrypted digest
                  byte[] cipherText = cipher.doFinal(digest);
      
                  instance.update(cipherText);            
                  byte[] signedMSG = instance.sign();
      
                  return signedMSG;
      
              } catch (Exception e) {
                  System.out.println("Signature error");
                  e.printStackTrace();
                  return null;
              }
      
          }
      

      en la parte de Bob:

      public boolean CheckSignature(byte[] aMessage, byte[] aSignature,
                  PublicKey aPK) {
              try {
                  // get an instance of a cipher with RSA with ENCRYPT_MODE
                  // Init the signature with the private key
                  // decrypt the signature
                  Cipher cipher = Cipher.getInstance("RSA");
                  cipher.init(Cipher.DECRYPT_MODE, aPK);
                  byte[] decrypted_digest =  cipher.doFinal(aSignature);
      
                  // get an instance of the java.security.MessageDigest with MD5
                  MessageDigest md5_digest = MessageDigest.getInstance("MD5");
      
                  // process the digest
                  md5_digest.update(aMessage);
                  byte[] digest = md5_digest.digest();
      
                  // check if digest1 == digest2
                  if (decrypted_digest == digest) {
                      return true;
                  }else {
                      return false;
                  }
      
              } catch (Exception e) {
                  System.out.println("Verify signature error");
                  e.printStackTrace();
                  return false;
              }
          }
      

¿Fue útil?

Solución

Finalmente encontré la respuesta.El error fue, haciendo un signo () en la parte de Alice.Debido a que cuando está haciendo el hashing y el cifrado, ya se convirtió en una firma, y cuando firmes (), otra vez, en la parte de Bob, se hizo imposible recuperar la firma hashed.

Mi código también es un buen ejemplo de "Hashing de un solo sentido en el cifrado de la clave pública de seguridad Java"

Esta es la modificación en la parte de Alice y todo funciona después.

public byte[] Sign(byte[] aMessage) {

        try {
            // get an instance of a cipher with RSA with ENCRYPT_MODE
            // Init the signature with the private key
            // Compute signature
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, thePrivateKey);

            // get an instance of the java.security.MessageDigest with MD5
            // process the digest
            MessageDigest md5_digest = MessageDigest.getInstance("MD5");
            byte[] digest = md5_digest.digest(aMessage);

            // return the encrypted digest
            byte[] cipherText = cipher.doFinal(digest);

            return cipherText;

        } catch (Exception e) {
            System.out.println("Signature error");
            e.printStackTrace();
            return null;
        }

    }

Otros consejos

La firma digital es la cifra cifrada (con clave privada) del hash del mensaje.

Signature S= C (H (M))

ahora está unido al mensaje m. El mensaje firmado para ser transmitido desde Alice a Bob es M + S

Después de recibir M + S en el lado de Bob, Bob descifraría la firma con la clave pública de Alice, que estaría presente en el certificado. Así que aquí hace D (S)= D (C (H (M))= H (M)

También el Bob recibió el mensaje para que calcule el hash del mensaje M, que es H (M)

Ahora comparara las salidas de los pasos anteriores para ver si coinciden. Esto asegura que el mensaje no haya sido manipulado por nadie en el medio.

Esta es la idea general de cómo funcionan las firmas digitales. Espero que esto ayude.

Wikipedia tiene una representación gráfica del mismo proceso aquí: http://upload.wikimedia.org/wikipedia/commons/2/2b /Digital_signature_diagram.svg

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top