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:hudson.util.Protector.java
public static String protect(String secret) { try {// www.jav a 2 s . c o m Cipher cipher = Secret.getCipher(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, DES_KEY); return new String(Base64.encodeBase64(cipher.doFinal((secret + MAGIC).getBytes("UTF-8")))); } catch (GeneralSecurityException e) { throw new Error(e); // impossible } catch (UnsupportedEncodingException e) { throw new Error(e); // impossible } }
From source file:Main.java
/** * Encrypts message string using a given symmetric key. * @param msg Message string to encrypt. * @param key Key to encrypt message with. * @return Byte array of encrypted and encoded message. * @throws NoSuchAlgorithmException/* w w w .ja v a 2 s.co m*/ * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws IllegalBlockSizeException * @throws BadPaddingException * @throws InvalidAlgorithmParameterException */ public static byte[] encryptMessage(String msg, SecretKey key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException { Cipher cipher = Cipher.getInstance("AES"); byte[] init = new byte[128 / 8]; //SecureRandom secureRandom = new SecureRandom(); //secureRandom.nextBytes(init); for (int i = 0; i < 16; i++) init[i] = 0; cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(init)); byte[] msgBytes = msg.getBytes(); //System.out.println(android.util.Base64.encode(msgBytes, Base64.DEFAULT)); byte[] msgCipherBytes = cipher.doFinal(msgBytes); return msgCipherBytes; }
From source file:Main.java
public static byte[] encryptMsg(String msg, RSAPublicKeySpec pubKeySpec) { if (msg != null && pubKeySpec != null && !msg.isEmpty()) { try {//from www .j a v a2s .c o m Log.w(TAG, "msg is: " + msg + " with length " + msg.length()); KeyFactory fact = KeyFactory.getInstance("RSA"); PublicKey pubKey = fact.generatePublic(pubKeySpec); // TODO encrypt the message and send it // Cipher cipher = Cipher.getInstance("RSA/None/NoPadding"); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); // Cipher cipher = // Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding", // "BC"); cipher.init(Cipher.ENCRYPT_MODE, pubKey); Log.d(TAG, "cipher block size is " + cipher.getBlockSize()); byte[] msgByteArray = msg.getBytes(); byte[] cipherData = new byte[cipher.getOutputSize(msgByteArray.length)]; cipherData = cipher.doFinal(msgByteArray); Log.d(TAG, "output size is " + cipher.getOutputSize(msgByteArray.length)); Log.d(TAG, "is the measurement already broken into chunks here? " + (new String(cipherData))); return cipherData; } catch (NoSuchAlgorithmException e) { Log.e(TAG, "RSA algorithm not available", e); } catch (InvalidKeySpecException e) { Log.e(TAG, "", e); } catch (NoSuchPaddingException e) { Log.e(TAG, "", e); } catch (InvalidKeyException e) { Log.e(TAG, "", e); } catch (BadPaddingException e) { Log.e(TAG, "", e); } catch (IllegalBlockSizeException e) { Log.e(TAG, "", e); } catch (Exception e) { Log.e(TAG, "", e); } /* * catch (NoSuchProviderException e) { Log.e(TAG, "", e); } */ } return null; }
From source file:com.aerohive.nms.engine.admin.task.licensemgr.license.processor2.PacketUtil.java
private static byte[] encryptData(byte[] content) { byte[] outBytes = null; try {/*from w w w . jav a 2 s .c om*/ Key key = new SecretKeySpec(secret_key, "DESede"); Cipher cipher = Cipher.getInstance("DESede", "SunJCE"); cipher.init(Cipher.ENCRYPT_MODE, key, cipher.getParameters()); outBytes = cipher.doFinal(content); } catch (Exception ex) { //log.error("PacketUtil",ex.getMessage(), ex); return outBytes; } return outBytes; }
From source file:com.example.license.RSAUtil.java
/** * ?/*from ww w .ja v a 2 s . co m*/ * * @param data * ? * @param key * * @return ?? */ public static String encrypt(String data, String seed) throws Exception { KeyPair keyPair = generatorKeyPair(seed); // Cipher?? Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); // SecureRandom random = new SecureRandom(); // ?Cipher? cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPrivate()); byte[] results = cipher.doFinal(data.getBytes()); // http://tripledes.online-domain-tools.com/?? for (int i = 0; i < results.length; i++) { System.out.print(results[i] + " "); } System.out.println(); // ??Base64? return Base64.encodeBase64String(results); }
From source file:Main.java
public static byte[] encipherAes256(byte[] clearText, String keyString) throws NullPointerException { if (keyString == null || keyString.length() == 0) { throw new NullPointerException("Please give Password"); }//from w ww .j av a 2 s . c om if (clearText == null || clearText.length <= 0) { throw new NullPointerException("Please give clearText"); } try { SecretKeySpec skeySpec = getKey(keyString); // IMPORTANT TO GET SAME RESULTS ON iOS and ANDROID final byte[] iv = new byte[16]; Arrays.fill(iv, (byte) 0x00); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); // Cipher is not thread safe Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec); return cipher.doFinal(clearText); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } return null; }
From source file:com.juicioenlinea.application.secutiry.Security.java
public static String encriptar(String texto) { final String secretKey = "hunter"; //llave para encriptar datos String encryptedString = null; try {/*from w w w.j a v a 2s . c o m*/ MessageDigest md = MessageDigest.getInstance("SHA"); byte[] digestOfPassword = md.digest(secretKey.getBytes("UTF-8")); byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); SecretKey key = new SecretKeySpec(keyBytes, "DESede"); Cipher cipher = Cipher.getInstance("DESede"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] plainTextBytes = texto.getBytes("UTF-8"); byte[] buf = cipher.doFinal(plainTextBytes); byte[] base64Bytes = Base64.encodeBase64(buf); encryptedString = new String(base64Bytes); } catch (NoSuchAlgorithmException | UnsupportedEncodingException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) { Logger.getLogger(Security.class.getName()).log(Level.SEVERE, null, ex); } return encryptedString; }
From source file:com.doculibre.constellio.utils.aes.SimpleProtector.java
public static String encrypt(String valueToEnc) throws Exception { Key key = generateKey();/* w w w . ja v a 2 s . com*/ Cipher c = Cipher.getInstance(ALGORITHM); c.init(Cipher.ENCRYPT_MODE, key); byte[] encValue = c.doFinal(valueToEnc.getBytes()); String encryptedValue = Base64.encodeBase64String(encValue); return encryptedValue; }
From source file:com.lingxiang2014.util.RSAUtils.java
public static byte[] encrypt(PublicKey publicKey, byte[] data) { Assert.notNull(publicKey);//ww w . j a v a 2s. co m Assert.notNull(data); try { Cipher cipher = Cipher.getInstance("RSA", PROVIDER); cipher.init(Cipher.ENCRYPT_MODE, publicKey); return cipher.doFinal(data); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:com.java.demo.DesDemo.java
public static byte[] Encrypt(String str) { try {/*from w w w . jav a 2 s. com*/ Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey); byte[] result = cipher.doFinal(str.getBytes()); return result; } catch (Exception e) { return null; } }