React Native
React Native
Get RSA Key Modulus from .cer or .key
See more Certificates Examples
Demonstrates how to get the RSA key modulus from either the certificate (.cer) or RSA key (.key). OpenSSL commands to do the same would be:openssl x509 -inform DER -in "test.cer" -modulus -nooutor
openssl pkcs8 -inform DER -inβ "test.key"β -outform PEM -passin pass:"12345β678aβ" | openssl rsa -inform PEM -modulus -noout
Chilkat React Native Downloads
import { BinData, Cert, PrivateKey, PublicKey, Xml } from '@chilkat/react-native'
function chilkatExample() {
const privKey = new PrivateKey();
const password = '12345678a';
try {
privKey.loadPkcs8EncryptedFile('qa_data/certs/test_12345678a.key', password);
} catch {
console.log(privKey.lastErrorText);
return;
}
const xml = new Xml();
xml.loadXml(privKey.getXml());
// The XML contains the parts of the key in base64.
console.log('Private Key XML:');
console.log(xml.getXml());
// We can get the base64 modulus like this:
let modulus = xml.getChildContent('Modulus');
console.log(`base64 modulus = ${modulus}`);
// To convert to hex:
const binDat = new BinData();
binDat.appendEncoded(modulus, 'base64');
let hexModulus = binDat.getEncoded('hex');
console.log(`hex modulus = ${hexModulus}`);
// Now get the modulus from the cert:
const cert = new Cert();
try {
cert.loadFromFile('qa_data/certs/test_12345678a.cer');
} catch {
console.log(cert.lastErrorText);
return;
}
// The cert contains the public key, which is composed of the
// modulus + exponent (for RSA keys).
const pubKey = new PublicKey();
cert.getPublicKey(pubKey);
xml.loadXml(pubKey.getXml());
console.log('Public Key XML:');
console.log(xml.getXml());
// Proceed in the same way as before....
modulus = xml.getChildContent('Modulus');
console.log(`base64 modulus = ${modulus}`);
// To convert to hex:
binDat.clear();
binDat.appendEncoded(modulus, 'base64');
hexModulus = binDat.getEncoded('hex');
console.log(`hex modulus = ${hexModulus}`);
}