List of usage examples for javax.crypto Cipher getAlgorithm
public final String getAlgorithm()
From source file:MainClass.java
public static void main(String[] args) throws Exception { Provider p = Security.getProvider("Rot13Provider"); System.out.println("Provider name: " + p.getName()); System.out.println("Provider version: " + p.getVersion()); System.out.println("Provider information: " + p.getInfo()); Cipher cipher = Cipher.getInstance("ROT13", "Rot13Provider"); System.out.println("Cipher: " + cipher.getAlgorithm()); String testString = "This is a test!"; cipher.init(Cipher.ENCRYPT_MODE, (Key) null, new SecureRandom()); byte[] b1 = cipher.doFinal(testString.getBytes()); cipher.init(Cipher.DECRYPT_MODE, (Key) null, new SecureRandom()); byte[] b2 = cipher.doFinal(b1); System.out.println("Decrypted data as a String: " + new String(b2)); }
From source file:com.dragoniade.encrypt.EncryptionHelper.java
public static void encrypt(Properties p, String seedKey, String key) { String value = p.getProperty(key); String seed = p.getProperty(seedKey, seedKey); String encrypted;// w ww .j av a 2 s .co m try { Cipher cipher = getEncrypter(seed); byte[] result = cipher.doFinal(value.getBytes("UTF-16")); encrypted = cipher.getAlgorithm() + "{" + Base64.encode(result) + "}"; } catch (Exception e) { try { encrypted = "{" + Base64.encode(value.getBytes("UTF-16")) + "}"; } catch (Exception e2) { encrypted = "{" + Base64.encode(value.getBytes()) + "}"; } } p.setProperty(key, encrypted); }
From source file:io.personium.core.model.file.DataCryptorTest.java
/** * Test encode().//w ww . j a v a 2 s. co m * normal. * @throws Exception Unexpected error. */ @Test public void encode_Normal() throws Exception { // -------------------- // Test method args // -------------------- String str = "0123456789"; InputStream input = new ByteArrayInputStream(str.getBytes(CharEncoding.UTF_8)); // -------------------- // Mock settings // -------------------- // Nothing. // -------------------- // Expected result // -------------------- // Nothing. // -------------------- // Run method // -------------------- DataCryptor cryptor = new DataCryptor("zyxwvutsrqponmlk"); CipherInputStream encodedInputStream = null; encodedInputStream = (CipherInputStream) cryptor.encode(input, true); // -------------------- // Confirm result // -------------------- Field field = FilterInputStream.class.getDeclaredField("in"); field.setAccessible(true); InputStream actualInput = (InputStream) field.get(encodedInputStream); assertThat(actualInput, is(input)); field = CipherInputStream.class.getDeclaredField("cipher"); field.setAccessible(true); Cipher actualCipher = (Cipher) field.get(encodedInputStream); byte[] expectedIV = "klmnopqrstuvwxyz".getBytes(CharEncoding.UTF_8); String expectedAlgorithm = "AES/CBC/PKCS5Padding"; assertThat(actualCipher.getIV(), is(expectedIV)); assertThat(actualCipher.getAlgorithm(), is(expectedAlgorithm)); }
From source file:io.personium.core.model.file.DataCryptorTest.java
/** * Test decode()./* w w w. j av a 2 s . c o m*/ * normal. * @throws Exception Unexpected error. */ @Test public void decode_Normal() throws Exception { // -------------------- // Test method args // -------------------- String str = "0123456789"; InputStream input = new ByteArrayInputStream(str.getBytes(CharEncoding.UTF_8)); // -------------------- // Mock settings // -------------------- // Nothing. // -------------------- // Expected result // -------------------- // Nothing. // -------------------- // Run method // -------------------- DataCryptor cryptor = new DataCryptor("zyxwvutsrqponmlk"); CipherInputStream encodedInputStream = null; encodedInputStream = (CipherInputStream) cryptor.decode(input, DataCryptor.ENCRYPTION_TYPE_AES); // -------------------- // Confirm result // -------------------- Field field = FilterInputStream.class.getDeclaredField("in"); field.setAccessible(true); InputStream actualInput = (InputStream) field.get(encodedInputStream); assertThat(actualInput, is(input)); field = CipherInputStream.class.getDeclaredField("cipher"); field.setAccessible(true); Cipher actualCipher = (Cipher) field.get(encodedInputStream); byte[] expectedIV = "klmnopqrstuvwxyz".getBytes(CharEncoding.UTF_8); String expectedAlgorithm = "AES/CBC/PKCS5Padding"; assertThat(actualCipher.getIV(), is(expectedIV)); assertThat(actualCipher.getAlgorithm(), is(expectedAlgorithm)); }
From source file:com.evolveum.midpoint.prism.crypto.AESProtector.java
private Cipher getCipher(int cipherMode, String algorithmUri) throws NoSuchAlgorithmException, NoSuchPaddingException, NoSuchProviderException, InvalidKeyException, InvalidAlgorithmParameterException { String jceAlgorithm = JCEMapper.translateURItoJCEID(algorithmUri);//JCEMapper.getJCEKeyAlgorithmFromURI(algorithmUri); Cipher cipher; if (requestedJceProviderName == null) { cipher = Cipher.getInstance(jceAlgorithm); } else {/*from www .j a va2 s .co m*/ cipher = Cipher.getInstance(jceAlgorithm, requestedJceProviderName); } if (LOGGER.isTraceEnabled()) { String desc; if (cipherMode == Cipher.ENCRYPT_MODE) { desc = "Encrypting"; } else if (cipherMode == Cipher.DECRYPT_MODE) { desc = "Decrypting"; } else { desc = "Ciphering (mode " + cipherMode + ")"; } LOGGER.trace("{} data by JCE algorithm {} (URI {}), cipher {}, provider {}", new Object[] { desc, jceAlgorithm, algorithmUri, cipher.getAlgorithm(), cipher.getProvider().getName() }); } return cipher; }
From source file:it.doqui.index.ecmengine.business.personalization.encryption.content.EncryptingContentWriterDecorator.java
public OutputStream getContentOutputStream() throws ContentIOException { logger.debug("[EncryptingContentWriterDecorator::getContentOutputStream] BEGIN"); IvParameterSpec iv = null;//from w w w. j av a 2 s. c om try { Cipher cipher = null; try { cipher = Cipher.getInstance(CryptoTransformationSpec.buildTransformationString(transformationSpec), "SunJCE"); } catch (NoSuchProviderException e) { logger.warn( "[EncryptingContentWriterDecorator::getContentOutputStream] Unknown provider \"SunJCE\". Using default..."); cipher = Cipher.getInstance(CryptoTransformationSpec.buildTransformationString(transformationSpec)); } if (transformationSpec.getMode() != null && !transformationSpec.getMode().equalsIgnoreCase("ECB")) { iv = new IvParameterSpec(transformationSpec.getIv()); cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); } else { cipher.init(Cipher.ENCRYPT_MODE, secretKey); } logger.debug("[EncryptingContentWriterDecorator::getContentOutputStream] " + "Cipher initialized: ENCRYPT - " + cipher.getProvider() + " - " + cipher.getAlgorithm()); CipherOutputStream cos = new CipherOutputStream(writer.getContentOutputStream(), cipher); return cos; } catch (NoSuchPaddingException e) { logger.warn("[EncryptingContentWriterDecorator::getContentOutputStream] Invalid padding: " + transformationSpec.getPadding()); throw new EncryptionRuntimeException("Invalid padding: " + transformationSpec.getPadding(), e); } catch (NoSuchAlgorithmException e) { logger.warn("[EncryptingContentWriterDecorator::getContentOutputStream] Invalid algorithm: " + transformationSpec.getAlgorithm()); throw new EncryptionRuntimeException("Invalid algorithm: " + transformationSpec.getAlgorithm(), e); } catch (InvalidKeyException e) { logger.warn("[EncryptingContentWriterDecorator::getContentOutputStream] Invalid key!"); throw new EncryptionRuntimeException("Invalid key!", e); } catch (InvalidAlgorithmParameterException e) { logger.warn("[EncryptingContentWriterDecorator::getContentOutputStream] Invalid algorithm parameter: " + iv); throw new EncryptionRuntimeException("Invalid algorithm parameter: " + iv, e); } finally { logger.debug("[EncryptingContentWriterDecorator::getContentOutputStream] END"); } }
From source file:it.doqui.index.ecmengine.business.personalization.encryption.content.DecryptingContentReaderDecorator.java
public InputStream getContentInputStream() throws ContentIOException { logger.debug("[DecryptingContentReaderDecorator::getContentInputStream] BEGIN"); IvParameterSpec iv = null;//from w ww. j av a 2 s. com try { Cipher cipher = null; try { cipher = Cipher.getInstance(CryptoTransformationSpec.buildTransformationString(transformationSpec), "SunJCE"); } catch (NoSuchProviderException e) { logger.warn( "[DecryptingContentReaderDecorator::getContentInputStream] Unknown provider \"SunJCE\". Using default..."); cipher = Cipher.getInstance(CryptoTransformationSpec.buildTransformationString(transformationSpec)); } if (transformationSpec.getMode() != null && !transformationSpec.getMode().equalsIgnoreCase("ECB")) { iv = new IvParameterSpec(transformationSpec.getIv()); logger.debug("[DecryptingContentReaderDecorator::getContentInputStream] IvParameterSpec: " + iv); cipher.init(Cipher.DECRYPT_MODE, secretKey, iv); } else { cipher.init(Cipher.DECRYPT_MODE, secretKey); } logger.debug("[DecryptingContentReaderDecorator::getContentInputStream] " + "Cipher initialized: DECRYPT - " + cipher.getProvider() + " - " + cipher.getAlgorithm()); CipherInputStream cis = new CipherInputStream(reader.getContentInputStream(), cipher); return cis; } catch (NoSuchPaddingException e) { logger.warn("[DecryptingContentReaderDecorator::getContentInputStream] Invalid padding: " + transformationSpec.getPadding()); throw new EncryptionRuntimeException("Invalid padding: " + transformationSpec.getPadding(), e); } catch (NoSuchAlgorithmException e) { logger.warn("[DecryptingContentReaderDecorator::getContentInputStream] Invalid algorithm: " + transformationSpec.getAlgorithm()); throw new EncryptionRuntimeException("Invalid algorithm: " + transformationSpec.getAlgorithm(), e); } catch (InvalidKeyException e) { logger.warn("[DecryptingContentReaderDecorator::getContentInputStream] Invalid key!"); throw new EncryptionRuntimeException("Invalid key!", e); } catch (InvalidAlgorithmParameterException e) { logger.warn( "[DecryptingContentReaderDecorator::getContentInputStream] Invalid algorithm parameter: " + iv); throw new EncryptionRuntimeException("Invalid algorithm parameter: " + iv, e); } finally { logger.debug("[DecryptingContentReaderDecorator::getContentInputStream] END"); } }
From source file:org.jumpmind.security.SecurityService.java
protected Cipher getCipher(int mode) throws Exception { if (secretKey == null) { secretKey = getSecretKey();// ww w .j a v a2 s.c om } Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm()); initializeCipher(cipher, mode); log.debug("Using {} algorithm provided by {}.", cipher.getAlgorithm(), cipher.getProvider().getName()); return cipher; }
From source file:org.jumpmind.security.SecurityService.java
protected void initializeCipher(Cipher cipher, int mode) throws Exception { AlgorithmParameterSpec paramSpec = Cipher.getMaxAllowedParameterSpec(cipher.getAlgorithm()); if (paramSpec instanceof PBEParameterSpec || (paramSpec == null && cipher.getAlgorithm().startsWith("PBE"))) { paramSpec = new PBEParameterSpec(SecurityConstants.SALT, SecurityConstants.ITERATION_COUNT); cipher.init(mode, secretKey, paramSpec); } else if (paramSpec instanceof IvParameterSpec) { paramSpec = new IvParameterSpec(SecurityConstants.SALT); cipher.init(mode, secretKey, paramSpec); } else {//w w w. jav a2 s. c o m cipher.init(mode, secretKey, (AlgorithmParameterSpec) null); } }