RSA Public key fingerprinting

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

RSA Public key fingerprinting

Laurent Marot
Hi Guys,

I' m brand new to crypto ans my english is sometimes awkward ... so I
hope you'll forgive me :-)

I'm just trying to fingerprint a public key (read from PEM file)

here is my key :

-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDRFNU++93aEvz3cV8LSUP9ib3i
UxT7SufdVXcgVFK9M3BYzvroA1uO/parFOJABTkNhTPPP/6mjrU2CPEZJ1zIkpaS
NJrrhpp/rNMO9nyLYPGs9MfdBiWUPmHW5mY1oD0ye4my0tEsHOlgHC8AhA8OtiHr
6IY0agXmH/y5YmSWbwIDAQAB
-----END PUBLIC KEY-----


I think fingerprint should return RSA Public Key
[f1:00:c0:ac:b5:7e:42:9c:a8:ba:59:95:d1:85:3e:82:47:84:40:2e]

an in fact I'm getting  [ 0x41 0xD2 0x88 0x58 0xC9 0x0C 0x97 0x16 0x74
0xF4 0xF5 0xA1 0x93 0xA4 0xEA 0xCC 0x04 0xDE 0xCF 0x83 ]

What could be wrong ?

Merci


package org.bouncycastle.x509.examples;

import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemReader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.EncodedKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bouncycastle.crypto.digests.SHA512tDigest;

public class PemUtils {

