List of usage examples for javax.crypto Cipher ENCRYPT_MODE
int ENCRYPT_MODE
To view the source code for javax.crypto Cipher ENCRYPT_MODE.
Click Source Link
From source file:com.cherong.mock.common.base.util.EncryptionUtil.java
/** * deskey/*from w w w.j a va 2 s . c om*/ * * @param content * @param key * @return * @throws Exception */ public static byte[] encryptByDES(String content, String key) { try { DESKeySpec dks = new DESKeySpec(key.getBytes()); SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); SecretKey sk = skf.generateSecret(dks); Cipher cip = Cipher.getInstance(DES_CIPHER_ALGORITHM); cip.init(Cipher.ENCRYPT_MODE, sk); return cip.doFinal(content.getBytes()); } catch (Exception e) { LOGGER.error("{}", e); return null; } }
From source file:br.com.vectorx.BlowfishCryptox.java
/** * Construtor private. Define o tamanho mximo da palavra salt de acordo com * o SecurityPolice instalado/*from w w w . j av a2 s . c o m*/ * * @param palavraChave * Palavra chave * @param algoritmo * Algoritmo a ser utilizado (no caso o Blowfish) * @param cifraCesar * em quantos numeros os caracteres sero trocados * @param charset * @throws IllegalArgumentException */ private BlowfishCryptox(String palavraChave, String algoritmo, int cifraCesar, Charset charset) throws IllegalArgumentException { try { int maxSalt = LIMITEDSALTLENGTH; // Checagem para ver se tem JCE Unlimited Strength Policy instalado if (Cipher.getMaxAllowedKeyLength(algoritmo) > BlowfishCryptox.CIFRAMAX) { maxSalt = BlowfishCryptox.UNLIMITEDJCESALT; } validateInput(palavraChave, cifraCesar, maxSalt); this.charset = charset; SecretKey chave = new SecretKeySpec(palavraChave.getBytes(charset), algoritmo); this.encrypt = Cipher.getInstance(algoritmo); this.decrypt = Cipher.getInstance(algoritmo); this.encrypt.init(Cipher.ENCRYPT_MODE, chave); this.decrypt.init(Cipher.DECRYPT_MODE, chave); this.cifraCesar = cifraCesar; } catch (Exception e) { throw new IllegalArgumentException(e); } }
From source file:com.springcryptoutils.core.cipher.symmetric.CiphererWithStaticKeyImpl.java
/** * Encrypts/decrypts a message based on the underlying mode of operation. * * @param message if in encryption mode, the clear-text message, otherwise * the message to decrypt// w w w.j a v a2 s.c o m * @return if in encryption mode, the encrypted message, otherwise the * decrypted message * @throws SymmetricEncryptionException on runtime errors * @see #setMode(Mode) */ public byte[] encrypt(byte[] message) { try { final Cipher cipher = (((provider == null) || (provider.length() == 0)) ? Cipher.getInstance(cipherAlgorithm) : Cipher.getInstance(cipherAlgorithm, provider)); switch (mode) { case ENCRYPT: cipher.init(Cipher.ENCRYPT_MODE, keySpec, initializationVectorSpec); break; case DECRYPT: cipher.init(Cipher.DECRYPT_MODE, keySpec, initializationVectorSpec); break; default: throw new SymmetricEncryptionException( "error encrypting/decrypting message: invalid mode; mode=" + mode); } return cipher.doFinal(message); } catch (Exception e) { throw new SymmetricEncryptionException("error encrypting/decrypting message; mode=" + mode, e); } }
From source file:com.bamboocloud.im.provisioner.json.crypto.simple.SimpleEncryptor.java
/** * Encrypts using an asymmetric cipher.//from www . ja va 2 s . com * * @param value the value to be encrypted. * @return the encrypted value. * @throws GeneralSecurityException if a cryptographic operation failed. * @throws IOException if an I/O exception occurred. */ private Object asymmetric(Object object) throws GeneralSecurityException, IOException { String symmetricCipher = "AES/ECB/PKCS5Padding"; // no IV required for randomly-generated session key KeyGenerator generator = KeyGenerator.getInstance("AES"); generator.init(128); SecretKey sessionKey = generator.generateKey(); Cipher symmetric = Cipher.getInstance(symmetricCipher); symmetric.init(Cipher.ENCRYPT_MODE, sessionKey); String data = Base64.encodeBase64String(symmetric.doFinal(mapper.writeValueAsBytes(object))); Cipher asymmetric = Cipher.getInstance(cipher); asymmetric.init(Cipher.ENCRYPT_MODE, key); HashMap<String, Object> keyObject = new HashMap<String, Object>(); keyObject.put("cipher", this.cipher); keyObject.put("key", this.alias); keyObject.put("data", Base64.encodeBase64String(asymmetric.doFinal(sessionKey.getEncoded()))); HashMap<String, Object> result = new HashMap<String, Object>(); result.put("cipher", symmetricCipher); result.put("key", keyObject); result.put("data", data); return result; }
From source file:org.jasig.cas.web.flow.CasFlowExecutionKeyFactory.java
protected String encrypt(final String value) { if (value == null) { return null; }// www. j a v a 2 s .c o m try { final Cipher cipher = Cipher.getInstance(this.cipherAlgorithm); cipher.init(Cipher.ENCRYPT_MODE, this.key, this.ivs); return byteArrayToHexString(cipher.doFinal(value.getBytes())); } catch (final Exception e) { throw new RuntimeException(e); } }
From source file:com.diona.fileReader.CipherUtil.java
/** * Encrypts the given string (plaintext). * /*from w w w . ja v a 2s .c o m*/ * @param bytes * byte array to be encrypted. * @param context * context to fetch preferences. * @return encrypted byte array. */ @SuppressLint("TrulyRandom") public byte[] encryptBytes(final byte[] bytes, final Context context) { // Transaction.checkLongRunningProcessing("encryptBytes"); if (!ENCRYPTION_ENABLED) { return bytes; } byte[] encryptedTextBytes = null; try { // Derive the secret key final SecretKeySpec secretKey = getSecretKey(context); // encrypt the message final Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM); final IvParameterSpec ivspec = new IvParameterSpec(getIV(context)); cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec); encryptedTextBytes = cipher.doFinal(bytes); } catch (final Exception e) { Log.e(TAG, "" + e); } return encryptedTextBytes; }
From source file:com.aqnote.shared.encrypt.symmetric.Blowfish.java
/** * ?CipherdoFinal?reset ???/*from ww w . j a va 2 s. c o m*/ * * @param b * ? * @return ? * @throws IllegalBlockSizeException * @throws BadPaddingException */ public synchronized static byte[] encrypt(byte[] b) throws IllegalBlockSizeException, BadPaddingException { byte[] buffer = null; initCipher(encryptCipher, Cipher.ENCRYPT_MODE, key, iv); buffer = encryptCipher.doFinal(b); return buffer; }
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 . j a v a 2 s . c om*/ } else if (mode == Cipher.DECRYPT_MODE) { cipher.init(Cipher.DECRYPT_MODE, key); CipherOutputStream cos = new CipherOutputStream(os, cipher); doCopy(is, cos); } }
From source file:JavaTron.JTP.java
/** * Encrypts a string//from w ww . ja v a2 s . co m * @param property * @return An encrypted string */ public static String encrypt(String property) { String p = new String(); try { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD)); Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); p = base64Encode(pbeCipher.doFinal(property.getBytes())); } catch (Exception e) { e.printStackTrace(); } return p; }
From source file:ai.serotonin.backup.Backup.java
private File encryptFile(final File file) throws Exception { final SecureRandom random = new SecureRandom(); final byte[] salt = random.generateSeed(8); final String saltStr = Hex.encodeHexString(salt); final SecretKey secret = createSecretKey(salt); final Cipher cipher = createCipher(); cipher.init(Cipher.ENCRYPT_MODE, secret); final AlgorithmParameters params = cipher.getParameters(); final byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV(); final String ivStr = Hex.encodeHexString(iv); final File encryptedFile = new File(file.getParent(), saltStr + "_" + ivStr + "_" + file.getName()); cipherizeFile(file, encryptedFile, cipher); file.delete();/*from ww w.j a v a 2s.c o m*/ LOG.info("Encrypted backup file to " + encryptedFile.getPath() + ", " + FileUtils.byteCountToDisplaySize(encryptedFile.length())); return encryptedFile; }