List of usage examples for javax.crypto SecretKey getAlgorithm
public String getAlgorithm();
From source file:org.tolven.security.bean.DocProtectionBean.java
/** * Currently assumes all content is encrypted and only the authorized loggedInUser will succeed in getting the readable content * This method calls decryption each time it is called. * Decryption takes CPU time and it requires access to security policy which means * the caller must have permission to call this method. * @param encryptedContent/*from w w w. j a v a 2 s.co m*/ * @return */ public byte[] getDecryptedContent(DocContentSecurity doc, AccountUser activeAccountUser, PrivateKey userPrivateKey) { // TolvenLogger.info("DocProtectedBean.getDecryptedContent", DocProtectionBean.class); if (doc.getContent() == null) return doc.getContent(); try { PrivateKey accountPrivateKey = KeyUtility.getAccountPrivateKey(activeAccountUser, userPrivateKey); // TolvenLogger.info(getClass() + " Decryption AccountPrivateKey=" + activeAccountPrivateKey, DocProtectionBean.class); if (doc.getDocumentSecretKey() == null) { //TODO: For backward compatibility, we no longer throw an exception here, since older accounts never had a documenSecretKey and // were thus never encrypted //throw new RuntimeException("Content cannot be decrypted without a documentSecretKey"); TolvenLogger.info(getClass() + " No DocumentSecretKey found for doc id=" + doc.getId(), DocProtectionBean.class); return doc.getContent(); } SecretKey docSecretKey = doc.getDocumentSecretKey().getSecretKey(accountPrivateKey); Cipher cipher = Cipher.getInstance(docSecretKey.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, docSecretKey); return cipher.doFinal(doc.getContent()); } catch (Exception ex) { ex.printStackTrace(); return "THIS DOCUMENT CANNOT BE DECRYPTED".getBytes(); } }
From source file:org.webical.dao.encryption.impl.DesEncryptor.java
/** * Creates the DesEncryptor//from w w w .j av a2 s. c o m * @param passPhrase the passphrase to use in encryption and decryption * @throws InvalidKeyException * @throws InvalidAlgorithmParameterException * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws InvalidKeySpecException */ public DesEncryptor(String passPhrase) throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException { // Create the key KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount); SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); encryptCipher = Cipher.getInstance(key.getAlgorithm()); decryptCipher = Cipher.getInstance(key.getAlgorithm()); // Prepare the parameter to the ciphers AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount); // Create the ciphers encryptCipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); decryptCipher.init(Cipher.DECRYPT_MODE, key, paramSpec); }
From source file:passworddecoder.DesEncrypter.java
DesEncrypter(String passPhrase) { KeySpec keySpec;/* www . j a v a 2 s. c om*/ try { keySpec = new PBEKeySpec(passPhrase.toCharArray(), this.salt, this.iterationCount); SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); this.dcipher = Cipher.getInstance(key.getAlgorithm()); AlgorithmParameterSpec paramSpec = new PBEParameterSpec(this.salt, this.iterationCount); this.dcipher.init(2, key, paramSpec); } catch (InvalidAlgorithmParameterException e) { } catch (InvalidKeySpecException e) { } catch (NoSuchPaddingException e) { } catch (NoSuchAlgorithmException e) { } catch (InvalidKeyException e) { } }
From source file:test.integ.be.fedict.commons.eid.client.JCATest.java
@Test public void testSoftwareRSAKeyWrapping() throws Exception { final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); final KeyPair keyPair = keyPairGenerator.generateKeyPair(); final KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); final SecretKey secretKey = keyGenerator.generateKey(); LOG.debug("secret key algo: " + secretKey.getAlgorithm()); final Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.WRAP_MODE, keyPair.getPublic()); LOG.debug("cipher security provider: " + cipher.getProvider().getName()); LOG.debug("cipher type: " + cipher.getClass().getName()); final byte[] wrappedKey = cipher.wrap(secretKey); cipher.init(Cipher.UNWRAP_MODE, keyPair.getPrivate()); final Key resultKey = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY); assertArrayEquals(secretKey.getEncoded(), resultKey.getEncoded()); }
From source file:test.integ.be.fedict.eid.idp.OpenIDAssociationsTest.java
@Test public void testEstablishAssociation() throws Exception { // setup//from ww w . j av a 2 s. c o m AssociationSessionType associationSessionType = AssociationSessionType.NO_ENCRYPTION_SHA1MAC; String opEndpoint = "https://www.e-contract.be/eid-idp/protocol/openid/auth"; // operate DiffieHellmanSession dhSession; if (null != associationSessionType.getHAlgorithm()) { // Diffie-Hellman DHParameterSpec dhParameterSpec = DiffieHellmanSession.getDefaultParameter(); dhSession = DiffieHellmanSession.create(associationSessionType, dhParameterSpec); } else { dhSession = null; } AssociationRequest associationRequest = AssociationRequest.createAssociationRequest(associationSessionType, dhSession); LOG.debug("association type: " + associationRequest.getType().getAssociationType()); LOG.debug("session type: " + associationRequest.getType().getSessionType()); Map<String, String> parameters = associationRequest.getParameterMap(); HttpClient httpClient = new HttpClient(); httpClient.getHostConfiguration().setProxy("proxy.yourict.net", 8080); PostMethod postMethod = new PostMethod(opEndpoint); for (Map.Entry<String, String> parameter : parameters.entrySet()) { postMethod.addParameter(parameter.getKey(), parameter.getValue()); } int statusCode = httpClient.executeMethod(postMethod); LOG.debug("status code: " + statusCode); assertEquals(HttpURLConnection.HTTP_OK, statusCode); postMethod.getResponseBody(); ParameterList responseParameterList = ParameterList .createFromKeyValueForm(postMethod.getResponseBodyAsString()); AssociationResponse associationResponse = AssociationResponse .createAssociationResponse(responseParameterList); Association association = associationResponse.getAssociation(dhSession); LOG.debug("association type: " + association.getType()); LOG.debug("association handle: " + association.getHandle()); LOG.debug("association expiry: " + association.getExpiry()); SecretKey secretKey = association.getMacKey(); LOG.debug("association MAC key algo: " + secretKey.getAlgorithm()); }
From source file:test.integ.be.fedict.eid.idp.OpenIDAssociationsTest.java
/** * http://code.google.com/p/openid4java/issues/detail?id=192 * //from www .j av a 2s . com * @throws Exception */ @Test public void testEstablishAssociationSteam() throws Exception { // setup AssociationSessionType associationSessionType = AssociationSessionType.NO_ENCRYPTION_SHA1MAC; String opEndpoint = "https://steamcommunity.com/openid/login"; // operate DiffieHellmanSession dhSession; if (null != associationSessionType.getHAlgorithm()) { // Diffie-Hellman DHParameterSpec dhParameterSpec = DiffieHellmanSession.getDefaultParameter(); dhSession = DiffieHellmanSession.create(associationSessionType, dhParameterSpec); } else { dhSession = null; } AssociationRequest associationRequest = AssociationRequest.createAssociationRequest(associationSessionType, dhSession); LOG.debug("association type: " + associationRequest.getType().getAssociationType()); LOG.debug("session type: " + associationRequest.getType().getSessionType()); Map<String, String> parameters = associationRequest.getParameterMap(); HttpClient httpClient = new HttpClient(); httpClient.getHostConfiguration().setProxy("proxy.yourict.net", 8080); PostMethod postMethod = new PostMethod(opEndpoint); for (Map.Entry<String, String> parameter : parameters.entrySet()) { postMethod.addParameter(parameter.getKey(), parameter.getValue()); } int statusCode = httpClient.executeMethod(postMethod); LOG.debug("status code: " + statusCode); assertEquals(HttpURLConnection.HTTP_OK, statusCode); postMethod.getResponseBody(); ParameterList responseParameterList = ParameterList .createFromKeyValueForm(postMethod.getResponseBodyAsString()); AssociationResponse associationResponse = AssociationResponse .createAssociationResponse(responseParameterList); Association association = associationResponse.getAssociation(dhSession); LOG.debug("association type: " + association.getType()); LOG.debug("association handle: " + association.getHandle()); LOG.debug("association expiry: " + association.getExpiry()); SecretKey secretKey = association.getMacKey(); LOG.debug("association MAC key algo: " + secretKey.getAlgorithm()); }
From source file:test.unit.be.fedict.eid.applet.service.UserIdentifierUtilTest.java
@Test public void testHMacSha1() throws Exception { SecretKey macKey = new SecretKeySpec("1234".getBytes(), "HmacSHA1"); Mac mac = Mac.getInstance(macKey.getAlgorithm()); mac.init(macKey);/*from w w w.j a va2 s .c om*/ byte[] data = "hello world".getBytes(); mac.update(data); byte[] resultHMac = mac.doFinal(); LOG.debug("size result HMAC-SHA1: " + resultHMac.length); String resultHex = new String(Hex.encodeHex(resultHMac)).toUpperCase(); LOG.debug("result HMAC-SHA1 HEX: " + resultHex); }
From source file:test.unit.be.fedict.eid.idp.protocol.saml2.SAML2Test.java
@Test public void testGetAlgorithm() throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128);//from w w w. j a va2s .c o m SecretKey key = kgen.generateKey(); LOG.debug("Algorithm AES-128: " + key.getAlgorithm()); }
From source file:test.unit.org.owasp.webscarab.plugin.saml.SamlTest.java
@Test public void testEncryptionAES() throws Exception { KeyGenerator keygen = KeyGenerator.getInstance("AES"); keygen.init(128);//from www .j a v a 2 s . co m SecretKey secretKey = keygen.generateKey(); LOG.debug("secret key algo: " + secretKey.getAlgorithm()); LOG.debug("secret key format: " + secretKey.getFormat()); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); LOG.debug("cipher provider: " + cipher.getProvider().getName()); byte[] result = cipher.doFinal("hello world".getBytes()); assertNotNull(result); byte[] encodedSecretKey = secretKey.getEncoded(); LOG.debug("encoded secret key size: " + encodedSecretKey.length * 8); // decrypt cipher = Cipher.getInstance("AES"); SecretKeySpec secretKeySpec = new SecretKeySpec(encodedSecretKey, "AES"); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); byte[] decryptedResult = cipher.doFinal(result); assertEquals("hello world", new String(decryptedResult)); }
From source file:tests.unit.configuration.ConfigurationTestCase.java
/** * @see junit.framework.TestCase#setUp() *///from ww w. j a va2s .com @Override protected void setUp() throws Exception { super.setUp(); try { FileUtils.moveFile(new File(CryptoHelper.getKeystorePath() + File.separator + KEYSTORE_NAME), new File(CryptoHelper.getKeystorePath() + File.separator + KEYSTORE_NAME + ".orig")); } catch (Exception exc) { exc.printStackTrace(); fail(); } try { KeyStoreID keySid = new KeyStoreID("TEMP", KeyStoreUtils.DEFAULT_KEYSTORE_TYPE, "", "", KeyStoreUtils.DEFAULT_KEYSTORE_PROVIDER); KeyID keyid = new KeyID("TEMP", keySid, ""); keySid.setKeyStoreName(KEYSTORE_NAME); keySid.setKeyStorePwd(KEY_STORE_PWD); keyid.setKeyAlias(ALIAS_KEY_NAME); keyid.setKeyPwd(ALIAS_KEY_PWD); SecretKey secretKey = CryptoUtils.generateSecretKey(CryptoUtils.TRIPLE_DES_TYPE, KEY_STORE_PWD.getBytes()); System.out.println("***************************************"); System.out.println("Registering SecretKey: " + secretKey.getAlgorithm() + " " + secretKey.getFormat() + " " + secretKey.toString()); System.out.println("In: " + keyid); KeyStoreUtils.writeKey(CryptoHelper.getKeystorePath(), keyid, secretKey, null); CryptoHelper.resetCache(); } catch (Exception exc) { exc.printStackTrace(); throw exc; } }