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:hjow.hgtable.util.SecurityUtil.java
/** * <p>? .</p>/*from ww w . j a v a2 s . c o m*/ * * @param text : ?? ? ? * @param key : ? ? * @param algorithm : (null AES ) * @return ? ? */ public static String encrypt(String text, String key, String algorithm) { try { String passwords = hash(key, null); String methods = algorithm; if (methods == null) methods = "AES"; String paddings; int need_keySize = -1; boolean useIv = false; byte[] befores = text.trim().getBytes("UTF-8"); byte[] keyByte = passwords.getBytes("UTF-8"); if (methods.equalsIgnoreCase("DES")) { paddings = "DES/CBC/PKCS5Padding"; need_keySize = 8; useIv = true; } else if (methods.equalsIgnoreCase("DESede")) { paddings = "TripleDES/ECB/PKCS5Padding"; need_keySize = 24; useIv = true; } else if (methods.equalsIgnoreCase("AES")) { paddings = "AES"; need_keySize = 16; useIv = false; } else return null; byte[] checkKeyByte = new byte[need_keySize]; byte[] ivBytes = new byte[checkKeyByte.length]; for (int i = 0; i < checkKeyByte.length; i++) { if (i < keyByte.length) { checkKeyByte[i] = keyByte[i]; } else { checkKeyByte[i] = 0; } } keyByte = checkKeyByte; SecretKeySpec keySpec = new SecretKeySpec(keyByte, algorithm); IvParameterSpec ivSpec = null; if (useIv) ivSpec = new IvParameterSpec(ivBytes); Cipher cipher = null; byte[] outputs; try { cipher = Cipher.getInstance(paddings); if (useIv) { cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); } else { cipher.init(Cipher.ENCRYPT_MODE, keySpec); } outputs = new byte[cipher.getOutputSize(befores.length)]; for (int i = 0; i < outputs.length; i++) { outputs[i] = 0; } int enc_len = cipher.update(befores, 0, befores.length, outputs, 0); enc_len = enc_len + cipher.doFinal(outputs, enc_len); return new String(Base64.encodeBase64(outputs), "UTF-8"); } catch (Throwable e) { Main.logError(e, Manager.applyStringTable("On encrypting")); return null; } } catch (Throwable e) { } return null; }
From source file:com.cfs.util.AESCriptografia.java
public String cifrar(String textoOriginal, String chave) { try {//from w ww. ja v a2 s. c o m byte[] original = textoOriginal.getBytes(); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, gerarChave(chave)); byte[] cifrada = cipher.doFinal(original); byte[] retorno = Base64.encodeBase64(cifrada); return new String(retorno); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:cn.util.RSAUtils.java
/** * ,?116,?#/*from w w w. ja v a 2 s . c o m*/ * * @param data * @param publicKey * @return * @throws Exception */ public static String encryptByPublicKey(String data) throws Exception { if (null == data) { return null; } RSAPublicKey publicKey = getPublicKey(); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); // int key_len = publicKey.getModulus().bitLength() / 8; // ? <= -12116 50 String[] datas = null; if (data.getBytes().length > key_len - 12) { datas = splitString(data, 30); } else { datas = new String[1]; datas[0] = data; } String mi = ""; //-11? for (int i = 0; i < datas.length; i++) { if (i < datas.length - 1) { mi += Base64Util.encryptBASE64(cipher.doFinal(datas[i].getBytes())) + "#"; } else { mi += Base64Util.encryptBASE64(cipher.doFinal(datas[i].getBytes())); } } //\r\n mi = mi.replace("\r", ""); mi = mi.replace("\n", ""); return mi; }
From source file:com.lling.qiqu.utils.AesUtils.java
/** * Encrypts a message using a 128bit Bse64 key using AES. * * @param key 128bit Base64 AES key// w ww . j av a 2 s .com * @param message Message to encrypt * @return bse64 encoded encrypted message */ public static String encryptBase64(String key, String message) { String encrypted = ""; try { // Generate the secret key specs. byte[] keyArray = Base64.decodeBase64(key.getBytes()); SecretKeySpec skeySpec = new SecretKeySpec(keyArray, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypt = cipher.doFinal(message.getBytes()); encrypted = new String(Base64.encodeBase64(encrypt)); } catch (Exception e) { logger.error(e.getMessage(), e); throw new RuntimeException(e); } return encrypted; }
From source file:com.formkiq.core.service.crypto.SecureTokenServiceImpl.java
@Override public String encryptToken(final PrivateKey privateKey, final String token) throws GeneralSecurityException { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, privateKey); byte[] bytes = Strings.getBytes(token); return Base64.getEncoder().encodeToString(cipher.doFinal(bytes)); }
From source file:ie.peternagy.jcrypto.algo.EllipticCurveWrapper.java
/** * Initialize the cipher for task//from w w w . j a v a2s. c o m * * @param isEncrypt */ private void initCipher(boolean isEncrypt) { try { if (isEncrypt) { ecCipher.init(Cipher.ENCRYPT_MODE, publicKey); } else { ecCipher.init(Cipher.DECRYPT_MODE, privateKey); } } catch (InvalidKeyException ex) { Logger.getLogger(EllipticCurveWrapper.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:info.fcrp.keepitsafe.bean.CryptBeanTest.java
@Test public void symetric() throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { KeyGenerator kg = KeyGenerator.getInstance("AES"); kg.init(256, new SecureRandom()); Key k = kg.generateKey();//from w ww .j av a 2 s . co m Cipher c = Cipher.getInstance("AES"); String plain = "plain"; byte[] plainBytes = plain.getBytes(); c.init(Cipher.ENCRYPT_MODE, k); c.update(plainBytes); byte[] encBytes = c.doFinal(); String enc = Base64.encodeBase64String(encBytes); assertNotSame(plain, enc); c.init(Cipher.DECRYPT_MODE, k); c.update(encBytes); byte[] decBytes = c.doFinal(); String dec = new String(decBytes); assertEquals(plain, dec); }
From source file:dz.alkhwarizmix.framework.java.utils.CryptoUtil.java
/** * encryptString//from w ww .j a v a2s. com */ public final String encryptString(String stringToEncrypt) { String result = null; byte[] dataToEncrypt = null; byte[] encrypted = null; dataToEncrypt = hex2Byte(stringToHex(stringToEncrypt)); try { cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec); encrypted = cipher.doFinal(dataToEncrypt); result = byte2hex(encrypted); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } return result; }
From source file:com.aqnote.shared.cryptology.symmetric.DES.java
private static void generateCipher(String rawKey) { try {/*ww w. ja va2 s . co m*/ DESKeySpec dks = new DESKeySpec(rawKey.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); SecretKey deskey = keyFactory.generateSecret(dks); encodeCipher = Cipher.getInstance(ALGORITHM); encodeCipher.init(Cipher.ENCRYPT_MODE, deskey); decodeCipher = Cipher.getInstance(ALGORITHM); decodeCipher.init(Cipher.DECRYPT_MODE, deskey); } catch (InvalidKeyException e) { throw new RuntimeException(e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (InvalidKeySpecException e) { throw new RuntimeException(e); } catch (NoSuchPaddingException e) { throw new RuntimeException(e); } }
From source file:com.forsrc.utils.MyRsa2Utils.java
/** * Encrypt string.//ww w. ja v a 2 s . com * * @param publicKey the public key * @param plaintext the plaintext * @return the string * @throws RsaException the rsa exception */ public static String encrypt(PublicKey publicKey, String plaintext) throws RsaException { Cipher cipher = null; try { cipher = Cipher.getInstance(RsaKey.ALGORITHM, new org.bouncycastle.jce.provider.BouncyCastleProvider()); } catch (NoSuchAlgorithmException e) { throw new RsaException(e); } catch (NoSuchPaddingException e) { throw new RsaException(e); } try { cipher.init(Cipher.ENCRYPT_MODE, publicKey); } catch (InvalidKeyException e) { throw new RsaException(e); } byte[] data = plaintext.getBytes(); int blockSize = cipher.getBlockSize(); blockSize = blockSize == 0 ? 117 : blockSize; int outputSize = cipher.getOutputSize(data.length); int count = (int) Math.ceil(data.length / blockSize) + 1; byte[] output = new byte[outputSize * count]; try { int i = 0; int start = 0; int outputStart = 0; do { start = i * blockSize; outputStart = i * outputSize; if (data.length - start >= blockSize) { cipher.doFinal(data, start, blockSize, output, outputStart); } else { cipher.doFinal(data, start, data.length - start, output, outputStart); } i++; } while (data.length - start - blockSize >= 0); } catch (IllegalBlockSizeException e) { throw new RsaException(e); } catch (BadPaddingException e) { throw new RsaException(e); } catch (ShortBufferException e) { throw new RsaException(e); } return new String(new Base64().encode(output)); }