Bitcoin

Bitcoin
Bitcoin

A java code to encrypt and decrypt a written text

package encryption;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 *
 * @author Sambyte
 */
public class EncryptionWork extends JFrame implements ActionListener{
    JButton encrypt = new JButton("ENCRYPT FILE"), decrypt = new JButton("DECRYPT FILE"), cancel = new JButton("CANCEL"),
                send = new JButton("SEND MESSAGE");
    private static final String ALG = "AES"; //AES is the encryption algorithm
    private static  String Key = "maryhasonecat123";//This is the key used for the encryption
    JTextArea area = new JTextArea();
    public EncryptionWork(){
        add(new JLabel("ENCRYPTION PORTAL"), "North");
        JPanel p = new JPanel();
     
        p.add(encrypt);p.add(decrypt);p.add(cancel);p.add(send);
        send.setEnabled(false);
        encrypt.addActionListener(this);decrypt.addActionListener(this);cancel.addActionListener(this);
        send.addActionListener(this);
        JInternalFrame f = new JInternalFrame();
        f.setLayout(new BorderLayout());
        f.add(p,"North");
     
        f.add(new JScrollPane(area));
        f.setVisible(true);
        add(f);
        setSize(700,600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    public static void main(String[] args){
        new EncryptionWork();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == encrypt){
         
                try{
                area.setText(encrypt());
                }catch(Exception ex){ex.printStackTrace();}
         
        }
        else if(e.getSource() == decrypt){
         
                try{
                    String v = decrypt(area.getText().trim());
                area.setText(v);
                }catch(Exception ex){ex.printStackTrace();}
         
        }
        else if(e.getSource() == cancel){
         
        }
        else if(e.getSource() == send){
         
        }
    }
    private static Key generateKey(){
        Key key = new SecretKeySpec(Key.trim().getBytes(), ALG);
        return key;
    }
//This method encrypt what is written in the JTextArea
    public  String encrypt() throws Exception{
        Key key = generateKey();
        Cipher cipher = Cipher.getInstance(ALG);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encrypt1 = cipher.doFinal(area.getText().trim().getBytes());
        String encryptedValue = new BASE64Encoder().encode(encrypt1);
        return encryptedValue;
    }
//This is the method to decrypt the encrypted file
    public String decrypt(String value) {
        String decrypting = null;
        try{
        Key key = generateKey();
        Cipher cipher = Cipher.getInstance(ALG);
        cipher.init(Cipher.DECRYPT_MODE,key);
        byte[] decrypt1 = new BASE64Decoder().decodeBuffer(value);
        byte[] decrypted = cipher.doFinal(decrypt1);
        decrypting = new String(decrypted);
     
        }catch(Exception e){decrypting = e.getMessage();}
        return decrypting;
    }
 
}

No comments:

Post a Comment

Facebook