Java tutorial
/* * Copyright (C) 2016 * * * * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ package edu.wright.cs.sp16.ceg3120.util; import org.apache.commons.codec.binary.Base64; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; /** * PasswordEncryptionService class handles the password encryption. It uses * PBKDF2 to encrypt and decrypt clear text. */ public class PasswordEncryptionUtility { private static String initVector = "zGPDuuc7RfWJPz3t"; private static String key = "vQ2bEnvgEehv23H9"; /** * Encrypts a given string using AES. * * @param value * // String to encrypt. * @return // Returns encrypted string. */ public static String encrypt(String value) { try { 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("UTF-8")); System.out.println("encrypted string: " + Base64.encodeBase64String(encrypted)); return Base64.encodeBase64String(encrypted); } catch (RuntimeException e) { throw e; } catch (Exception ex) { ex.printStackTrace(); } return null; } /** * Decrypts a given string using AES. * * @param encrypted * // Encrypted string. * @return // Returns decrypted string. */ public static String decrypt(String encrypted) { try { 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.DECRYPT_MODE, skeySpec, iv); byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted)); System.out.println("Decrypted Password: " + new String(original, "UTF-8")); return new String(original, "UTF-8"); } catch (RuntimeException e) { throw e; } catch (Exception ex) { ex.printStackTrace(); } return null; } }