    private static byte[] parsePEMFile(File pemFile) throws IOException {
        if (!pemFile.isFile() || !pemFile.exists()) {
            throw new FileNotFoundException(String.format("The file '%s'
doesn't exist.", pemFile.getAbsolutePath()));
        }
        PemReader reader = new PemReader(new FileReader(pemFile));
        PemObject pemObject = reader.readPemObject();
        byte[] content = pemObject.getContent();
        reader.close();
        return content;
    }

    private static PublicKey getPublicKey(byte[] keyBytes, String
algorithm) {
        PublicKey publicKey = null;
        try {
            KeyFactory kf = KeyFactory.getInstance(algorithm);
            EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
            publicKey = kf.generatePublic(keySpec);
        } catch (NoSuchAlgorithmException e) {
            System.out.println("Could not reconstruct the public key,
the given algorithm could not be found.");
        } catch (InvalidKeySpecException e) {
            System.out.println("Could not reconstruct the public key");
        }
        return publicKey;
    }

    public static PublicKey readPublicKeyFromFile(String filepath,
String algorithm) throws IOException {
        byte[] bytes = PemUtils.parsePEMFile(new File(filepath));
        return PemUtils.getPublicKey(bytes, algorithm);
    }

    public static byte[] calculateFingerprint(byte[] input) {
            SHA512tDigest digest = new SHA512tDigest(160);
            digest.update(input, 0, input.length);
            byte[] rv = new byte[digest.getDigestSize()];
            digest.doFinal(rv, 0);
            return rv;
        }
   
    public static void main(String[] argv) {
       
        try {
            PublicKey publicKey = readPublicKeyFromFile
("/home/ensibs/Documents/Ressources/tech_Crypto/mypublic.pem","RSA");
            System.out.println(publicKey.toString());
            byte[] entree =  PemUtils.parsePEMFile(new
File("/home/ensibs/Documents/Ressources/tech_Crypto/mypublic.pem"));       
            byte[] sortie =  calculateFingerprint(entree);
                StringBuilder sb = new StringBuilder();
                sb.append("[ ");
                for (byte b : sortie) {
                    sb.append(String.format("0x%02X ", b));
                }
                sb.append("]");
                System.out.println(sb.toString());
        } catch (IOException ex) {
            System.out.println(ex.toString());
            Logger.getLogger(PemUtils.class.getName()).log(Level.SEVERE,
null, ex);
        }
    }
}

Reply | Threaded
Open this post in threaded view
|

RE: RSA Public key fingerprinting

Eckenfels. Bernd
May you want a SHA1 fingerprint? You are using a truncated SHA512 instead.

Gruss
Bene

--
http://www.seeburger.com
________________________________________
From: Laurent Marot [[hidden email]]
Sent: Tuesday, April 07, 2020 20:45
To: [hidden email]
Subject: [dev-crypto] RSA Public key fingerprinting

Hi Guys,

I' m brand new to crypto ans my english is sometimes awkward ... so I
hope you'll forgive me :-)

I'm just trying to fingerprint a public key (read from PEM file)

here is my key :

-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDRFNU++93aEvz3cV8LSUP9ib3i
UxT7SufdVXcgVFK9M3BYzvroA1uO/parFOJABTkNhTPPP/6mjrU2CPEZJ1zIkpaS
NJrrhpp/rNMO9nyLYPGs9MfdBiWUPmHW5mY1oD0ye4my0tEsHOlgHC8AhA8OtiHr
6IY0agXmH/y5YmSWbwIDAQAB
-----END PUBLIC KEY-----


I think fingerprint should return RSA Public Key
[f1:00:c0:ac:b5:7e:42:9c:a8:ba:59:95:d1:85:3e:82:47:84:40:2e]

an in fact I'm getting  [ 0x41 0xD2 0x88 0x58 0xC9 0x0C 0x97 0x16 0x74
0xF4 0xF5 0xA1 0x93 0xA4 0xEA 0xCC 0x04 0xDE 0xCF 0x83 ]

What could be wrong ?

Merci


package org.bouncycastle.x509.examples;

import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemReader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.EncodedKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bouncycastle.crypto.digests.SHA512tDigest;

public class PemUtils {

    private static byte[] parsePEMFile(File pemFile) throws IOException {
        if (!pemFile.isFile() || !pemFile.exists()) {
            throw new FileNotFoundException(String.format("The file '%s'
doesn't exist.", pemFile.getAbsolutePath()));
        }
        PemReader reader = new PemReader(new FileReader(pemFile));
        PemObject pemObject = reader.readPemObject();
        byte[] content = pemObject.getContent();
        reader.close();
        return content;
    }

    private static PublicKey getPublicKey(byte[] keyBytes, String
algorithm) {
        PublicKey publicKey = null;
        try {
            KeyFactory kf = KeyFactory.getInstance(algorithm);
            EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
            publicKey = kf.generatePublic(keySpec);
        } catch (NoSuchAlgorithmException e) {
            System.out.println("Could not reconstruct the public key,
the given algorithm could not be found.");
        } catch (InvalidKeySpecException e) {
            System.out.println("Could not reconstruct the public key");
        }
        return publicKey;
    }

    public static PublicKey readPublicKeyFromFile(String filepath,
String algorithm) throws IOException {
        byte[] bytes = PemUtils.parsePEMFile(new File(filepath));
        return PemUtils.getPublicKey(bytes, algorithm);
    }

    public static byte[] calculateFingerprint(byte[] input) {
            SHA512tDigest digest = new SHA512tDigest(160);
            digest.update(input, 0, input.length);
            byte[] rv = new byte[digest.getDigestSize()];
            digest.doFinal(rv, 0);
            return rv;
        }

    public static void main(String[] argv) {

        try {
            PublicKey publicKey = readPublicKeyFromFile
("/home/ensibs/Documents/Ressources/tech_Crypto/mypublic.pem","RSA");
            System.out.println(publicKey.toString());
            byte[] entree =  PemUtils.parsePEMFile(new
File("/home/ensibs/Documents/Ressources/tech_Crypto/mypublic.pem"));
            byte[] sortie =  calculateFingerprint(entree);
                StringBuilder sb = new StringBuilder();
                sb.append("[ ");
                for (byte b : sortie) {
                    sb.append(String.format("0x%02X ", b));
                }
                sb.append("]");
                System.out.println(sb.toString());
        } catch (IOException ex) {
            System.out.println(ex.toString());
            Logger.getLogger(PemUtils.class.getName()).log(Level.SEVERE,
null, ex);
        }
    }
}









SEEBURGER AG            Vorstand/SEEBURGER Executive Board:
Sitz der Gesellschaft/Registered Office:                Axel Haas, Michael Kleeberg, Axel Otto, Dr. Martin Kuntz, Matthias Feßenbecker
Edisonstr. 1
D-75015 Bretten         Vorsitzende des Aufsichtsrats/Chairperson of the SEEBURGER Supervisory Board:
Tel.: 07252 / 96 - 0            Prof. Dr. Simone Zeuchner
Fax: 07252 / 96 - 2222
Internet: http://www.seeburger.de               Registergericht/Commercial Register:
e-mail: [hidden email]               HRB 240708 Mannheim


Dieses E-Mail ist nur für den Empfänger bestimmt, an den es gerichtet ist und kann vertrauliches bzw. unter das Berufsgeheimnis fallendes Material enthalten. Jegliche darin enthaltene Ansicht oder Meinungsäußerung ist die des Autors und stellt nicht notwendigerweise die Ansicht oder Meinung der SEEBURGER AG dar. Sind Sie nicht der Empfänger, so haben Sie diese E-Mail irrtümlich erhalten und jegliche Verwendung, Veröffentlichung, Weiterleitung, Abschrift oder jeglicher Druck dieser E-Mail ist strengstens untersagt. Weder die SEEBURGER AG noch der Absender (Eckenfels. Bernd) übernehmen die Haftung für Viren; es obliegt Ihrer Verantwortung, die E-Mail und deren Anhänge auf Viren zu prüfen.


This email is intended only for the recipient(s) to whom it is addressed. This email may contain confidential material that may be protected by professional secrecy. Any fact or opinion contained, or expression of the material herein, does not necessarily reflect that of SEEBURGER AG. If you are not the addressee or if you have received this email in error, any use, publication or distribution including forwarding, copying or printing is strictly prohibited. Neither SEEBURGER AG, nor the sender (Eckenfels. Bernd) accept liability for viruses; it is your responsibility to check this email and its attachments for viruses.