-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.java
More file actions
43 lines (33 loc) · 1.33 KB
/
encryption.java
File metadata and controls
43 lines (33 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.secure.kk.action;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.JOptionPane;
import sun.misc.BASE64Encoder;
public class encryption {
public String encrypt(String text, SecretKey secretkey) {
String plainData = null, cipherText = null;
try {
plainData = text;
Cipher aesCipher = Cipher.getInstance("AES");//getting AES instance
aesCipher.init(Cipher.ENCRYPT_MODE, secretkey);//initiating ciper encryption using secretkey
byte[] byteDataToEncrypt = plainData.getBytes();
byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt);//encrypting data
cipherText = new BASE64Encoder().encode(byteCipherText);//converting encrypted data to string
System.out.println("\n Given text : " + plainData + " \n Cipher Data : " + cipherText);
} catch (Exception e) {
System.out.println(e);
}
return cipherText;
}
}