List of usage examples for javax.crypto SecretKeyFactory getInstance
public static final SecretKeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:com.asquareb.kaaval.MachineKey.java
/** * Method to encrypt a string. Accepts the string to be encrypted and the * name of the file to store the key vale which can be used for decryption */// w ww . ja va2 s .c o m public static String encrypt(String property, String app) throws IOException, KaavalException { InetAddress ip = null; String ipAddress = null; ObjectOutputStream os = null; NetworkInterface macAddress = null; byte[] macId = null; Cipher pbeCipher = null; Random rand = new Random(); rand.nextBytes(salt); try { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(password)); ip = InetAddress.getLocalHost(); ipAddress = ip.getHostAddress(); macAddress = NetworkInterface.getByInetAddress(ip); macId = macAddress.getHardwareAddress(); MachineKey mKey = new MachineKey(); mKey.api = ipAddress; mKey.macad = new String(macId); mKey.yek = key; mKey.tlas = salt; mKey.eti = rand.nextInt(1000); os = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(app))); os.writeObject(mKey); os.close(); pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(salt, mKey.eti)); return base64Encode(pbeCipher.doFinal(property.getBytes())); } catch (IOException e) { throw new KaavalException(1, "Error in key file during encryption", e); } catch (Exception e) { throw new KaavalException(2, "Errors during encryption", e); } finally { if (os != null) os.close(); } }
From source file:org.wisdom.crypto.CryptoServiceSingleton.java
/** * Generate the AES key from the salt and the private key. * * @param salt the salt (hexadecimal) * @param privateKey the private key//www . j a v a2s . co m * @return the generated key. */ private SecretKey generateAESKey(String privateKey, String salt) { try { byte[] raw = decodeHex(salt); KeySpec spec = new PBEKeySpec(privateKey.toCharArray(), raw, iterationCount, keySize); SecretKeyFactory factory = SecretKeyFactory.getInstance(PBKDF_2_WITH_HMAC_SHA_1); return new SecretKeySpec(factory.generateSecret(spec).getEncoded(), AES_ECB_ALGORITHM); } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { throw new IllegalStateException(e); } }
From source file:com.mmj.app.common.security.EncryptBuilder.java
private byte[] doCrypt(String source, String secretKey, ICrypt iCrypt) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { byte[] cryptedData = iCrypt.getCryptedData(source); Security.addProvider(new com.sun.crypto.provider.SunJCE()); SecureRandom sr = new SecureRandom(); byte[] rawKeyData = (new String(secretKey)).getBytes(); DESKeySpec dks = new DESKeySpec(rawKeyData); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance("DES"); iCrypt.initCipher(key, sr, cipher);/* ww w . j av a2 s . c om*/ return cipher.doFinal(cryptedData); }
From source file:com.anteam.demo.codec.cipher.symmetric.DESTest.java
License:asdf
public byte[] testDESedeEn(String plainText) { try {//from w ww . j av a2 s . co m // Create an array to hold the key byte[] encryptKey = "This is a test DESede key".getBytes(); // Create a DESede key spec from the key DESedeKeySpec spec = new DESedeKeySpec(encryptKey); // Get the secret key factor for generating DESede keys SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); // Generate a DESede SecretKey object SecretKey theKey = keyFactory.generateSecret(spec); // Create a DESede Cipher Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); // Create an initialization vector (necessary for CBC mode) IvParameterSpec IvParameters = new IvParameterSpec( new byte[] { 0x01, 0x02, 0x03, 0x04, 0x0F, 0x0E, 0x0D, 0x0C }); // Initialize the cipher and put it into encrypt mode cipher.init(Cipher.ENCRYPT_MODE, theKey, IvParameters); return cipher.doFinal(plainText.getBytes()); } catch (Exception exc) { exc.printStackTrace(); } return null; }
From source file:com.cherong.mock.common.base.util.EncryptionUtil.java
/** * des//from ww w .ja v a2s . c o m * * @param content * @param key * @return * @throws Exception */ public static String decryptByDES(byte[] content, String key) { try { DESKeySpec desKS = new DESKeySpec(key.getBytes()); SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); SecretKey sk = skf.generateSecret(desKS); Cipher cip = Cipher.getInstance(DES_CIPHER_ALGORITHM); cip.init(Cipher.DECRYPT_MODE, sk); byte[] result = cip.doFinal(content); return new String(result); } catch (Exception e) { LOGGER.error("{}", e); return null; } }
From source file:com.shenit.commons.codec.DesUtils.java
/** * /* ww w . j a v a2 s . co m*/ * @param rawData * @param rawKey * @param mode */ private static byte[] crypt(byte[] rawData, KeySpec keySpec, int mode) { // ?DESKeySpec byte[] result = null; try { // ?DESKeySpec??SecretKey SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(CODEC_DES); Key key = keyFactory.generateSecret(keySpec); if (key == null) { if (LOG.isWarnEnabled()) LOG.warn("[crypt] No key generated!"); return null; } // Cipher?? Cipher cipher = Cipher.getInstance(CODEC_DES); // DES???? cipher.init(mode, key, new SecureRandom()); // ?? // ?? result = cipher.doFinal(rawData); } catch (Exception ex) { LOG.warn("[crypt] crypt with exceptions", ex); } return result; }
From source file:com.fengduo.bee.commons.security.EncryptBuilder.java
@SuppressWarnings("restriction") private byte[] doCrypt(String source, String secretKey, ICrypt iCrypt) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { byte[] cryptedData = iCrypt.getCryptedData(source); Security.addProvider(new com.sun.crypto.provider.SunJCE()); SecureRandom sr = new SecureRandom(); byte[] rawKeyData = (new String(secretKey)).getBytes(); DESKeySpec dks = new DESKeySpec(rawKeyData); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance("DES"); iCrypt.initCipher(key, sr, cipher);//from w w w. j a v a2 s . co m return cipher.doFinal(cryptedData); }
From source file:org.zuinnote.flink.office.common.FlinkKeyStoreManager.java
/** * Sets the password in the currently openend keystore. Do not forget to store it afterwards * /*from w w w . ja v a 2s. c o m*/ * @param alias * @param password to store * @param passwordPassword password for encrypting password. You can use the same as the keystore password * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws KeyStoreException */ public void setPassword(String alias, String password, String passwordPassword) throws NoSuchAlgorithmException, InvalidKeySpecException, KeyStoreException { SecretKeyFactory skf = SecretKeyFactory.getInstance("PBE"); SecretKey pSecret = skf.generateSecret(new PBEKeySpec(password.toCharArray())); KeyStore.PasswordProtection kspp = new KeyStore.PasswordProtection(passwordPassword.toCharArray()); this.keystore.setEntry(alias, new KeyStore.SecretKeyEntry(pSecret), kspp); }
From source file:org.alfresco.encryption.KeyStoreTests.java
protected Key generateSecretKey(String keyAlgorithm) { try {//from w ww .j a va2 s. c o m DESedeKeySpec keySpec = new DESedeKeySpec(generateKeyData()); SecretKeyFactory kf = SecretKeyFactory.getInstance(keyAlgorithm); SecretKey secretKey = kf.generateSecret(keySpec); return secretKey; } catch (Throwable e) { fail("Unexpected exception: " + e.getMessage()); return null; } }
From source file:org.kawanfw.commons.util.convert.Pbe.java
/** * Encrypt or decrypt a string using a password * /*from www . j a v a 2 s . co m*/ * @param mode * Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE * @param in * the string to encrypt or Decrypt. if to decrypt: string must * be Hex encoded * @param password * the password to use * @return if Cipher.ENCRYPT_MODE: the encrypted string in hexadecimal if * Cipher.DECRYPT_MODE: the decrypted string in clear readable * format * * @throws Exception */ private String cipher(int mode, String in, char[] password) throws Exception { if (mode != Cipher.ENCRYPT_MODE && mode != Cipher.DECRYPT_MODE) { throw new IllegalArgumentException("mode is not Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE!"); } if (in == null) { throw new IllegalArgumentException("in string can not be null!"); } if (password == null) { throw new IllegalArgumentException("password can not be null!"); } PBEKeySpec pbeKeySpec; PBEParameterSpec pbeParamSpec; SecretKeyFactory keyFac; // Salt byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 }; // Iteration count int count = 20; // Create PBE parameter set pbeParamSpec = new PBEParameterSpec(salt, count); pbeKeySpec = new PBEKeySpec(password); keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec); // Create PBE Cipher Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); // Initialize PBE Cipher with key and parameters pbeCipher.init(mode, pbeKey, pbeParamSpec); // Our cleartext byte[] inText = null; if (mode == Cipher.ENCRYPT_MODE) { inText = in.getBytes(); } else { inText = CodecHex.decodeHex(in.toCharArray()); } // Encrypt the cleartext byte[] ciphertext = pbeCipher.doFinal(inText); if (mode == Cipher.ENCRYPT_MODE) { return new String(CodecHex.encodeHex(ciphertext)); } else { return new String(ciphertext); } }