List of usage examples for java.security Key getEncoded
public byte[] getEncoded();
From source file:com.zacwolf.commons.crypto._CRYPTOfactory.java
final public static _CRYPTOfactory getInstanceFromKeystore(final KeyStore keystore, final char[] keystorepass, final String alias) throws UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException, IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException { final Key keyFromStore = keystore.getKey(alias, keystorepass); final String type = keyFromStore.getAlgorithm(); return new _CRYPTOfactory( (Crypter) Class.forName(_CRYPTOfactory.class.getPackage().getName() + "." + type + "Crypter") .getConstructor(byte[].class).newInstance(keyFromStore.getEncoded())); }
From source file:org.opensaml.xml.security.SecurityHelper.java
/** * Get the key length in bits of the specified key. * //w w w . jav a 2s . c om * @param key the key to evaluate * @return length of the key in bits, or null if the length can not be determined */ public static Integer getKeyLength(Key key) { // TODO investigate techniques (and use cases) to determine length in other cases, // e.g. RSA and DSA keys, and non-RAW format symmetric keys if (key instanceof SecretKey && "RAW".equals(key.getFormat())) { return key.getEncoded().length * 8; } log.debug("Unable to determine length in bits of specified Key instance"); return null; }
From source file:net.jmhertlein.core.crypto.Keys.java
/** * Saves the given key to the given file. This method will NOT clobber * existing files- will return false if file exists The file will be * created, along with any parent directories needed. * * @param file name of file to save to/*w w w . ja v a 2s . co m*/ * @param key key to save * * @return true if successfully written, false otherwise */ public static boolean storeKey(String file, Key key) { File f = new File(file); if (!f.exists()) try { if (f.getParentFile() != null) f.getParentFile().mkdirs(); f.createNewFile(); } catch (IOException ex) { Logger.getLogger(Keys.class.getName()).log(Level.SEVERE, null, ex); } else return false; try (FileOutputStream fos = new FileOutputStream(file); PrintStream ps = new PrintStream(fos)) { ps.println(Base64.encodeBase64String(key.getEncoded())); return true; } catch (IOException ioe) { ioe.printStackTrace(); return false; } }
From source file:com.glaf.core.security.SecurityUtils.java
/** * ?????,??/*from ww w . j av a 2 s . co m*/ * * @param ctx * * @param symmetryKey * * @param pubKey * * @return String(?base64?) */ public static String generateDigitalEnvelope(SecurityContext ctx, Key symmetryKey, byte[] pubKey) { String result = null; InputStream inputStream = null; try { CertificateFactory cf = CertificateFactory.getInstance("X.509"); inputStream = new ByteArrayInputStream(pubKey); java.security.cert.Certificate cert = cf.generateCertificate(inputStream); inputStream.close(); PublicKey publicKey = cert.getPublicKey(); Cipher cipher = Cipher.getInstance(ctx.getAsymmetryAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, publicKey); result = Base64.encodeBase64String(cipher.doFinal(symmetryKey.getEncoded())); return result; } catch (Exception ex) { throw new SecurityException(ex); } finally { try { if (inputStream != null) { inputStream.close(); inputStream = null; } } catch (IOException ex) { } } }
From source file:org.apache.hadoop.hbase.security.EncryptionUtil.java
/** * Protect a key by encrypting it with the secret key of the given subject. * The configuration must be set up correctly for key alias resolution. * @param conf configuration//from ww w . j a va 2s . co m * @param subject subject key alias * @param key the key * @return the encrypted key bytes */ public static byte[] wrapKey(Configuration conf, String subject, Key key) throws IOException { // Wrap the key with the configured encryption algorithm. String algorithm = conf.get(HConstants.CRYPTO_KEY_ALGORITHM_CONF_KEY, HConstants.CIPHER_AES); Cipher cipher = Encryption.getCipher(conf, algorithm); if (cipher == null) { throw new RuntimeException("Cipher '" + algorithm + "' not available"); } EncryptionProtos.WrappedKey.Builder builder = EncryptionProtos.WrappedKey.newBuilder(); builder.setAlgorithm(key.getAlgorithm()); byte[] iv = null; if (cipher.getIvLength() > 0) { iv = new byte[cipher.getIvLength()]; RNG.nextBytes(iv); builder.setIv(ByteStringer.wrap(iv)); } byte[] keyBytes = key.getEncoded(); builder.setLength(keyBytes.length); builder.setHash(ByteStringer.wrap(Encryption.hash128(keyBytes))); ByteArrayOutputStream out = new ByteArrayOutputStream(); Encryption.encryptWithSubjectKey(out, new ByteArrayInputStream(keyBytes), subject, conf, cipher, iv); builder.setData(ByteStringer.wrap(out.toByteArray())); // Build and return the protobuf message out.reset(); builder.build().writeDelimitedTo(out); return out.toByteArray(); }
From source file:bftsmart.tom.util.RSAKeyPairGenerator.java
private String getKeyAsString(Key key) { byte[] keyBytes = key.getEncoded(); return Base64.encodeBase64String(keyBytes); }
From source file:com.github.sshw.crypt.EncryptionBean.java
public String encrypt(String message, String password) throws Exception { Cipher encrypt = Cipher.getInstance("AES/CBC/PKCS5Padding"); Key key = keyFromPassword(password); IvParameterSpec ivb = new IvParameterSpec(key.getEncoded()); encrypt.init(Cipher.ENCRYPT_MODE, key, ivb); byte[] cb = encrypt.doFinal(message.getBytes()); String c = Base64.encodeBase64URLSafeString(cb); return c;/* ww w . ja v a 2 s.co m*/ }
From source file:org.apache.hadoop.hbase.io.crypto.TestKeyStoreKeyProvider.java
@Test(timeout = 30000) public void testKeyStoreKeyProviderWithPassword() throws Exception { KeyProvider provider = new KeyStoreKeyProvider(); provider.init("jceks://" + storeFile.toURI().getPath() + "?password=" + PASSWORD); Key key = provider.getKey(ALIAS); assertNotNull(key);//from w ww. j a v a2 s . c o m byte[] keyBytes = key.getEncoded(); assertEquals(keyBytes.length, KEY.length); for (int i = 0; i < KEY.length; i++) { assertEquals(keyBytes[i], KEY[i]); } }
From source file:org.apache.hadoop.hbase.io.crypto.TestKeyStoreKeyProvider.java
@Test(timeout = 30000) public void testKeyStoreKeyProviderWithPasswordFile() throws Exception { KeyProvider provider = new KeyStoreKeyProvider(); provider.init("jceks://" + storeFile.toURI().getPath() + "?passwordFile=" + URLEncoder.encode(passwordFile.getAbsolutePath(), "UTF-8")); Key key = provider.getKey(ALIAS); assertNotNull(key);//w w w .j a v a2 s . c o m byte[] keyBytes = key.getEncoded(); assertEquals(keyBytes.length, KEY.length); for (int i = 0; i < KEY.length; i++) { assertEquals(keyBytes[i], KEY[i]); } }
From source file:org.sakaiproject.tool.rutgers.LinkTool.java
/** * Writes <code>key</code> to file with name <code>filename</code> * * @throws IOException if something goes wrong. *///from w w w .j a v a 2 s. c o m private static void writeKey(Key key, String filename) { FileOutputStream file = null; try { file = new FileOutputStream(filename); file.write(key.getEncoded()); } catch (FileNotFoundException e) { M_log.error("Unable to write new key to " + filename); } catch (IOException e) { M_log.error("Unable to write new key to " + filename); } finally { if (file != null) { try { file.close(); } catch (IOException e) { // tried } } } }