password encryption and decryption in java

Solutions on MaxInterview for password encryption and decryption in java by the best coders in the world

showing results for - "password encryption and decryption in java"
Laila
10 Jun 2017
1import java.security.spec.KeySpec;
2import javax.crypto.Cipher;
3import javax.crypto.SecretKey;
4import javax.crypto.SecretKeyFactory;
5import javax.crypto.spec.DESedeKeySpec;
6import org.apache.commons.codec.binary.Base64;
7
8public class TrippleDes {
9
10    private static final String UNICODE_FORMAT = "UTF8";
11    public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
12    private KeySpec ks;
13    private SecretKeyFactory skf;
14    private Cipher cipher;
15    byte[] arrayBytes;
16    private String myEncryptionKey;
17    private String myEncryptionScheme;
18    SecretKey key;
19
20    public TrippleDes() throws Exception {
21        myEncryptionKey = "ThisIsSpartaThisIsSparta";
22        myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;
23        arrayBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
24        ks = new DESedeKeySpec(arrayBytes);
25        skf = SecretKeyFactory.getInstance(myEncryptionScheme);
26        cipher = Cipher.getInstance(myEncryptionScheme);
27        key = skf.generateSecret(ks);
28    }
29
30
31    public String encrypt(String unencryptedString) {
32        String encryptedString = null;
33        try {
34            cipher.init(Cipher.ENCRYPT_MODE, key);
35            byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);
36            byte[] encryptedText = cipher.doFinal(plainText);
37            encryptedString = new String(Base64.encodeBase64(encryptedText));
38        } catch (Exception e) {
39            e.printStackTrace();
40        }
41        return encryptedString;
42    }
43
44
45    public String decrypt(String encryptedString) {
46        String decryptedText=null;
47        try {
48            cipher.init(Cipher.DECRYPT_MODE, key);
49            byte[] encryptedText = Base64.decodeBase64(encryptedString);
50            byte[] plainText = cipher.doFinal(encryptedText);
51            decryptedText= new String(plainText);
52        } catch (Exception e) {
53            e.printStackTrace();
54        }
55        return decryptedText;
56    }
57
58
59    public static void main(String args []) throws Exception
60    {
61        TrippleDes td= new TrippleDes();
62
63        String target="imparator";
64        String encrypted=td.encrypt(target);
65        String decrypted=td.decrypt(encrypted);
66
67        System.out.println("String To Encrypt: "+ target);
68        System.out.println("Encrypted String:" + encrypted);
69        System.out.println("Decrypted String:" + decrypted);
70
71    }
72
73}
74
Giada
09 Oct 2020
1package com.javapapers.java.security;
2
3import java.io.FileInputStream;
4import java.io.FileOutputStream;
5import java.util.Random;
6
7import javax.crypto.Cipher;
8import javax.crypto.SecretKey;
9import javax.crypto.SecretKeyFactory;
10import javax.crypto.spec.PBEKeySpec;
11import javax.crypto.spec.PBEParameterSpec;
12
13public class FileEncryption {
14
15	public static void main(String[] args) throws Exception {
16
17		FileInputStream inFile = new FileInputStream("plainfile.txt");
18		FileOutputStream outFile = new FileOutputStream("plainfile.des");
19
20		String password = "javapapers";
21		PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
22		SecretKeyFactory secretKeyFactory = SecretKeyFactory
23				.getInstance("PBEWithMD5AndTripleDES");
24		SecretKey secretKey = secretKeyFactory.generateSecret(pbeKeySpec);
25
26		byte[] salt = new byte[8];
27		Random random = new Random();
28		random.nextBytes(salt);
29
30		PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, 100);
31		Cipher cipher = Cipher.getInstance("PBEWithMD5AndTripleDES");
32		cipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeParameterSpec);
33		outFile.write(salt);
34
35		byte[] input = new byte[64];
36		int bytesRead;
37		while ((bytesRead = inFile.read(input)) != -1) {
38			byte[] output = cipher.update(input, 0, bytesRead);
39			if (output != null)
40				outFile.write(output);
41		}
42
43		byte[] output = cipher.doFinal();
44		if (output != null)
45			outFile.write(output);
46
47		inFile.close();
48		outFile.flush();
49		outFile.close();
50	}
51
52}
queries leading to this page
java encryption decryption examplealgorithm for encryption and decryption in javapassword decryption java codekey based encryption decryption algorithm java examplesimple encryption decryption algorithms in javaencrypt and decrypt password in javadecryption and encryption in javaencrypt decrypt password in javajava encrypt and decrypt passwordpassword encryption and decryption in javapassword encryption and decryption in javascript examplejava code for encryption and decryptionencryption and decryption javasimple java encryption decryption exampleencryption and decryption in java source codejava text encryption decryptionencryption and decryption algorithm javaencryption and decryption program in javapassword encryption and decryption in java source codeencryption and decryption in javajava encrypt decrypt password examplejava string encryption and decryption examplejava encryption and decryptionencrypt password and decrypt in javaencrypt 2f decrypt paasswords javaclass to encryot 26 decrypt password in javaencryption decryption in java exampletext encryption and decryption javasimple encryption and decryption in javaencryption decryption library javasimple java program for encryption and decryptionclass to encryot 26 decrypt password in hjavaencryption decryption in javaencryption and decryption algorithm in javajava symmetric encryption decryption example encryption decryption algorithms javacaesar cipher encryption and decryption code javahow to encrypt decrypt password in java using keyhow to decrypt a password in javajava encryption and decryption examplejava password decryptionjava password encryptionencryption decryption algorithms in javawhat is encryption and decryption in javajava encryption and decryption libraryhow to decrypt password in javaencryption and decryption password in javajava 11 encryption decryption examplejava encryption and decryption tutorialencryption and decryption with key javajava encryption and decrption passwordpassword encryption decryption class in javajava encryption and decryption programhow to encrypt and decrypt password in javaencryption decryption algorithms in java passwordencryption decryption java examplejava encrypt string with passwordhow to make an encryption and decryption program in javasimple password encryption javaencryption and decryption java intagerspassword encryption and decryption in java