private void populatePublicKey(PublicKey csrPublicKey) throws CryptoException { KeyInfo keyInfo = KeyPairUtil.getKeyInfo(csrPublicKey); jtfPublicKey.setText(keyInfo.getAlgorithm()); Integer keySize = keyInfo.getSize(); if (keySize != null) { jtfPublicKey.setText(MessageFormat.format(res.getString("DViewCsr.jtfPublicKey.text"), jtfPublicKey.getText(), "" + keySize)); } else { jtfPublicKey.setText(MessageFormat.format(res.getString("DViewCsr.jtfPublicKey.text"), jtfPublicKey.getText(), "?")); } jtfPublicKey.setCaretPosition(0); }
/** * Get the information about the supplied secret key. * * @param secretKey * The secret key * @return Key information */ public static KeyInfo getKeyInfo(SecretKey secretKey) { String algorithm = secretKey.getAlgorithm(); if (algorithm.equals("RC4")) { algorithm = "ARC4"; // RC4 is trademarked so we never want to display it } if (secretKey.getFormat().equals("RAW")) { int keySize = secretKey.getEncoded().length * 8; return new KeyInfo(SYMMETRIC, algorithm, keySize); } else { // Key size unknown return new KeyInfo(SYMMETRIC, algorithm); } } }
private String getAlgorithmName(KeyInfo keyInfo) { String algorithm = keyInfo.getAlgorithm(); if (keyInfo.getKeyType() == SYMMETRIC) { // Try and get friendly algorithm name for secret key SecretKeyType secretKeyType = SecretKeyType.resolveJce(algorithm); if (secretKeyType != null) { algorithm = secretKeyType.friendly(); } } return algorithm; }
data[i][iKeySizeColumn] = keyInfo.getSize(); data[i][iCurveColumn] = keyInfo.getDetailedAlgorithm(); if (iColWidth[iCurveColumn] < data[i][iCurveColumn].toString().length()) { iColWidth[iCurveColumn] = data[i][iCurveColumn].toString().length();
case RSA: KeyInfo keyInfo = KeyPairUtil.getKeyInfo(privateKey); sigAlgs = SignatureType.rsaSignatureTypes(keyInfo.getSize()); break; case DSA:
private void populatePublicKey() throws CryptoException { KeyInfo keyInfo = KeyPairUtil.getKeyInfo(csrPublicKey); jtfCsrPublicKey.setText(keyInfo.getAlgorithm()); Integer keySize = keyInfo.getSize(); if (keySize != null) { jtfCsrPublicKey.setText(MessageFormat.format(res.getString("DSignCsr.jtfCsrPublicKey.text"), jtfCsrPublicKey.getText(), "" + keySize)); } else { jtfCsrPublicKey.setText(MessageFormat.format(res.getString("DSignCsr.jtfCsrPublicKey.text"), jtfCsrPublicKey.getText(), "?")); } jtfCsrPublicKey.setCaretPosition(0); }
BigInteger exponent = keySpec.getPublicExponent(); if (exponent.intValue() < 0x10001) { return new KeyInfo(ASYMMETRIC, algorithm, modulus.toString(2).length(), algorithm.toUpperCase() + modulus.toString(2).length()); } else { return new KeyInfo(ASYMMETRIC, algorithm, modulus.toString(2).length(), algorithm.toLowerCase() + modulus.toString(2).length()); DSAPublicKeySpec keySpec = keyFact.getKeySpec(publicKey, DSAPublicKeySpec.class); BigInteger prime = keySpec.getP(); return new KeyInfo(ASYMMETRIC, algorithm, prime.toString(2).length(), algorithm.toUpperCase() + prime.toString(2).length()); } else if (algorithm.equals(EC.jce())) { int size = spec.getOrder().bitLength(); if (spec instanceof ECNamedCurveSpec) { return new KeyInfo(ASYMMETRIC, algorithm, size, ((ECNamedCurveSpec) spec).getName()); } else { return new KeyInfo(ASYMMETRIC, algorithm, size); return new KeyInfo(ASYMMETRIC, algorithm); // size unknown } catch (GeneralSecurityException ex) { throw new CryptoException(res.getString("NoPublicKeysize.exception.message"), ex);
private void populateDialog() throws CryptoException { KeyInfo keyInfo = KeyPairUtil.getKeyInfo(publicKey); jtfAlgorithm.setText(keyInfo.getAlgorithm()); Integer keyLength = keyInfo.getSize(); if (keyLength != null) { jtfKeySize.setText(MessageFormat.format(res.getString("DViewPublicKey.jtfKeySize.text"), "" + keyLength)); } else { jtfKeySize.setText(MessageFormat.format(res.getString("DViewPublicKey.jtfKeySize.text"), "?")); } jtfFormat.setText(publicKey.getFormat()); jtaEncoded.setText(new BigInteger(1, publicKey.getEncoded()).toString(16).toUpperCase()); jtaEncoded.setCaretPosition(0); if ((publicKey instanceof RSAPublicKey) || (publicKey instanceof DSAPublicKey)) { jbFields.setEnabled(true); } else { jbFields.setEnabled(false); } }
RSAPrivateKeySpec keySpec = keyFact.getKeySpec(privateKey, RSAPrivateKeySpec.class); BigInteger modulus = keySpec.getModulus(); return new KeyInfo(ASYMMETRIC, algorithm, modulus.toString(2).length()); } else { return new KeyInfo(ASYMMETRIC, algorithm, 0); DSAPrivateKeySpec keySpec = keyFact.getKeySpec(privateKey, DSAPrivateKeySpec.class); BigInteger prime = keySpec.getP(); return new KeyInfo(ASYMMETRIC, algorithm, prime.toString(2).length()); } else if (algorithm.equals(EC.jce()) || algorithm.equals(ECDSA.jce())) { ECPrivateKey pubk = (ECPrivateKey) privateKey; int size = spec.getOrder().bitLength(); if (spec instanceof ECNamedCurveSpec) { return new KeyInfo(ASYMMETRIC, algorithm, size, ((ECNamedCurveSpec) spec).getName()); } else { return new KeyInfo(ASYMMETRIC, algorithm, size); return new KeyInfo(ASYMMETRIC, algorithm); // size unknown } catch (GeneralSecurityException ex) { throw new CryptoException(res.getString("NoPrivateKeysize.exception.message"), ex);
private void populateDialog() throws CryptoException { KeyInfo keyInfo = KeyPairUtil.getKeyInfo(privateKey); jtfAlgorithm.setText(keyInfo.getAlgorithm()); Integer keyLength = keyInfo.getSize(); if (keyLength != null) { jtfKeySize.setText(MessageFormat.format(res.getString("DViewPrivateKey.jtfKeySize.text"), "" + keyLength)); } else { jtfKeySize.setText(MessageFormat.format(res.getString("DViewPrivateKey.jtfKeySize.text"), "?")); } jtfFormat.setText(privateKey.getFormat()); jtaEncoded.setText(new BigInteger(1, privateKey.getEncoded()).toString(16).toUpperCase()); jtaEncoded.setCaretPosition(0); if ((privateKey instanceof RSAPrivateKey) || (privateKey instanceof DSAPrivateKey)) { jbFields.setEnabled(true); } else { jbFields.setEnabled(false); } }
private void populateDialog() { KeyInfo keyInfo = SecretKeyUtil.getKeyInfo(secretKey); String algorithm = keyInfo.getAlgorithm(); // Try and get friendly algorithm name SecretKeyType secretKeyType = SecretKeyType.resolveJce(algorithm); if (secretKeyType != null) { algorithm = secretKeyType.friendly(); } jtfAlgorithm.setText(algorithm); Integer keyLength = keyInfo.getSize(); if (keyLength != null) { jtfKeySize.setText(MessageFormat.format(res.getString("DViewSecretKey.jtfKeySize.text"), "" + keyLength)); } else { jtfKeySize.setText(MessageFormat.format(res.getString("DViewSecretKey.jtfKeySize.text"), "?")); } jtfFormat.setText(secretKey.getFormat()); jtaEncoded.setText(new BigInteger(1, secretKey.getEncoded()).toString(16).toUpperCase()); jtaEncoded.setCaretPosition(0); }
private void createSecretKeyNodes(DefaultMutableTreeNode parentNode, SecretKey secretKey) { DefaultMutableTreeNode secretKeyNode = new DefaultMutableTreeNode( res.getString("DProperties.properties.SecretKey")); parentNode.add(secretKeyNode); KeyInfo keyInfo = SecretKeyUtil.getKeyInfo(secretKey); String keyAlg = keyInfo.getAlgorithm(); // Try and get friendly algorithm name for secret key SecretKeyType secretKeyType = SecretKeyType.resolveJce(keyAlg); if (secretKeyType != null) { keyAlg = secretKeyType.friendly(); } secretKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format( res.getString("DProperties.properties.Algorithm"), keyAlg))); Integer keySize = keyInfo.getSize(); if (keySize != null) { secretKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format( res.getString("DProperties.properties.KeySize"), "" + keyInfo.getSize()))); } else { secretKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format( res.getString("DProperties.properties.KeySize"), "?"))); } String keyFormat = secretKey.getFormat(); secretKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format( res.getString("DProperties.properties.Format"), keyFormat))); String keyEncoded = "0x" + new BigInteger(1, secretKey.getEncoded()).toString(16).toUpperCase(); secretKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format( res.getString("DProperties.properties.Encoded"), keyEncoded))); }
String keyAlg = keyInfo.getAlgorithm(); Integer keySize = keyInfo.getSize(); res.getString("DProperties.properties.KeySize"), "" + keyInfo.getSize()))); } else { publicKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format(
String keyAlg = keyInfo.getAlgorithm(); Integer keySize = keyInfo.getSize(); res.getString("DProperties.properties.KeySize"), "" + keyInfo.getSize()))); } else { privateKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format(
@Test private void testSecretKeyType(SecretKeyType secretKeyType) throws CryptoException { for (int keySize = secretKeyType.minSize(); keySize <= secretKeyType.maxSize(); keySize += secretKeyType .stepSize()) { SecretKey secretKey = SecretKeyUtil.generateSecretKey(secretKeyType, keySize); KeyInfo keyInfo = SecretKeyUtil.getKeyInfo(secretKey); assertThat(secretKeyType.jce()).isEqualToIgnoringCase(keyInfo.getAlgorithm()); assertThat(secretKeyType).isEqualTo(SecretKeyType.resolveJce(keyInfo.getAlgorithm())); assertThat(keySize).isEqualTo(keyInfo.getSize().intValue()); } } }
jtfPublicKey.setText(keyInfo.getAlgorithm()); Integer keySize = keyInfo.getSize();
@ParameterizedTest @CsvSource({ "DSA, 512", "DSA, 1024", "RSA, 512", "RSA, 1024", "RSA, 2048", //"RSA, 3072", takes too long //"RSA, 4096", takes too long }) public void generateRsaDsaKeys(KeyPairType keyPairType, Integer keySize) throws Exception { KeyPair keyPair = KeyPairUtil.generateKeyPair(keyPairType, keySize, BC); PrivateKey privateKey = keyPair.getPrivate(); KeyInfo privateKeyInfo = KeyPairUtil.getKeyInfo(privateKey); assertEquals(keyPairType.toString(), privateKeyInfo.getAlgorithm()); assertEquals(keySize, privateKeyInfo.getSize()); PublicKey publicKey = keyPair.getPublic(); KeyInfo publicKeyInfo = KeyPairUtil.getKeyInfo(publicKey); assertEquals(keyPairType.toString(), publicKeyInfo.getAlgorithm()); assertEquals(keySize, publicKeyInfo.getSize()); assertTrue(KeyPairUtil.validKeyPair(privateKey, publicKey)); }