Score:1

Why the Modulus and Exponent of the public key and the private key are the same?

aw flag

Given a certificate .p12 I want to extract the public key, the private key and Modulus and Exponent both from the public key and from the private key.

I am using PHP and OpenSSL functions

As I understand it, the Module and Exponent of the public key must be different from the Module and Exponent of the private key.

            // Obtenemos el certificado .p12
            if (!$pfx = file_get_contents(UtilController::getPathDelCertificado())){
                UtilController::registrarLog('ERR - No se puede leer el fichero del certificado o no existe en la ruta especificada: '.UtilController::getPathDelCertificado().' - [Firmador->firmar()]');
                exit;
            }
            
            // Lee el archivo PKCS12.
            //      key:        En esta variable se guardará la clave privada extraida del certificado
            //      password:   Es la constraseña que tiene el certificado
            if (!openssl_pkcs12_read($pfx, $key, UtilController::PASSWORD_CERTIFICADO)){
                UtilController::registrarLog('ERR - No se puede leer el almacén de certificados o la clave no es la correcta - [Firmador->firmar()]');
                exit;
            }

            $publicKey = openssl_x509_read($key["cert"]);
            $aInfoPublicKey = openssl_pkey_get_details(openssl_pkey_get_public($publicKey));

            // Obtenemos informacion detallada sobre la clave privada. Es necesario pasarle como parametro un resource a openssl_pkey_get_details
            $aInfoPrivateKey = openssl_pkey_get_details(openssl_pkey_get_private($key["pkey"]));

            $this->certificate  = $key["cert"]; 
            $this->publicKey    = $aInfoPublicKey["key"];       
            $this->moduloDeLaPublicKey = base64_encode($aInfoPublicKey['rsa']['n']);
            $this->exponenteDeLaPublicKey = base64_encode($aInfoPublicKey['rsa']['e']); 
            $this->privateKey   = $key["pkey"];         // Clave privada del certificado en formato .pem
            $this->moduloDeLaPrivateKey = base64_encode($aInfoPrivateKey['rsa']['n']);
            $this->exponenteDeLaPrivateKey = base64_encode($aInfoPrivateKey['rsa']['e']);

However, when I print the variables: $this->moduloDeLaPrivateKey and $this->moduloDeLaPublicKey and $this->exponentDeLaPublicKey and $this->exponentDeLaPrivateKey.

Why is this happening?

Score:3
ng flag

the Module and Exponent of the public key must be different from the Module and Exponent of the private key.

Most commonly, public key is $(n,e)$, and private key is either $(n,d)$ or $(n,e,d,p,q,d_p,d_q,q_\text{inv})$. See PKCS#1v2.2 for details in the formatting, and rarely used forms for multiprime RSA.

The public and private keys of the same key pair must share the same public modulus $n$.

And if a private key includes the public exponent $e$ (not all private keys do), then the public and private keys must share the same public exponent $e$. This is what happens in the question.

The public exponent $e$ and the private exponent $d$ can be identical too, but only for pathological and insecure key pairs.

cobbal avatar
gs flag
e=d for when you need military-grade ROT13 encryption
Score:1
ch flag

As I understand it, the Module and Exponent of the public key must be different from the Module and Exponent of the private key.

I don't think your understanding is correct. The modulus is (and must be) the same, in both certificat files. (the modulus is part of the public key). The exponent is the other part of the public key, and you expect to have the same exponent (if included) in both the public and the private components.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.