Example usage for java.security.spec X509EncodedKeySpec X509EncodedKeySpec

List of usage examples for java.security.spec X509EncodedKeySpec X509EncodedKeySpec

Introduction

In this page you can find the example usage for java.security.spec X509EncodedKeySpec X509EncodedKeySpec.

Prototype

public X509EncodedKeySpec(byte[] encodedKey) 

Source Link

Document

Creates a new X509EncodedKeySpec with the given encoded key.

Usage

From source file:org.apache.abdera2.common.security.DHBase.java

private Key decode(byte[] key) throws NoSuchAlgorithmException, InvalidKeySpecException {
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
    KeyFactory keyFact = KeyFactory.getInstance("DH");
    return keyFact.generatePublic(x509KeySpec);
}

From source file:com.jinhe.tss.core.common.license.LicenseManager.java

/**
 * ?license??/* w  ww  .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 = MacAddressUtil.getMacAddress();
        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:pepperim.util.IMCrypt.java

/**
 * @param b64str Base64-encoded public key
 * @return PublicKey object// w  w  w . j  a va2s.c  om
 */
public static PublicKey decodePublicKey(String b64str) {
    try {
        byte[] keydata = B64_Dec(b64str);
        X509EncodedKeySpec ks = new X509EncodedKeySpec(keydata);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PublicKey pk = kf.generatePublic(ks);
        return pk;
    } catch (GeneralSecurityException e) {
        Main.log(e.getMessage());
        return null;
    }
}

From source file:tv.ouya.sdk.TestOuyaFacade.java

public TestOuyaFacade(Context context, Bundle savedInstanceState, String developerId, byte[] applicationKey) {
    try {/*from w  w  w. j  av  a  2 s. co m*/
        this.context = context;

        Log.i("TestOuyaFacade", "TestOuyaFacade.Init(" + developerId + ");");
        UnityPlayer.UnitySendMessage("OuyaGameObject", "DebugLog", "TestOuyaFacade.Init(" + developerId + ");");

        ouyaFacade = OuyaFacade.getInstance();

        Init(developerId);

        // Create a PublicKey object from the key data downloaded from the developer portal.
        try {
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(applicationKey);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            mPublicKey = keyFactory.generatePublic(keySpec);
        } catch (Exception e) {
            Log.e(LOG_TAG, "Unable to create encryption key", e);
        }
    } catch (Exception ex) {
        Log.e(LOG_TAG, "TestOuyaFacade constructor exception", ex);
    }
}

From source file:tv.ouya.sdk.UnityOuyaFacade.java

public UnityOuyaFacade(Context context, Bundle savedInstanceState, String developerId, byte[] applicationKey) {
    try {//from ww w. j  a v a  2 s  .c o  m
        this.context = context;

        Log.i(LOG_TAG, "UnityOuyaFacade.Init(" + developerId + ");");
        UnityPlayer.UnitySendMessage("OuyaGameObject", "DebugLog",
                "UnityOuyaFacade.Init(" + developerId + ");");

        ouyaFacade = OuyaFacade.getInstance();

        Init(developerId);

        // Create a PublicKey object from the key data downloaded from the developer portal.
        try {
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(applicationKey);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            mPublicKey = keyFactory.generatePublic(keySpec);
        } catch (Exception e) {
            Log.e(LOG_TAG, "Unable to create encryption key", e);
        }
    } catch (Exception ex) {
        Log.e(LOG_TAG, "UnityOuyaFacade constructor exception", ex);
    }
}

From source file:RGSDigestTools.SignatureTool.java

/**
 * Init keys with private key from keystore and pubkey from resource
 * @param pKeyStorePath//from w ww  .  j  ava  2s.c om
 * @param pKeyStorePasswd
 * @param pDSAlias
 * @param pPrivKeyPasswd
 * @param PubkeyResource 
 * @throws java.security.KeyStoreException 
 * @throws java.security.cert.CertificateException 
 * @throws java.security.NoSuchAlgorithmException 
 * @throws java.io.IOException 
 * @throws java.security.UnrecoverableEntryException 
 * @throws java.security.spec.InvalidKeySpecException 
 */
