List of usage examples for javax.crypto.spec SecretKeySpec SecretKeySpec
public SecretKeySpec(byte[] key, String algorithm)
From source file:Main.java
public static String encryption(String string) throws Exception { // TODO Auto-generated method stub //generate symmetric key KeyGenerator keygt = KeyGenerator.getInstance("AES"); keygt.init(128);/*from w w w. ja va 2 s . c o m*/ SecretKey symkey = keygt.generateKey(); //get it encoded byte[] aes_ba = symkey.getEncoded(); //create cipher SecretKeySpec skeySpec = new SecretKeySpec(aes_ba, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); //encryption byte[] EncSymbyteArray = cipher.doFinal(string.getBytes()); //encrypt symKey with PublicKey // Key pubKey = getPublicKey(); Key pubKey = publicKey; //RSA cipher Cipher cipherAsm = Cipher.getInstance("RSA", "BC"); cipherAsm.init(Cipher.ENCRYPT_MODE, pubKey); //RSA encryption byte[] asymEncsymKey = cipherAsm.doFinal(aes_ba); // File f3 = new File(BASE_PATH,"enc.txt"); // File f3key = new File(BASE_PATH,"enckey.txt"); // File f3file = new File(BASE_PATH,"encfile.txt"); // writeToFile2(f3,f3key,f3file, asymEncsymKey, EncSymbyteArray); //byte != new String //return new String(byteMerger(asymEncsymKey, EncSymbyteArray)); return Base64.encodeToString(byteMerger(asymEncsymKey, EncSymbyteArray), Base64.DEFAULT); }
From source file:authentication.AES.java
public static String encrypt(String textopuro) throws Exception { Cipher encripta = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE"); SecretKeySpec key = new SecretKeySpec(chaveencriptacao.getBytes("UTF-8"), "AES"); encripta.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8"))); byte[] a = encripta.doFinal(textopuro.getBytes("UTF-8")); return StringUtils.newStringUtf8(Base64.encodeBase64(a, false)); }
From source file:EncDec.AES.java
public static String encrypt(String key, String initVector, String value) { try {/*w w w. j a v a 2 s . c om*/ IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8")); SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = cipher.doFinal(value.getBytes()); //System.out.println("encrypted string: " // + Base64.encodeBase64String(encrypted)); return Base64.encodeBase64String(encrypted); } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:net.foreworld.util.RestUtil.java
public static String genSignature(String data, String key) { byte[] byteHMAC = null; try {/*from w ww . ja va 2 s.co m*/ Mac mac = Mac.getInstance(ALGORITHM); SecretKeySpec spec = new SecretKeySpec(key.getBytes(), ALGORITHM); mac.init(spec); byteHMAC = mac.doFinal(data.toLowerCase(Locale.getDefault()).getBytes()); } catch (InvalidKeyException ignore) { return null; } catch (NoSuchAlgorithmException ignore) { return null; } // END if (null == byteHMAC) return null; // String code = new BASE64Encoder().encode(byteHMAC); String code = Base64.encodeBase64String(byteHMAC); try { return URLEncoder.encode(code, ENC); } catch (UnsupportedEncodingException ignore) { } return null; }
From source file:clases.Seguridad.java
public static String encriptar(String cleartext) throws Exception { Cipher cipher = Cipher.getInstance(CI); SecretKeySpec skeySpec = new SecretKeySpec(KEY.getBytes(), ALG); IvParameterSpec ivParameterSpec = new IvParameterSpec(IV.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec); byte[] encrypted = cipher.doFinal(cleartext.getBytes()); return new String(encodeBase64(encrypted)); }
From source file:BD.Encriptador.java
public static String Encriptar(String texto) { String secretKey = "qualityinfosolutions"; //llave para encriptar datos String base64EncryptedString = ""; try {//from ww w . j a va 2 s. c om MessageDigest md = MessageDigest.getInstance("MD5"); 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); base64EncryptedString = new String(base64Bytes); } catch (Exception ex) { } return base64EncryptedString; }
From source file:controlpac.EncryptHelper.java
public static String Encriptar(String texto) { String base64EncryptedString = ""; try {// w w w .ja va 2s.com MessageDigest md = MessageDigest.getInstance("MD5"); //Crea un hash con la clave elegida byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8")); byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); SecretKey key = new SecretKeySpec(keyBytes, "DESede"); //Crea la clave Cipher cipher = Cipher.getInstance("DESede"); cipher.init(Cipher.ENCRYPT_MODE, key);//Inicializa el cifrado con la clave byte[] plainTextBytes = texto.getBytes("utf-8"); byte[] buf = cipher.doFinal(plainTextBytes);//Cifra el texto byte[] base64Bytes = Base64.encodeBase64(buf);//Encodea el texto en base64 base64EncryptedString = new String(base64Bytes); } catch (Exception ex) { } return base64EncryptedString; }
From source file:com.fpt.crypto.CipherDemo.java
public static String encrypt(String in, String key) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException { Cipher cipher = Cipher.getInstance("AES"); byte[] inputBytes = in.getBytes(); SecretKey secKey = new SecretKeySpec(key.getBytes(), "AES"); cipher.init(Cipher.ENCRYPT_MODE, secKey); byte[] outputBytes = cipher.doFinal(inputBytes); return Hex.encodeHexString(outputBytes); }
From source file:Controller.StringEncrypt.java
public final static String encrypt(String cleartext) { byte[] encrypted = null; try {/*w w w . j av a2s.c om*/ Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec skeySpec = new SecretKeySpec("92AE31A79FEEB2A3".getBytes(), "AES"); IvParameterSpec ivParameterSpec = new IvParameterSpec("0123456789ABCDEF".getBytes()); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec); encrypted = cipher.doFinal(cleartext.getBytes()); } catch (InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException e) { JOptionPane.showMessageDialog(null, "Error encriptando contrasea"); } return new String(encodeBase64(encrypted)); }
From source file:Main.java
public static byte[] aesIGEdecrypt(byte[] tmpAESiv, byte[] tmpAesKey, byte[] data) { try {//www . j ava 2 s .c o m ByteBuffer out = ByteBuffer.allocate(data.length); byte[] iv2p = Arrays.copyOfRange(tmpAESiv, 0, tmpAESiv.length / 2); byte[] ivp = Arrays.copyOfRange(tmpAESiv, tmpAESiv.length / 2, tmpAESiv.length); int len = data.length / AES_BLOCK_SIZE; byte[] xorInput = null; byte[] xorOutput = null; SecretKeySpec keySpec = null; keySpec = new SecretKeySpec(tmpAesKey, "AES"); Cipher cipher = null; cipher = Cipher.getInstance("AES/ECB/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, keySpec); byte[] input = null; byte[] output = null; for (int i = 0; i < len; i++) { input = Arrays.copyOfRange(data, i * AES_BLOCK_SIZE, (i + 1) * AES_BLOCK_SIZE); xorInput = xor(input, ivp); output = cipher.doFinal(xorInput); xorOutput = xor(output, iv2p); out.put(xorOutput); ivp = xorOutput; iv2p = input; } return out.array(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } return null; }