List of usage examples for java.security Key getEncoded
public byte[] getEncoded();
From source file:com.github.sshw.crypt.EncryptionBean.java
public String decrypt(String message, String password) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); Key key = keyFromPassword(password); byte[] cb = Base64.decodeBase64(message); IvParameterSpec ivb = new IvParameterSpec(key.getEncoded()); cipher.init(Cipher.DECRYPT_MODE, key, ivb); String plaintext = new String(cipher.doFinal(cb)); return plaintext; }
From source file:com.concursive.commons.codec.PrivateStringTest.java
public void testNewKey() throws Exception { // Generate a key Key key = PrivateString.generateKey(); assertEquals("AES", key.getAlgorithm()); // Hex encode the key for portability String hexEncodedKey = new String(Hex.encodeHex(key.getEncoded())); assertNotNull("hexEncodedKey", hexEncodedKey); // Encrypt some text String encryptedText = PrivateString.encrypt(key, plainText); assertNotNull("encryptedText", encryptedText); // Decrypt the text for comparison String decryptedText = PrivateString.decrypt(key, encryptedText); assertEquals(plainText, decryptedText); // Decode the key for re-use and make sure it's the same Key hexDecodedKey = PrivateString.decodeHex(hexEncodedKey, key.getAlgorithm()); assertEquals(key, hexDecodedKey);//from w w w . j a va 2 s. c o m // Decode the encrypted text for comparison String hexDecryptedText = PrivateString.decrypt(hexDecodedKey, encryptedText); assertEquals(plainText, hexDecryptedText); // Compare the original key and the hex encoded key String hexEncryptedText = PrivateString.encrypt(hexDecodedKey, hexDecryptedText); assertEquals(encryptedText, hexEncryptedText); }
From source file:org.sonar.application.AesCipherTest.java
@Test public void loadSecretKeyFromFile_trim_content() throws Exception { String path = getPath("non_trimmed_secret_key.txt"); AesCipher cipher = new AesCipher(null); Key secretKey = cipher.loadSecretFileFromFile(path); assertThat(secretKey.getAlgorithm()).isEqualTo("AES"); assertThat(secretKey.getEncoded().length).isGreaterThan(10); }
From source file:org.wso2.carbon.identity.provider.openid.cache.OpenIDAssociationCache.java
/** * Read entries from the cache. If no value found then returns null. * If the association is expired then returns null. * Else returns the <code>Association</code> * * @param handle//w w w .ja v a 2 s.c om * @return <code>Association<code> */ public Association getFromCache(String handle) { if (IdentityUtil.isBlank(handle)) { throw new IllegalArgumentException("Handle is \'NULL\'"); } OpenIDIdentityCacheKey cacheKey = new OpenIDIdentityCacheKey(0, handle); OpenIDIdentityCacheEntry cacheEntry = associationCache.getValueFromCache(cacheKey); if (cacheEntry != null) { if (log.isDebugEnabled()) { log.debug("Cache hit for handle : " + handle); } Date expiry = cacheEntry.getDate(); String type = cacheEntry.getCacheEntry(); Key secretKey = cacheEntry.getSecretKey(); if (Association.TYPE_HMAC_SHA1.equals(type)) { return Association.createHmacSha1(handle, secretKey.getEncoded(), expiry); } else if (Association.TYPE_HMAC_SHA256.equals(type)) { return Association.createHmacSha256(handle, secretKey.getEncoded(), expiry); } else { throw new IdentityRuntimeException("Invalid algorithm " + type); } /* * We are not removing expired handles from the cache. If we * do, then at a lookup for a expired search, it will fall * back to a database lookup which costs a lot. JCache * should remove an entry if an entry was never called. * * if(association.hasExpired()){ * associationCache.removeCacheEntry(handle); * if(log.isDebugEnabled()){ * log.debug("Expired entry in cache for handle : " + * handle); } } else { return association; } */ } else { if (log.isDebugEnabled()) { log.debug("Cache miss for handle : " + handle); } return null; } }
From source file:org.sonar.application.AesCipherTest.java
@Test public void loadSecretKeyFromFile() throws Exception { AesCipher cipher = new AesCipher(null); Key secretKey = cipher.loadSecretFileFromFile(pathToSecretKey()); assertThat(secretKey.getAlgorithm()).isEqualTo("AES"); assertThat(secretKey.getEncoded().length).isGreaterThan(10); }
From source file:org.apache.ws.security.saml.SamlTokenDerivedTest.java
/** * Create a WSSecDKSign object, that signs the SOAP Body as well as the SAML Assertion * via a STR Transform./*from w w w . j a v a 2 s . c o m*/ */ private WSSecDKSign createDKSign(Document doc, SecurityTokenReference secRefSaml) throws WSSecurityException { SecurityTokenReference secToken = new SecurityTokenReference(doc); CryptoType cryptoType = new CryptoType(CryptoType.TYPE.ALIAS); cryptoType.setAlias("16c73ab6-b892-458f-abf5-2f875f74882e"); X509Certificate[] certs = crypto.getX509Certificates(cryptoType); secToken.setKeyIdentifierThumb(certs[0]); WSSecDKSign sigBuilder = new WSSecDKSign(); java.security.Key key = crypto.getPrivateKey("16c73ab6-b892-458f-abf5-2f875f74882e", "security"); sigBuilder.setExternalKey(key.getEncoded(), secToken.getElement()); sigBuilder.setSignatureAlgorithm(WSConstants.HMAC_SHA1); List<WSEncryptionPart> parts = new ArrayList<WSEncryptionPart>(2); String soapNamespace = WSSecurityUtil.getSOAPNamespace(doc.getDocumentElement()); WSEncryptionPart encP = new WSEncryptionPart(WSConstants.ELEM_BODY, soapNamespace, "Content"); parts.add(encP); encP = new WSEncryptionPart("STRTransform", "", "Element"); encP.setId(secRefSaml.getID()); encP.setElement(secRefSaml.getElement()); parts.add(encP); sigBuilder.setParts(parts); return sigBuilder; }
From source file:org.sonar.api.config.AesCipherTest.java
@Test public void loadSecretKeyFromFile_trim_content() throws Exception { URL resource = getClass().getResource("/org/sonar/api/config/AesCipherTest/non_trimmed_secret_key.txt"); String path = new File(resource.toURI()).getCanonicalPath(); AesCipher cipher = new AesCipher(new Settings()); Key secretKey = cipher.loadSecretFileFromFile(path); assertThat(secretKey.getAlgorithm(), is("AES")); assertThat(secretKey.getEncoded().length, greaterThan(10)); }
From source file:hudson.cli.Connection.java
public void writeKey(Key key) throws IOException { writeUTF(new String(Base64.encodeBase64(key.getEncoded()))); }
From source file:org.sonar.api.config.AesCipherTest.java
@Test public void loadSecretKeyFromFile() throws Exception { AesCipher cipher = new AesCipher(new Settings()); Key secretKey = cipher.loadSecretFileFromFile(pathToSecretKey()); assertThat(secretKey.getAlgorithm(), is("AES")); assertThat(secretKey.getEncoded().length, greaterThan(10)); }
From source file:org.apigw.commons.crypto.ApigwCrypto.java
/** * * @return The SecretKeySpec to be used for encryption/decryption of messages * @throws UnrecoverableKeyException/*from w w w.j av a 2s .c o m*/ * @throws NoSuchAlgorithmException * @throws KeyStoreException */ protected SecretKeySpec getSecretKeySpec() throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException { Key key = keyStore.getKey(alias, keyStorePassword.toCharArray()); return new SecretKeySpec(key.getEncoded(), KEY_ALGORITHM); }