use of java.security.interfaces.RSAPublicKey in project remusic by aa112901.
the class RSAUtils method getPublicKey.
/**
* 使用模和指数生成RSA公钥
* 注意:【此代码用了默认补位方式,为RSA/None/PKCS1Padding,不同JDK默认的补位方式可能不同,如Android默认是RSA
* /None/NoPadding】
*
* @param modulus 模
* @param exponent 指数
* @return
*/
public static RSAPublicKey getPublicKey(String modulus, String exponent) {
try {
BigInteger b1 = new BigInteger(modulus);
BigInteger b2 = new BigInteger(exponent);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(b1, b2);
return (RSAPublicKey) keyFactory.generatePublic(keySpec);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
use of java.security.interfaces.RSAPublicKey in project javautils by jiadongpo.
the class RSAUtils method initKey.
/**
* 初始化密钥
* @return
* @throws Exception
*/
public static Map<String, Object> initKey() throws Exception {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] encodedKey = input2byte(RSAUtils.class.getResourceAsStream("private_key.pem"));
RSAPrivateKey privateKey = (RSAPrivateKey) keyFactory.generatePrivate(new PKCS8EncodedKeySpec(Base64Utils.decode(new String(encodedKey))));
// byte[] encodedKey2 = input2byte(RSAUtils.class.getResourceAsStream("rsa_public_key.pem"));
// KeySpec keySpec = new X509EncodedKeySpec(Base64Utils.decode(new String(encodedKey2)));
// RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(keySpec);
CertificateFactory certificatefactory = CertificateFactory.getInstance("X.509");
InputStream input = RSAUtils.class.getResourceAsStream("rsa_public_key.pem.crt");
X509Certificate Cert = (X509Certificate) certificatefactory.generateCertificate(input);
RSAPublicKey publicKey = (RSAPublicKey) Cert.getPublicKey();
// KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORTHM);
// keyPairGenerator.initialize(1024);
// KeyPair keyPair = keyPairGenerator.generateKeyPair();
// 公钥
// RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
// 私钥
// RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
Map<String, Object> keyMap = new HashMap<String, Object>(2);
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);
return keyMap;
}
use of java.security.interfaces.RSAPublicKey in project keystore-explorer by kaikramer.
the class DProperties method createPublicKeyNodes.
private void createPublicKeyNodes(DefaultMutableTreeNode parentNode, PublicKey publicKey) throws CryptoException {
DefaultMutableTreeNode publicKeyNode = new DefaultMutableTreeNode(res.getString("DProperties.properties.PublicKey"));
parentNode.add(publicKeyNode);
KeyInfo keyInfo = KeyPairUtil.getKeyInfo(publicKey);
String keyAlg = keyInfo.getAlgorithm();
publicKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format(res.getString("DProperties.properties.Algorithm"), keyAlg)));
Integer keySize = keyInfo.getSize();
if (keySize != null) {
publicKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format(res.getString("DProperties.properties.KeySize"), "" + keyInfo.getSize())));
} else {
publicKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format(res.getString("DProperties.properties.KeySize"), "?")));
}
String keyFormat = publicKey.getFormat();
publicKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format(res.getString("DProperties.properties.Format"), keyFormat)));
String keyEncoded = "0x" + new BigInteger(1, publicKey.getEncoded()).toString(16).toUpperCase();
publicKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format(res.getString("DProperties.properties.Encoded"), keyEncoded)));
if (publicKey instanceof RSAPublicKey) {
RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
String publicExponent = MessageFormat.format(res.getString("DProperties.properties.public.rsa.PublicExponent"), "0x" + rsaPublicKey.getPublicExponent().toString(16).toUpperCase());
publicKeyNode.add(new DefaultMutableTreeNode(publicExponent));
String modulus = MessageFormat.format(res.getString("DProperties.properties.public.rsa.Modulus"), "0x" + rsaPublicKey.getModulus().toString(16).toUpperCase());
publicKeyNode.add(new DefaultMutableTreeNode(modulus));
} else if (publicKey instanceof DSAPublicKey) {
DSAPublicKey dsaPublicKey = (DSAPublicKey) publicKey;
DSAParams dsaParams = dsaPublicKey.getParams();
String primeModulusP = MessageFormat.format(res.getString("DProperties.properties.public.dsa.PrimeModulusP"), "0x" + dsaParams.getP().toString(16).toUpperCase());
publicKeyNode.add(new DefaultMutableTreeNode(primeModulusP));
String primeQ = MessageFormat.format(res.getString("DProperties.properties.public.dsa.PrimeQ"), "0x" + dsaParams.getQ().toString(16).toUpperCase());
publicKeyNode.add(new DefaultMutableTreeNode(primeQ));
String generatorG = MessageFormat.format(res.getString("DProperties.properties.public.dsa.GeneratorG"), "0x" + dsaParams.getG().toString(16).toUpperCase());
publicKeyNode.add(new DefaultMutableTreeNode(generatorG));
String publicKeyY = MessageFormat.format(res.getString("DProperties.properties.public.dsa.PublicKeyY"), "0x" + dsaPublicKey.getY().toString(16).toUpperCase());
publicKeyNode.add(new DefaultMutableTreeNode(publicKeyY));
}
}
use of java.security.interfaces.RSAPublicKey in project keystore-explorer by kaikramer.
the class DViewAsymmetricKeyFields method populateFields.
private void populateFields() {
Field[] fields = null;
if (key instanceof RSAPublicKey) {
RSAPublicKey rsaPub = (RSAPublicKey) key;
fields = new Field[] { new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PubRsaPublicExponent.text"), rsaPub.getPublicExponent()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PubRsaModulus.text"), rsaPub.getModulus()) };
} else if (key instanceof DSAPublicKey) {
DSAPublicKey dsaPub = (DSAPublicKey) key;
DSAParams dsaParams = dsaPub.getParams();
fields = new Field[] { new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PubDsaPrimeModulusP.text"), dsaParams.getP()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PubDsaPrimeQ.text"), dsaParams.getQ()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PubDsaGeneratorG.text"), dsaParams.getG()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PubDsaPublicKeyY.text"), dsaPub.getY()) };
} else if (key instanceof RSAPrivateCrtKey) {
RSAPrivateCrtKey rsaPvk = (RSAPrivateCrtKey) key;
fields = new Field[] { new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivRsaPublicExponent.text"), rsaPvk.getPublicExponent()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivRsaModulus.text"), rsaPvk.getModulus()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivRsaPrimeP.text"), rsaPvk.getPrimeP()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivRsaPrimeQ.text"), rsaPvk.getPrimeQ()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivRsaPrimeExponentP.text"), rsaPvk.getPrimeExponentP()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivRsaPrimeExponentQ.text"), rsaPvk.getPrimeExponentQ()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivRsaCrtCoefficient.text"), rsaPvk.getCrtCoefficient()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivRsaPrivateExponent.text"), rsaPvk.getPrivateExponent()) };
} else if (key instanceof RSAPrivateKey) {
RSAPrivateKey rsaPvk = (RSAPrivateKey) key;
fields = new Field[] { new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivRsaModulus.text"), rsaPvk.getModulus()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivRsaPrivateExponent.text"), rsaPvk.getPrivateExponent()) };
} else if (key instanceof DSAPrivateKey) {
DSAPrivateKey dsaPvk = (DSAPrivateKey) key;
DSAParams dsaParams = dsaPvk.getParams();
fields = new Field[] { new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivDsaPrimeModulusP.text"), dsaParams.getP()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivDsaPrimeQ.text"), dsaParams.getQ()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivDsaGeneratorG.text"), dsaParams.getG()), new Field(res.getString("DViewAsymmetricKeyFields.jltFields.PrivDsaSecretExponentX.text"), dsaPvk.getX()) };
}
if (fields != null) {
jltFields.setListData(fields);
jltFields.setSelectedIndex(0);
}
}
use of java.security.interfaces.RSAPublicKey in project keystore-explorer by kaikramer.
the class DViewPublicKey method populateDialog.
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());
jtfEncoded.setText("0x" + new BigInteger(1, publicKey.getEncoded()).toString(16).toUpperCase());
jtfEncoded.setCaretPosition(0);
if ((publicKey instanceof RSAPublicKey) || (publicKey instanceof DSAPublicKey)) {
jbFields.setEnabled(true);
} else {
jbFields.setEnabled(false);
}
}
Aggregations