Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

password encryption and decryption in java

import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import org.apache.commons.codec.binary.Base64;

public class TrippleDes {

    private static final String UNICODE_FORMAT = "UTF8";
    public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
    private KeySpec ks;
    private SecretKeyFactory skf;
    private Cipher cipher;
    byte[] arrayBytes;
    private String myEncryptionKey;
    private String myEncryptionScheme;
    SecretKey key;

    public TrippleDes() throws Exception {
        myEncryptionKey = "ThisIsSpartaThisIsSparta";
        myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;
        arrayBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
        ks = new DESedeKeySpec(arrayBytes);
        skf = SecretKeyFactory.getInstance(myEncryptionScheme);
        cipher = Cipher.getInstance(myEncryptionScheme);
        key = skf.generateSecret(ks);
    }


    public String encrypt(String unencryptedString) {
        String encryptedString = null;
        try {
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);
            byte[] encryptedText = cipher.doFinal(plainText);
            encryptedString = new String(Base64.encodeBase64(encryptedText));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encryptedString;
    }


    public String decrypt(String encryptedString) {
        String decryptedText=null;
        try {
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] encryptedText = Base64.decodeBase64(encryptedString);
            byte[] plainText = cipher.doFinal(encryptedText);
            decryptedText= new String(plainText);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return decryptedText;
    }


    public static void main(String args []) throws Exception
    {
        TrippleDes td= new TrippleDes();

        String target="imparator";
        String encrypted=td.encrypt(target);
        String decrypted=td.decrypt(encrypted);

        System.out.println("String To Encrypt: "+ target);
        System.out.println("Encrypted String:" + encrypted);
        System.out.println("Decrypted String:" + decrypted);

    }

}
Comment

PREVIOUS NEXT
Code Example
Java :: package within another java 
Java :: switch activity android studi 
Java :: encode file to base64 java 
Java :: java csv 
Java :: foreach map java 
Java :: arrays.fill java 
Java :: get text from edittext android 
Java :: entre clavier java 
Java :: rewrite file java 
Java :: how to iterate hashmap java 
Java :: android kotlin center text 
Java :: java read lines from file 
Java :: Failed to determine a suitable driver class 
Java :: android vibrate 
Java :: print random from list java 
Java :: byte to bufferedimage java 
Java :: play sound in java 
Java :: iterating over a hashmap 
Java :: java collection to list 
Java :: java date string 
Java :: javafx location is required 
Java :: findviewbyid java android 
Java :: comment in java 
Java :: check instance of java 
Java :: variable between two numbers java 
Java :: math.sin java 
Java :: static int java 
Java :: java stream get list of one field 
Java :: string to byte array java 
Java :: java download file from url 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =