· 6 years ago · Mar 19, 2019, 11:10 AM
1import javax.crypto.KeyGenerator.*;
2import javax.crypto.*;
3import java.util.*;
4import java.io.*;
5import javax.crypto.Cipher.*;
6import java.nio.charset.Charset;
7import javax.crypto.spec.IvParameterSpec;
8public class encrypt{
9 public static void main(String []args)throws Exception{
10 KeyGenerator genobj=KeyGenerator.getInstance("DES");
11 SecretKey symkey=genobj.generateKey();
12 Scanner s=new Scanner(System.in);
13 Cipher cipher=Cipher.getInstance("DES/CBC/PKCS5Padding");
14 cipher.init(Cipher.ENCRYPT_MODE,symkey,new IvParameterSpec(new byte[8]));
15 while (true){
16 String test=s.nextLine();
17 byte[] tmp = test.getBytes(Charset.forName("UTF-8"));
18 byte[] done=cipher.doFinal(tmp);
19 String son=new String(done,"UTF-8");
20 System.out.println(son);
21 decrypt(done,symkey);
22 }
23
24 }
25 static void decrypt(byte[] don,SecretKey lol)throws Exception{
26 Cipher cipher2=Cipher.getInstance("DES/CBC/PKCS5Padding");
27 cipher2.init(Cipher.DECRYPT_MODE,lol,new IvParameterSpec(new byte[8]));
28 byte[] dec=cipher2.doFinal(don);
29 String son2=new String(dec,"UTF-8");
30 System.out.println(son2);
31
32
33 }
34}