· 6 years ago · Mar 26, 2019, 10:18 AM
1import javax.crypto.*;
2import java.io.*;
3import javax.crypto.Cipher.*;
4public class symm9{
5 public static void main(String[] args) throws Exception
6 {
7KeyGenerator Genobj= KeyGenerator.getInstance("DES");
8SecretKey symmkey=Genobj.generateKey();
9String toencrypt="hi my name";
10Cipher cipherobj=Cipher.getInstance("DES/ECB/PKCS5Padding");
11cipherobj.init(Cipher.ENCRYPT_MODE,symmkey);
12byte[] Bytetoencrypt=toencrypt.getBytes("UTF-8");
13byte[] Encrypted=cipherobj.doFinal(Bytetoencrypt);
14String word= new String(Encrypted,"UTF-8");
15System.out.println(word);
16cipherobj.init(Cipher.DECRYPT_MODE,symmkey);
17byte[] bytedecrypted=cipherobj.doFinal(Encrypted);
18String word1= new String(bytedecrypted,"UTF-8");
19System.out.println("answer2="+word1);
20
21
22
23}
24}