List of usage examples for javax.crypto SecretKey getEncoded
public byte[] getEncoded();
From source file:us.the.mac.android.jni.helpers.EncryptedStringTest.java
@Test public void creatingASecretKeySpec() throws Exception { byte[] keyBytes = TestConstants.TEST_ENCRYPTION_KEY.getBytes(); assertEquals(TestConstants.TEST_ENCRYPTION_KEY, new String(keyBytes)); byte[] decodedKeyBytes = Base64.decodeBase64(keyBytes); byte[] encodedKeyBytes = Base64.encodeBase64(decodedKeyBytes); // assertEquals(TestConstants.TEST_DECODED_ENCRYPTION_KEY, new String(decodedKeyBytes)); byte[] nativeDecodedKeyBytes = nativeBase64DecodeBase64(keyBytes); byte[] nativeEncodedKeyBytes = nativeBase64EncodeBase64(nativeDecodedKeyBytes); assertEquals(new String(encodedKeyBytes), new String(nativeEncodedKeyBytes)); assertEquals(new String(decodedKeyBytes), new String(nativeDecodedKeyBytes)); // assertEquals(TestConstants.TEST_DECODED_ENCRYPTION_KEY, new String(nativeDecodedKeyBytes)); SecretKey originalKey = new SecretKeySpec(decodedKeyBytes, 0, decodedKeyBytes.length, "AES"); assertEquals(TestConstants.TEST_ENCRYPTION_KEY, new String(Base64.encodeBase64(originalKey.getEncoded())).concat("\n")); // assertEquals(TestConstants.TEST_DECODED_ENCRYPTION_KEY, new String(originalKey.getEncoded()).concat("\n")); SecretKey nativeKey = createSecretKeySpecWithParams(decodedKeyBytes, 0, decodedKeyBytes.length, "AES"); assertEquals(originalKey, nativeKey); }
From source file:pt.lunacloud.services.storage.internal.crypto.EncryptionUtils.java
/** * Encrypts a symmetric key using the provided encryption materials and returns * it in raw byte array form./*from w w w. ja v a2 s. c om*/ */ public static byte[] getEncryptedSymmetricKey(SecretKey toBeEncrypted, EncryptionMaterials materials, Provider cryptoProvider) { Key keyToDoEncryption; if (materials.getKeyPair() != null) { // Do envelope encryption with public key from key pair keyToDoEncryption = materials.getKeyPair().getPublic(); } else { // Do envelope encryption with symmetric key keyToDoEncryption = materials.getSymmetricKey(); } try { Cipher cipher; byte[] toBeEncryptedBytes = toBeEncrypted.getEncoded(); if (cryptoProvider != null) { cipher = Cipher.getInstance(keyToDoEncryption.getAlgorithm(), cryptoProvider); } else { cipher = Cipher.getInstance(keyToDoEncryption.getAlgorithm()); // Use default JCE Provider } cipher.init(Cipher.ENCRYPT_MODE, keyToDoEncryption); return cipher.doFinal(toBeEncryptedBytes); } catch (Exception e) { throw new LunacloudClientException("Unable to encrypt symmetric key: " + e.getMessage(), e); } }
From source file:org.openengsb.core.services.SecureJsonPortTest.java
@Override protected String encodeAndEncrypt(MethodCallMessage secureRequest, SecretKey sessionKey) throws Exception { byte[] content = mapper.writeValueAsBytes(secureRequest); LOGGER.info("encrypting: " + new String(content)); byte[] encryptedContent = CipherUtils.encrypt(content, sessionKey); EncryptedMessage encryptedMessage = new EncryptedMessage(); encryptedMessage.setEncryptedContent(encryptedContent); byte[] encryptedKey = CipherUtils.encrypt(sessionKey.getEncoded(), serverPublicKey); encryptedMessage.setEncryptedKey(encryptedKey); return mapper.writeValueAsString(encryptedMessage); }
From source file:com.diona.fileReader.CipherUtil.java
/** * Generates a random Base64 encoded string value. * /*from ww w.j a va2 s.com*/ * @param length * The length of the key. * @return A random key value. */ public byte[] generateRandomKeyBytes(final int length) { byte[] randomKey = null; // Use a SecureRandom generator try { final SecureRandom secureRandom = new SecureRandom(); final KeyGenerator keyGenerator = KeyGenerator.getInstance(ENCRYPTION_ALGORITHM); keyGenerator.init(length, secureRandom); final SecretKey secretKey = keyGenerator.generateKey(); randomKey = secretKey.getEncoded(); } catch (final NoSuchAlgorithmException e) { Log.e(TAG, "Exception generating random key", e); } return randomKey; }
From source file:com.microsoft.aad.adal.CordovaAdalPlugin.java
public CordovaAdalPlugin() { // Android API < 18 does not support AndroidKeyStore so ADAL requires // some extra work to crete and pass secret key to ADAL. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) { try {/*from w w w .ja v a 2 s . co m*/ SecretKey secretKey = this.createSecretKey(SECRET_KEY); AuthenticationSettings.INSTANCE.setSecretKey(secretKey.getEncoded()); } catch (Exception e) { Log.w("CordovaAdalPlugin", "Unable to create secret key: " + e.getMessage()); } } }
From source file:org.apache.spark.network.crypto.AuthEngine.java
private SecretKeySpec generateKey(String kdf, int iterations, byte[] salt, int keyLength) throws GeneralSecurityException { SecretKeyFactory factory = SecretKeyFactory.getInstance(kdf); PBEKeySpec spec = new PBEKeySpec(secret, salt, iterations, keyLength); long start = System.nanoTime(); SecretKey key = factory.generateSecret(spec); long end = System.nanoTime(); LOG.debug("Generated key with {} iterations in {} us.", conf.keyFactoryIterations(), (end - start) / 1000); return new SecretKeySpec(key.getEncoded(), conf.keyAlgorithm()); }
From source file:com.adaptris.security.password.AesCrypto.java
public String encode(String plainText, String charset) throws PasswordException { String result = null;//from ww w.ja va 2s . c o m try { KeyGenerator kg = KeyGenerator.getInstance(ALG); kg.init(KEY_LEN, SecurityUtil.getSecureRandom()); SecretKey sessionKey = kg.generateKey(); Cipher dataCipher = Cipher.getInstance(CIPHER); dataCipher.init(Cipher.ENCRYPT_MODE, sessionKey); byte[] encryptedBody = dataCipher.doFinal(seed(plainText, charset)); Output output = new Output(); output.setSessionKey(sessionKey.getEncoded()); output.setSessionVector(dataCipher.getIV()); output.setEncryptedData(encryptedBody); result = Password.PORTABLE_PASSWORD + output.write(); } catch (Exception e) { throw new PasswordException(e); } return result; }
From source file:org.apache.ws.security.message.token.BSTKerberosTest.java
/** * A test for encryption using a direct reference to a Kerberos token *//*from w ww .j a v a 2 s. c om*/ @org.junit.Test public void testKerberosEncryptionDRCreation() throws Exception { Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG); WSSecHeader secHeader = new WSSecHeader(); secHeader.insertSecurityHeader(doc); BinarySecurity bst = new BinarySecurity(doc); bst.setValueType(AP_REQ); bst.setEncodingType(BASE64_NS); KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(128); SecretKey key = keyGen.generateKey(); byte[] keyData = key.getEncoded(); bst.setToken(keyData); bst.setID("Id-" + bst.hashCode()); WSSecurityUtil.prependChildElement(secHeader.getSecurityHeader(), bst.getElement()); WSSecEncrypt builder = new WSSecEncrypt(); builder.setSymmetricEncAlgorithm(WSConstants.AES_128); builder.setSymmetricKey(key); builder.setEncryptSymmKey(false); builder.setCustomReferenceValue(AP_REQ); builder.setEncKeyId(bst.getID()); Document encryptedDoc = builder.build(doc, crypto, secHeader); if (LOG.isDebugEnabled()) { String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(encryptedDoc); LOG.debug(outputString); } }
From source file:org.apache.ws.security.message.token.BSTKerberosTest.java
/** * A test for signing using a direct reference to a Kerberos token *//* w w w . jav a 2 s .co m*/ @org.junit.Test public void testKerberosSignatureDRCreation() throws Exception { Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG); WSSecHeader secHeader = new WSSecHeader(); secHeader.insertSecurityHeader(doc); BinarySecurity bst = new BinarySecurity(doc); bst.setValueType(AP_REQ); bst.setEncodingType(BASE64_NS); KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(128); SecretKey key = keyGen.generateKey(); byte[] keyData = key.getEncoded(); bst.setToken(keyData); bst.setID("Id-" + bst.hashCode()); WSSecurityUtil.prependChildElement(secHeader.getSecurityHeader(), bst.getElement()); WSSecSignature sign = new WSSecSignature(); sign.setSignatureAlgorithm(SignatureMethod.HMAC_SHA1); sign.setKeyIdentifierType(WSConstants.CUSTOM_SYMM_SIGNING); sign.setCustomTokenValueType(AP_REQ); sign.setCustomTokenId(bst.getID()); sign.setSecretKey(keyData); Document signedDoc = sign.build(doc, crypto, secHeader); if (LOG.isDebugEnabled()) { String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(signedDoc); LOG.debug(outputString); } }
From source file:org.apache.ws.security.message.token.BSTKerberosTest.java
/** * A test for signing using a KeyIdentifier to a Kerberos token *//*from w w w .j av a 2 s. c o m*/ @org.junit.Test public void testKerberosSignatureKICreation() throws Exception { Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG); WSSecHeader secHeader = new WSSecHeader(); secHeader.insertSecurityHeader(doc); BinarySecurity bst = new BinarySecurity(doc); bst.setValueType(AP_REQ); bst.setEncodingType(BASE64_NS); KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(128); SecretKey key = keyGen.generateKey(); byte[] keyData = key.getEncoded(); bst.setToken(keyData); bst.setID("Id-" + bst.hashCode()); WSSecurityUtil.prependChildElement(secHeader.getSecurityHeader(), bst.getElement()); WSSecSignature sign = new WSSecSignature(); sign.setSignatureAlgorithm(SignatureMethod.HMAC_SHA1); sign.setKeyIdentifierType(WSConstants.CUSTOM_KEY_IDENTIFIER); sign.setCustomTokenValueType(WSConstants.WSS_KRB_KI_VALUE_TYPE); byte[] digestBytes = WSSecurityUtil.generateDigest(keyData); sign.setCustomTokenId(Base64.encode(digestBytes)); sign.setSecretKey(keyData); Document signedDoc = sign.build(doc, crypto, secHeader); if (LOG.isDebugEnabled()) { String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(signedDoc); LOG.debug(outputString); } }