Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java spring username encode and decode

private static final String ALGO = "AES";
private static final byte[] keyValue = new byte[] { 'T', 'E', 'S', 'T' };


/**
 * Encrypt a string using AES encryption algorithm.
 *
 * @param pwd the password to be encrypted
 * @return the encrypted string
 */
public static String encrypt(String pwd) {
    String encodedPwd = "";
    try {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encVal = c.doFinal(pwd.getBytes());
        encodedPwd = Base64.getEncoder().encodeToString(encVal);

    } catch (Exception e) {

        e.printStackTrace();
    }
    return encodedPwd;

}

/**
 * Decrypt a string with AES encryption algorithm.
 *
 * @param encryptedData the data to be decrypted
 * @return the decrypted string
 */
public static String decrypt(String encryptedData) {
    String decodedPWD = "";
    try {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = Base64.getDecoder().decode(encryptedData);
        byte[] decValue = c.doFinal(decordedValue);
        decodedPWD = new String(decValue);

    } catch (Exception e) {

    }
    return decodedPWD;
}

/**
 * Generate a new encryption key.
 */
private static Key generateKey() {
    SecretKeySpec key = new SecretKeySpec(keyValue, ALGO);
    return key;
}
Comment

PREVIOUS NEXT
Code Example
Java :: reading string after double in java 
Java :: how to convert 2d to 1d array in java 
Java :: spiral traversal of matrix leetcode 
Java :: java date equals other date 
Java :: java implement interface 
Java :: java get element occurrences in a list java 
Java :: iterate over multi array java 
Java :: spring properties list 
Java :: java checked exception 
Java :: Java Create a BufferedOutputStream 
Java :: remove tableview separator lines in javafx 
Java :: public static void main(String[] args) in java 
Java :: fragment view in XML android 
Java :: MyArrayList 
Java :: nextint java 
Java :: Calendar ranges java 
Java :: how to delete an element from an array in java data structure 
Java :: osmdroid get current zoom level 
Java :: dockerfile spring boot 
Java :: Java boolean Keyword 
Java :: singleton implementation 
Java :: iterative inorder traversal 
Java :: why are my if statements not working with inputs in java 
Java :: java swing border 
Java :: mettre caractère de string en majuscule java 
Java :: string to pojo java 
Java :: Write code to declare an array that will hold calendar months (.e. January to December) java 
Java :: where to use findviewbyid in fragment 
Java :: click on recyclerview item android animation 
Java :: stringbuilder java setlength 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =