List of usage examples for javax.crypto Cipher DECRYPT_MODE
int DECRYPT_MODE
To view the source code for javax.crypto Cipher DECRYPT_MODE.
Click Source Link
From source file:net.fender.crypto.CryptoUtil.java
/** * @param keyName/*from www. j a va 2 s .c o m*/ * @param base64Encoded * @return * @throws GeneralSecurityException */ public static String decryptUsingSystemPropertyKey(String keyName, String base64Encoded) throws GeneralSecurityException { byte[] encryptedBytes = Base64.decodeBase64(base64Encoded.getBytes()); Key key = getSystemPropertyKey(keyName); Cipher cipher = Cipher.getInstance(key.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, key); byte[] decryptedBytes = cipher.doFinal(encryptedBytes); String decrypted = new String(decryptedBytes); return decrypted; }
From source file:br.com.ufjf.labredes.crypto.Cryptography.java
public static Object decrypt(String encryptedObject, byte[] aesKey) { Object object = null;//from w w w . ja v a 2s . c om SecretKeySpec secretKey = new SecretKeySpec(aesKey, ALGORITHM_SYM); try { byte[] cipherText = new BASE64Decoder().decodeBuffer(encryptedObject); Cipher cipher = Cipher.getInstance(ALGORITHM_SYM); cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] serialized_obj = cipher.doFinal(cipherText); object = (Object) SerializationUtils.deserialize(serialized_obj); } catch (Exception ex) { ex.printStackTrace(); } return object; }
From source file:bobs.is.compress.sevenzip.AES256SHA256Decoder.java
@Override InputStream decode(final String archiveName, final InputStream in, final long uncompressedLength, final Coder coder, final byte[] passwordBytes) throws IOException { return new InputStream() { private boolean isInitialized = false; private CipherInputStream cipherInputStream = null; private CipherInputStream init() throws IOException { if (isInitialized) { return cipherInputStream; }// w w w. j a v a2s . co m final int byte0 = 0xff & coder.properties[0]; final int numCyclesPower = byte0 & 0x3f; final int byte1 = 0xff & coder.properties[1]; final int ivSize = ((byte0 >> 6) & 1) + (byte1 & 0x0f); final int saltSize = ((byte0 >> 7) & 1) + (byte1 >> 4); if (2 + saltSize + ivSize > coder.properties.length) { throw new IOException("Salt size + IV size too long in " + archiveName); } final byte[] salt = new byte[saltSize]; System.arraycopy(coder.properties, 2, salt, 0, saltSize); final byte[] iv = new byte[16]; System.arraycopy(coder.properties, 2 + saltSize, iv, 0, ivSize); if (passwordBytes == null) { throw new PasswordRequiredException(archiveName); } final byte[] aesKeyBytes; if (numCyclesPower == 0x3f) { aesKeyBytes = new byte[32]; System.arraycopy(salt, 0, aesKeyBytes, 0, saltSize); System.arraycopy(passwordBytes, 0, aesKeyBytes, saltSize, Math.min(passwordBytes.length, aesKeyBytes.length - saltSize)); } else { final MessageDigest digest; try { digest = MessageDigest.getInstance("SHA-256"); } catch (final NoSuchAlgorithmException noSuchAlgorithmException) { throw new IOException("SHA-256 is unsupported by your Java implementation", noSuchAlgorithmException); } final byte[] extra = new byte[8]; for (long j = 0; j < (1L << numCyclesPower); j++) { digest.update(salt); digest.update(passwordBytes); digest.update(extra); for (int k = 0; k < extra.length; k++) { ++extra[k]; if (extra[k] != 0) { break; } } } aesKeyBytes = digest.digest(); } final SecretKey aesKey = new SecretKeySpec(aesKeyBytes, "AES"); try { final Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, aesKey, new IvParameterSpec(iv)); cipherInputStream = new CipherInputStream(in, cipher); isInitialized = true; return cipherInputStream; } catch (final GeneralSecurityException generalSecurityException) { throw new IOException("Decryption error " + "(do you have the JCE Unlimited Strength Jurisdiction Policy Files installed?)", generalSecurityException); } } @Override public int read() throws IOException { return init().read(); } @Override public int read(final byte[] b, final int off, final int len) throws IOException { return init().read(b, off, len); } @Override public void close() { } }; }
From source file:com.ubipass.middleware.web.action.LicenceMgtAction.java
private String getDate(String userName, String licenceKey) throws Exception { // DES????/* w w w . jav a 2 s .c om*/ SecureRandom sr = new SecureRandom(); byte rawKeyData[] = (userName + "midware").getBytes(); // ?DESKeySpec DESKeySpec dks = new DESKeySpec(rawKeyData); // ?DESKeySpec?? // SecretKey SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(dks); // Cipher?? Cipher cipher = Cipher.getInstance("DES"); // ?Cipher cipher.init(Cipher.DECRYPT_MODE, key, sr); // ?? licenceKey = new String(cipher.doFinal(Base64.decodeBase64(licenceKey.getBytes()))); String[] tmpStr = licenceKey.split("-"); if (tmpStr.length == 2) return tmpStr[1]; throw new InvalidLicenseException(); }
From source file:com.titilink.common.app.EncryptDecryptUtil.java
public void testDES() throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException { ///*from w ww. j a va2 s. c om*/ DESKeySpec desKeySpec = new DESKeySpec("SECURITY".getBytes(Charset.forName("UTF-8"))); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec); //?? Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey, new SecureRandom()); byte[] cipherData = cipher .doFinal("this is a security text from server".getBytes(Charset.forName("UTF-8"))); //? Cipher cipher1 = Cipher.getInstance("DES"); cipher1.init(Cipher.DECRYPT_MODE, secretKey, new SecureRandom()); byte[] plainData = cipher1.doFinal(cipherData); System.out.println(new String(plainData, Charset.forName("UTF-8"))); }
From source file:net.thewaffleshop.nimbus.api.EncryptionAPI.java
/** * Decrypt/*from ww w.jav a 2 s . c o m*/ * * @param secretKey * @param iv * @param secret * @return */ public byte[] decrypt(SecretKey secretKey, byte[] iv, byte[] secret) { try { IvParameterSpec ivSpec = new IvParameterSpec(iv); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec); byte[] ret = cipher.doFinal(secret); return ret; } catch (GeneralSecurityException e) { throw new RuntimeException(e); } }
From source file:models.logic.CipherDecipher.java
public static void encryptOrDecrypt(SecretKey key, int mode, InputStream is, OutputStream os) throws Throwable { Cipher cipher = Cipher.getInstance("AES"); if (mode == Cipher.ENCRYPT_MODE) { cipher.init(Cipher.ENCRYPT_MODE, key); CipherInputStream cis = new CipherInputStream(is, cipher); doCopy(cis, os);// w w w. ja va 2 s. co m } else if (mode == Cipher.DECRYPT_MODE) { cipher.init(Cipher.DECRYPT_MODE, key); CipherOutputStream cos = new CipherOutputStream(os, cipher); doCopy(is, cos); } }
From source file:com.shenit.commons.codec.RsaUtils.java
/** * //w w w .j a v a2s.c o m * * @param content * * @param privateKey * ? * @param inputCharset * ?? * @return ? */ public static String decrypt(String content, String privateKey, String inputCharset) { InputStream ins = null; ByteArrayOutputStream writer = null; String result = null; try { PrivateKey prikey = getPrivateKey(privateKey); Cipher cipher = Cipher.getInstance(CODEC_RSA); cipher.init(Cipher.DECRYPT_MODE, prikey); ins = new ByteArrayInputStream(Base64.decodeBase64(content)); writer = new ByteArrayOutputStream(); // rsa?128?128? byte[] buf = new byte[128]; int bufl; while ((bufl = ins.read(buf)) != -1) { byte[] block = null; if (buf.length == bufl) { block = buf; } else { block = new byte[bufl]; for (int i = 0; i < bufl; i++) { block[i] = buf[i]; } } writer.write(cipher.doFinal(block)); } result = new String(writer.toByteArray(), inputCharset); } catch (Exception ex) { if (LOG.isWarnEnabled()) LOG.warn("[decrypt] Decrypt failed with exception.", ex); } finally { IOUtils.closeQuietly(writer); IOUtils.closeQuietly(ins); } return result; }
From source file:io.stallion.utils.Encrypter.java
private static String doDecryptString(String password, String encryptedBase32) throws Exception { encryptedBase32 = StringUtils.strip(encryptedBase32, "="); String salt = encryptedBase32.substring(0, 16); String ivString = encryptedBase32.substring(16, 48); byte[] iv = new byte[0]; try {/*from w w w . j a v a2s . c o m*/ iv = Hex.decodeHex(ivString.toCharArray()); } catch (DecoderException e) { throw new RuntimeException(e); } encryptedBase32 = encryptedBase32.substring(48); Base32 decoder = new Base32(); byte[] encrypted = decoder.decode(encryptedBase32.toUpperCase()); SecretKeySpec skeySpec = makeKeySpec(password, salt); /* Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(iv)); */ Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, skeySpec, new GCMParameterSpec(128, iv)); byte[] original = cipher.doFinal(encrypted); return new String(original, Charset.forName("UTF-8")); }
From source file:mitm.common.security.crypto.PBDecryptionInputStream.java
private void init(String algorithm, byte[] salt, int iterationCount) throws CryptoException { try {/*from w w w . j a v a 2 s. co m*/ SecurityFactory securityFactory = SecurityFactoryFactory.getSecurityFactory(); SecretKeyFactory keyFactory = securityFactory.createSecretKeyFactory(algorithm); PBEKeySpec keySpec = new PBEKeySpec(password, salt, iterationCount); /* * Clear out the password */ Arrays.fill(password, '#'); Key secretKey = keyFactory.generateSecret(keySpec); cipher = securityFactory.createCipher(algorithm); cipher.init(Cipher.DECRYPT_MODE, secretKey); } catch (NoSuchProviderException e) { throw new NoSuchProviderRuntimeException(e); } catch (NoSuchAlgorithmException e) { throw new CryptoException(e); } catch (InvalidKeySpecException e) { throw new CryptoException(e); } catch (NoSuchPaddingException e) { throw new CryptoException(e); } catch (InvalidKeyException e) { throw new CryptoException(e); } }