List of usage examples for java.security.spec X509EncodedKeySpec X509EncodedKeySpec
public X509EncodedKeySpec(byte[] encodedKey)
From source file:com.aqnote.shared.cryptology.asymmetric.DSA.java
/** * ?public key/*from w w w . j ava 2 s . co m*/ * * @throws RuntimeException key */ public static PublicKey readPublicKey(byte[] keyBytes) throws RuntimeException { try { KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM); byte[] encodedKey = Base64.decodeBase64(keyBytes); EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey); return keyFactory.generatePublic(keySpec); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeySpecException e) { e.printStackTrace(); } return null; }
From source file:com.ss.license.LicenseManager.java
/** * license//from ww w.j av a2s .co m * Mac * * * @param license * @return * @throws Exception */ boolean validate(License license) throws Exception { String macAddress = license.getMacAddress(); if (macAddress != null && macAddress.length() > 0) { String curMacAddress = ""; if (!macAddress.equals(curMacAddress)) return false; } String publicKey = FileHelper.readFile(new File(LicenseFactory.PUBLIC_KEY_FILE)).trim(); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(EasyUtils.decodeHex(publicKey)); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); java.security.PublicKey pubKey = keyFactory.generatePublic(pubKeySpec); Signature sig = Signature.getInstance("DSA"); sig.initVerify(pubKey); sig.update(license.getFingerprint()); return sig.verify(EasyUtils.decodeHex(license.getLicenseSignature())); }
From source file:org.artifactory.security.crypto.CryptoHelper.java
static KeyPair createKeyPair(byte[] encodedPrivateKey, byte[] encodedPublicKey) { try {// w w w .jav a 2s .c om EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey); KeyFactory generator = KeyFactory.getInstance(ASYM_ALGORITHM); PrivateKey privateKey = generator.generatePrivate(privateKeySpec); EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey); PublicKey publicKey = generator.generatePublic(publicKeySpec); return new KeyPair(publicKey, privateKey); } catch (Exception e) { throw new IllegalArgumentException("Failed to create KeyPair from provided encoded keys", e); } }
From source file:hudson.cli.Connection.java
public X509EncodedKeySpec readKey() throws IOException { byte[] otherHalf = Base64.decodeBase64(readUTF()); // for historical reasons, we don't use readByteArray() return new X509EncodedKeySpec(otherHalf); }
From source file:org.apache.abdera.security.util.KeyHelper.java
public static PublicKey generatePublicKey(String hex) { try {/*from www. j a v a2 s. c o m*/ if (hex == null || hex.trim().length() == 0) return null; byte[] data = Hex.decodeHex(hex.toCharArray()); X509EncodedKeySpec keyspec = new X509EncodedKeySpec(data); KeyFactory keyfactory = KeyFactory.getInstance("RSA"); return keyfactory.generatePublic(keyspec); } catch (Exception e) { return null; } }
From source file:com.vexsoftware.votifier.util.rsa.RSAIO.java
/** * Loads an RSA public key from a URL.//from ww w . j a v a2s. c o m * * @param url * The URL that has the public key * @return * The public key * @throws Exception * If an error occurs */ public static PublicKey loadPublicKey(URL url) throws Exception { String publicKey = new String(IOUtils.toByteArray(url), "UTF-8") .replaceAll("(-+BEGIN PUBLIC KEY-+\\r?\\n|-+END PUBLIC KEY-+\\r?\\n?)", ""); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(DatatypeConverter.parseBase64Binary(publicKey)); return keyFactory.generatePublic(publicKeySpec); }
From source file:net.nicholaswilliams.java.licensing.TestLicenseManager.java
@BeforeClass public static void setUpClass() throws Exception { TestLicenseManager.control = EasyMock.createStrictControl(); TestLicenseManager.licenseProvider = TestLicenseManager.control.createMock(LicenseProvider.class); TestLicenseManager.publicKeyPasswordProvider = TestLicenseManager.control .createMock(PasswordProvider.class); TestLicenseManager.licensePasswordProvider = TestLicenseManager.control.createMock(PasswordProvider.class); TestLicenseManager.keyDataProvider = TestLicenseManager.control.createMock(PublicKeyDataProvider.class); TestLicenseManager.licenseValidator = TestLicenseManager.control.createMock(LicenseValidator.class); try {//from ww w. jav a 2 s . com LicenseManager.getInstance(); fail("Expected java.lang.IllegalArgumentException, got no exception."); } catch (IllegalArgumentException ignore) { } LicenseManagerProperties.setLicenseProvider(TestLicenseManager.licenseProvider); try { LicenseManager.getInstance(); fail("Expected java.lang.IllegalArgumentException, got no exception."); } catch (IllegalArgumentException ignore) { } LicenseManagerProperties.setPublicKeyDataProvider(TestLicenseManager.keyDataProvider); try { LicenseManager.getInstance(); fail("Expected java.lang.IllegalArgumentException, got no exception."); } catch (IllegalArgumentException ignore) { } LicenseManagerProperties.setPublicKeyPasswordProvider(TestLicenseManager.publicKeyPasswordProvider); LicenseManagerProperties.setLicensePasswordProvider(TestLicenseManager.licensePasswordProvider); LicenseManagerProperties.setLicenseValidator(TestLicenseManager.licenseValidator); LicenseManagerProperties.setCacheTimeInMinutes(0); LicenseManager.getInstance(); KeyPair keyPair = KeyPairGenerator.getInstance(KeyFileUtilities.keyAlgorithm).generateKeyPair(); TestLicenseManager.privateKey = keyPair.getPrivate(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyPair.getPublic().getEncoded()); IOUtils.write(Encryptor.encryptRaw(x509EncodedKeySpec.getEncoded(), TestLicenseManager.keyPassword), outputStream); TestLicenseManager.encryptedPublicKey = outputStream.toByteArray(); }
From source file:com.boubei.tss.modules.license.LicenseManager.java
/** * <pre>/*from w w w . j av a2 s.co m*/ * ?license???????? * </pre> * @param license * @return * @throws Exception */ boolean validate(License license) throws Exception { File keyFile = new File(LicenseFactory.PUBLIC_KEY_FILE); String publicKey = FileHelper.readFile(keyFile).trim(); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(EasyUtils.decodeHex(publicKey)); KeyFactory keyFactory = KeyFactory.getInstance(LicenseFactory.KEY_ALGORITHM); java.security.PublicKey pubKey = keyFactory.generatePublic(pubKeySpec); Signature sig = Signature.getInstance(LicenseFactory.KEY_ALGORITHM); sig.initVerify(pubKey); sig.update(license.getFingerprint()); return sig.verify(EasyUtils.decodeHex(license.signature)); }
From source file:com.buzzcoders.security.cryptoutils.asymmetric.AbstractAsymmetricEncryptionModule.java
public PublicKey loadPublicKey(String path) { FileInputStream fis = null;/*from w w w. j a v a2 s. co m*/ try { File filePublicKey = new File(path); fis = new FileInputStream(path); byte[] pubKey = new byte[(int) filePublicKey.length()]; fis.read(pubKey); KeyFactory keyFactory = KeyFactory.getInstance(getAlgorithm()); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(pubKey); PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); return publicKey; } catch (Exception e) { LOG.error("An error occurred while loading the public key from disk.", e); } finally { IOUtils.closeQuietly(fis); } return null; }
From source file:be.e_contract.mycarenet.common.SessionKey.java
/** * Loader constructor. Loads an existing MyCareNet session key. * // w w w . j ava 2 s.c o m * @param encodedPrivateKey * @param encodedPublicKey * @param encodedCertificate * @param notBefore * @param notAfter */ public SessionKey(byte[] encodedPrivateKey, byte[] encodedPublicKey, byte[] encodedCertificate, Date notBefore, Date notAfter) { this.notBefore = notBefore; this.notAfter = notAfter; X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(encodedPublicKey); KeyFactory keyFactory; try { keyFactory = KeyFactory.getInstance("RSA"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("RSA", e); } PublicKey publicKey; try { publicKey = keyFactory.generatePublic(x509EncodedKeySpec); } catch (InvalidKeySpecException e) { throw new RuntimeException("invalid public key: " + e.getMessage(), e); } PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey); PrivateKey privateKey; try { privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); } catch (InvalidKeySpecException e) { throw new RuntimeException("invalid private key: " + e.getMessage(), e); } this.keyPair = new KeyPair(publicKey, privateKey); CertificateFactory certificateFactory; try { certificateFactory = CertificateFactory.getInstance("X.509"); } catch (CertificateException e) { throw new RuntimeException(e); } try { this.certificate = (X509Certificate) certificateFactory .generateCertificate(new ByteArrayInputStream(encodedCertificate)); } catch (CertificateException e) { throw new RuntimeException("certificate decoding error: " + e.getMessage(), e); } }