public void initKeysWithKeystoreAndFile(String pKeyStorePath, String pKeyStorePasswd, String pDSAlias,
        String pPrivKeyPasswd, String PubkeyResource) throws KeyStoreException, CertificateException,
        NoSuchAlgorithmException, IOException, UnrecoverableEntryException, InvalidKeySpecException {
    KeyStore ks = TrustStoreLoader.loadKeyStore(pKeyStorePath, pKeyStorePasswd);
    KeyStore.PasswordProtection passProtection = new KeyStore.PasswordProtection(pPrivKeyPasswd.toCharArray());
    KeyStore.PrivateKeyEntry DSKeyEnt = (KeyStore.PrivateKeyEntry) ks.getEntry(pDSAlias, passProtection);

    this.signKey = DSKeyEnt.getPrivateKey();

    InputStream is = SignatureTool.class.getResourceAsStream(PubkeyResource);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int read = is.read();
    while (read != -1) {
        baos.write(read);
        read = is.read();
    }

    byte[] keyBytes = baos.toByteArray();
    X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    this.verifyKey = keyFactory.generatePublic(spec);
}

From source file:com.kuzumeji.platform.standard.SecurityService.java

/**
 * RSA??// ww w  .j  ava2s  .co m
 * <dl>
 * <dt>?
 * <dd>RSA??
 * </dl>
 * @param name RSA???
 * @return RSA?
 */
public KeyPair loadKeyPair(final String name) {
    try {
        final Properties property = new PropertyService(PROPERTY_NAME).getProperty();
        final KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGO_NAME);
        final RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(new X509EncodedKeySpec(
                Hex.decodeHex(property.getProperty(String.format(KEY_PUBLIC_ENCODED, name)).toCharArray())));
        final RSAPrivateKey privateKey = (RSAPrivateKey) keyFactory.generatePrivate(new PKCS8EncodedKeySpec(
                Hex.decodeHex(property.getProperty(String.format(KEY_PRIVATE_ENCODED, name)).toCharArray())));
        return new KeyPair(publicKey, privateKey);
    } catch (final IOException | DecoderException | InvalidKeySpecException | NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.znsx.util.licence.LicenceUtil.java

/**
 * ???//from w  w w.  ja  v a  2s  . c  om
 * 
 * @param data
 *            ??
 * @param publicKeyString
 *            ??base64?
 * @param signature
 *            base64????
 * @return
 * @throws Exception
 */
public static boolean verify(String data, String publicKeyString, String signature) throws Exception {
    // ???
    // BASE64Decoder decoder = new BASE64Decoder();
    // byte[] bytes = decoder.decodeBuffer(publicKeyString);
    Base64 base64 = new Base64();
    byte[] bytes = base64.decode(publicKeyString.getBytes("utf8"));
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bytes);
    PublicKey publicKey = KeyFactory.getInstance("DSA").generatePublic(keySpec);
    // ?
    Signature sign = Signature.getInstance("DSA");
    sign.initVerify(publicKey);
    sign.update(data.getBytes("utf8"));
    // return sign.verify(decoder.decodeBuffer(signature));
    return sign.verify(base64.decode(signature.getBytes("utf8")));
}

From source file:de.alpharogroup.crypto.key.KeyExtensions.java

/**
 * Read public key./*w  ww  .j a  v  a 2  s.  com*/
 *
 * @param publicKeyBytes
 *            the public key bytes
 * @param provider
 *            the provider
 * @return the public key
 * @throws NoSuchAlgorithmException
 *             is thrown if instantiation of the cypher object fails.
 * @throws InvalidKeySpecException
 *             is thrown if generation of the SecretKey object fails.
 * @throws NoSuchProviderException
 *             is thrown if the specified provider is not registered in the security provider
 *             list.
 */
public static PublicKey readPublicKey(final byte[] publicKeyBytes, final String provider)
        throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
    final X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
    final KeyFactory keyFactory = KeyFactory.getInstance(KeyPairGeneratorAlgorithm.RSA.getAlgorithm());
    final PublicKey publicKey = keyFactory.generatePublic(keySpec);
    return publicKey;
}

From source file:com.ibm.dbwkl.helper.CryptionModule.java

/**
 * @return PublicKey Instance// w w w .ja v  a 2 s. co m
 * @throws NoSuchAlgorithmException
 * @throws IOException
 * @throws InvalidKeySpecException
 */
private RSAPublicKey getPublicKey() throws NoSuchAlgorithmException, IOException, InvalidKeySpecException {
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    KeySpec keySpec = new X509EncodedKeySpec(Base64.decodeBase64(PUBLIC_KEY));
    RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(keySpec);

    return publicKey;
}