Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package enigma3;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JFileChooser;
- import javax.swing.JOptionPane;
- public class Crypt extends javax.swing.JFrame {
- int rueckgabeWert=0;
- JFileChooser chooser = new JFileChooser();
- public Crypt() {
- initComponents();
- }
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jButton1 = new javax.swing.JButton();
- jScrollPane1 = new javax.swing.JScrollPane();
- jTextArea1 = new javax.swing.JTextArea();
- jScrollPane2 = new javax.swing.JScrollPane();
- jTextArea2 = new javax.swing.JTextArea();
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- jButton2 = new javax.swing.JButton();
- jButton3 = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jButton1.setText("Decrypt");
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- jTextArea1.setColumns(20);
- jTextArea1.setRows(5);
- jScrollPane1.setViewportView(jTextArea1);
- jTextArea2.setColumns(20);
- jTextArea2.setRows(5);
- jScrollPane2.setViewportView(jTextArea2);
- jLabel1.setText("Input:");
- jLabel2.setText("Output");
- jButton2.setText("Encrypt");
- jButton2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton2ActionPerformed(evt);
- }
- });
- jButton3.setText("Create Key");
- jButton3.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton3ActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jLabel1)
- .addComponent(jButton2))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jButton1)
- .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jLabel2)))
- .addComponent(jButton3))
- .addContainerGap(38, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jButton3)
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jButton1)
- .addComponent(jButton2))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 48, Short.MAX_VALUE)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addComponent(jLabel1)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addComponent(jLabel2)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addContainerGap(34, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
- Encrypt();
- }
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- Decrypt();
- }
- private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
- Prozess();
- }
- public void Encrypt(){
- rueckgabeWert = chooser.showOpenDialog(null);
- if(rueckgabeWert == JFileChooser.APPROVE_OPTION)
- {
- JOptionPane.showMessageDialog(rootPane, "Successfully loaded");
- rueckgabeWert=0;
- try {
- String T= jTextArea1.getText();
- char[] c= T.toCharArray();
- int p=c.length;
- int a1=0;
- int a2=0;
- int a3=0;
- int a4=0;
- int a5=0;
- int a6=0;
- int kl=0;
- boolean b=false;
- int[] i=new int[1000];
- FileReader fe=new FileReader(chooser.getSelectedFile().getPath());
- BufferedReader re =new BufferedReader(fe);
- char[] n=re.readLine().toCharArray();
- int p2=n.length;
- int i2=0;
- for(int l = 0;l<p2;l++){
- String s="";
- for(;n[l]!='|'&&l<p2;l++){
- s=s+n[l];
- }
- i[i2]= Integer.parseInt(s);
- if(i2<1000)i2++;
- else{
- b=true;
- }
- }
- if(b==true){
- JOptionPane.showMessageDialog(rootPane, "Error at reading data from file");
- re.close();
- }
- else{
- re.close();
- int[] in = new int[p];
- for(int l = 0;l<p;l++){
- in[l] = LtoN(c[l]);
- }
- for(int l = 0;l<p;l++){
- a6=a5;
- a5=a4;
- a4=a3;
- a3=a2;
- a2=a1;
- a1=in[l];
- in[l]=in[l]+a2;
- if(in[l]>65535){
- in[l]=in[l]-65535;
- }
- in[l]=in[l]+a3;
- if(in[l]>65535){
- in[l]=in[l]-65535;
- }
- in[l]=in[l]-a4;
- if(in[l]<1){
- in[l]=65535+in[l];
- }
- in[l]=in[l]-a5;
- if(in[l]<1){
- in[l]=65535+in[l];
- }
- in[l]=in[l]+a6;
- if(in[l]>65535){
- in[l]=in[l]-65535;
- }
- in[l]=in[l]+i[kl];
- if(in[l]>65535){
- in[l]=in[l]-65535;
- }
- kl++;
- if(kl>999){
- kl=0;
- }
- }
- String Output="";
- for(int l = 0;l<p;l++){
- Output=Output+NtoL(in[l]);
- }
- jTextArea2.setText(Output);
- }
- } catch (FileNotFoundException ex) {
- JOptionPane.showMessageDialog(rootPane, "No file choosen");
- } catch (IOException ex) {
- JOptionPane.showMessageDialog(rootPane, "Error at reading data from file");
- }
- }
- else{
- JOptionPane.showMessageDialog(rootPane, "No file selected");
- }
- }
- public void Encryptfile(){
- JOptionPane.showMessageDialog(rootPane, "Please select key");
- rueckgabeWert = chooser.showOpenDialog(null);
- if(rueckgabeWert == JFileChooser.APPROVE_OPTION)
- {
- JOptionPane.showMessageDialog(rootPane, "Successfully loaded");
- rueckgabeWert=0;
- try {
- int a1=0;
- int a2=0;
- int a3=0;
- int a4=0;
- int a5=0;
- int a6=0;
- int kl=0;
- boolean b=false;
- int[] i=new int[1000];
- FileReader fe=new FileReader(chooser.getSelectedFile().getPath());
- BufferedReader re =new BufferedReader(fe);
- chooser.cancelSelection();
- char[] n=re.readLine().toCharArray();
- int p2=n.length;
- int i2=0;
- for(int l = 0;l<p2;l++){
- String s="";
- for(;n[l]!='|'&&l<p2;l++){
- s=s+n[l];
- }
- i[i2]= Integer.parseInt(s);
- if(i2<1000)i2++;
- else{
- b=true;
- }
- }
- JOptionPane.showMessageDialog(rootPane, "Please select file to encrypt");
- rueckgabeWert = chooser.showOpenDialog(null);
- if(rueckgabeWert == JFileChooser.APPROVE_OPTION)
- {
- JOptionPane.showMessageDialog(rootPane, "Successfully loaded");
- }
- else{
- JOptionPane.showMessageDialog(rootPane, "No file selected");
- chooser.cancelSelection();
- }
- rueckgabeWert=0;
- FileReader f=new FileReader(chooser.getSelectedFile().getPath());
- BufferedReader r =new BufferedReader(f);
- chooser.cancelSelection();
- char[] c=r.readLine().toCharArray();
- int p=c.length;
- r.close();
- if(b==true){
- JOptionPane.showMessageDialog(rootPane, "Error at reading data from file");
- re.close();
- }
- else{
- re.close();
- int[] in = new int[p];
- for(int l = 0;l<p;l++){
- in[l] = LtoN(c[l]);
- }
- for(int l = 0;l<p;l++){
- a6=a5;
- a5=a4;
- a4=a3;
- a3=a2;
- a2=a1;
- a1=in[l];
- in[l]=in[l]+a2;
- if(in[l]>65535){
- in[l]=in[l]-65535;
- }
- in[l]=in[l]+a3;
- if(in[l]>65535){
- in[l]=in[l]-65535;
- }
- in[l]=in[l]-a4;
- if(in[l]<1){
- in[l]=65535+in[l];
- }
- in[l]=in[l]-a5;
- if(in[l]<1){
- in[l]=65535+in[l];
- }
- in[l]=in[l]+a6;
- if(in[l]>65535){
- in[l]=in[l]-65535;
- }
- in[l]=in[l]+i[kl];
- if(in[l]>65535){
- in[l]=in[l]-65535;
- }
- kl++;
- if(kl>999){
- kl=0;
- }
- }
- String Output="";
- for(int l = 0;l<p;l++){
- Output=Output+NtoL(in[l]);
- }
- JOptionPane.showMessageDialog(rootPane, "Please select result file");
- rueckgabeWert = chooser.showSaveDialog(null);
- if(rueckgabeWert == JFileChooser.APPROVE_OPTION)
- {
- JOptionPane.showMessageDialog(rootPane, "Successfully loaded");
- }
- else{
- JOptionPane.showMessageDialog(rootPane, "No file selected");
- chooser.cancelSelection();
- }
- rueckgabeWert=0;
- PrintWriter writer = new PrintWriter(chooser.getSelectedFile().getPath(), "UTF-8");
- chooser.cancelSelection();
- writer.println(Output);
- writer.close();
- }
- } catch (FileNotFoundException ex) {
- JOptionPane.showMessageDialog(rootPane, "No file choosen");
- } catch (IOException ex) {
- JOptionPane.showMessageDialog(rootPane, "Error at reading data from file");
- }
- }
- else{
- JOptionPane.showMessageDialog(rootPane, "No file selected");
- }
- }
- public void Decrypt(){
- rueckgabeWert = chooser.showOpenDialog(null);
- if(rueckgabeWert == JFileChooser.APPROVE_OPTION)
- {
- rueckgabeWert=0;
- JOptionPane.showMessageDialog(rootPane, "Successfully loaded");
- try {
- boolean b=false;
- String T= jTextArea1.getText();
- char[] c= T.toCharArray();
- int p=c.length;
- int a2=0;
- int a3=0;
- int a4=0;
- int a5=0;
- int a6=0;
- int kl=0;
- int[] i=new int[1000];
- FileReader fe=new FileReader(chooser.getSelectedFile().getPath());
- BufferedReader re =new BufferedReader(fe);
- chooser.cancelSelection();
- char[] n=re.readLine().toCharArray();
- int p2=n.length;
- int i2=0;
- for(int l = 0;l<p2;l++){
- String s="";
- for(;n[l]!='|'&&l<p2;l++){
- s=s+n[l];
- }
- i[i2]= Integer.parseInt(s);
- if(i2<1000)i2++;
- else{
- b=true;
- }
- }
- if(b==true){
- JOptionPane.showMessageDialog(rootPane, "Error at reading data from file"+i[99]);
- re.close();
- }
- else{
- re.close();
- int[] in = new int[p];
- for(int l = 0;l<p;l++){
- in[l] = LtoN(c[l]);
- }
- for(int l = 0;l<p;l++){
- in[l]=in[l]-i[kl];
- if(in[l]<1){
- in[l]=in[l]+65535;
- }
- kl++;
- if(kl>999){
- kl=0;
- }
- in[l]=in[l]-a2;
- if(in[l]<1){
- in[l]=65535+in[l];
- }
- in[l]=in[l]-a3;
- if(in[l]<1){
- in[l]=65535+in[l];
- }
- in[l]=in[l]+a4;
- if(in[l]>65535){
- in[l]=in[l]-65535;
- }
- in[l]=in[l]+a5;
- if(in[l]>65535){
- in[l]=in[l]-65535;
- }
- in[l]=in[l]-a6;
- if(in[l]<1){
- in[l]=65535+in[l];
- }
- a6=a5;
- a5=a4;
- a4=a3;
- a3=a2;
- a2=in[l];
- }
- String Output="";
- for(int l = 0;l<p;l++){
- Output=Output+NtoL(in[l]);
- }
- jTextArea2.setText(Output);
- }
- } catch (FileNotFoundException ex) {
- JOptionPane.showMessageDialog(rootPane, "No file choosen");
- } catch (IOException ex) {
- JOptionPane.showMessageDialog(rootPane, "Error at reading data from file");
- }
- }
- else{
- JOptionPane.showMessageDialog(rootPane, "No file selected");
- rueckgabeWert=0;
- chooser.cancelSelection();
- }
- }
- public void Decryptfile(){
- rueckgabeWert = chooser.showOpenDialog(null);
- if(rueckgabeWert == JFileChooser.APPROVE_OPTION)
- {
- JOptionPane.showMessageDialog(rootPane, "Successfully loaded");
- rueckgabeWert=0;
- try {
- boolean b=false;
- int a2=0;
- int a3=0;
- int a4=0;
- int a5=0;
- int a6=0;
- int kl=0;
- int[] i=new int[1000];
- FileReader fe=new FileReader(chooser.getSelectedFile().getPath());
- BufferedReader re =new BufferedReader(fe);
- chooser.cancelSelection();
- char[] n=re.readLine().toCharArray();
- int p2=n.length;
- int i2=0;
- for(int l = 0;l<p2;l++){
- String s="";
- for(;n[l]!='|'&&l<p2;l++){
- s=s+n[l];
- }
- i[i2]= Integer.parseInt(s);
- if(i2<1000)i2++;
- else{
- b=true;
- }
- }
- if(b==true){
- JOptionPane.showMessageDialog(rootPane, "Error at reading data from file"+i[99]);
- re.close();
- }
- else{
- re.close();
- JOptionPane.showMessageDialog(rootPane, "Please select file to decrypt");
- rueckgabeWert = chooser.showOpenDialog(null);
- if(rueckgabeWert == JFileChooser.APPROVE_OPTION)
- {
- JOptionPane.showMessageDialog(rootPane, "Successfully loaded");
- }
- else{
- JOptionPane.showMessageDialog(rootPane, "No file selected");
- chooser.cancelSelection();
- }
- rueckgabeWert=0;
- FileReader f=new FileReader(chooser.getSelectedFile().getPath());
- BufferedReader r =new BufferedReader(f);
- chooser.cancelSelection();
- char[] c=r.readLine().toCharArray();
- int p=c.length;
- r.close();
- int[] in = new int[p];
- for(int l = 0;l<p;l++){
- in[l] = LtoN(c[l]);
- }
- for(int l = 0;l<p;l++){
- in[l]=in[l]-i[kl];
- if(in[l]<1){
- in[l]=in[l]+65535;
- }
- kl++;
- if(kl>999){
- kl=0;
- }
- in[l]=in[l]-a2;
- if(in[l]<1){
- in[l]=65535+in[l];
- }
- in[l]=in[l]-a3;
- if(in[l]<1){
- in[l]=65535+in[l];
- }
- in[l]=in[l]+a4;
- if(in[l]>65535){
- in[l]=in[l]-65535;
- }
- in[l]=in[l]+a5;
- if(in[l]>65535){
- in[l]=in[l]-65535;
- }
- in[l]=in[l]-a6;
- if(in[l]<1){
- in[l]=65535+in[l];
- }
- a6=a5;
- a5=a4;
- a4=a3;
- a3=a2;
- a2=in[l];
- }
- String Output="";
- for(int l = 0;l<p;l++){
- Output=Output+NtoL(in[l]);
- }
- JOptionPane.showMessageDialog(rootPane, "Please select result file");
- rueckgabeWert = chooser.showSaveDialog(null);
- if(rueckgabeWert == JFileChooser.APPROVE_OPTION)
- {
- JOptionPane.showMessageDialog(rootPane, "Successfully loaded");
- }
- else{
- JOptionPane.showMessageDialog(rootPane, "No file selected");
- chooser.cancelSelection();
- }
- rueckgabeWert=0;
- PrintWriter writer = new PrintWriter(chooser.getSelectedFile().getPath(), "UTF-8");
- chooser.cancelSelection();
- writer.println(Output);
- writer.close();
- }
- } catch (FileNotFoundException ex) {
- JOptionPane.showMessageDialog(rootPane, "No file choosen");
- } catch (IOException ex) {
- JOptionPane.showMessageDialog(rootPane, "Error at reading data from file");
- }
- }
- else{
- JOptionPane.showMessageDialog(rootPane, "No file selected");
- rueckgabeWert=0;
- chooser.cancelSelection();
- }
- }
- public char NtoL(int i){
- char c = (char)i;
- return c;
- }
- public int LtoN(char c){
- int i = (int) c;
- return i;
- }
- public void Prozess(){
- JFileChooser chooser = new JFileChooser();
- int rueckgabeWert = chooser.showSaveDialog(null);
- if(rueckgabeWert == JFileChooser.APPROVE_OPTION)
- {
- System.out.println(chooser.getSelectedFile().getPath());
- try{
- FileWriter fw = new FileWriter(chooser.getSelectedFile().getPath());
- BufferedWriter bw = new BufferedWriter(fw);
- for(int i=0;i<1000;i++){
- int n=(int) ((Math.random()*65534)+1);
- System.out.println(n);
- String s=""+n;
- bw.write(s);
- bw.write("|");
- }
- bw.close();
- JOptionPane.showMessageDialog(rootPane, "Sucessfull");
- }
- catch(IOException ioe){
- }
- }
- }
- public void start(){
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(Crypt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(Crypt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(Crypt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(Crypt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new Crypt().setVisible(true);
- }
- });
- }
- public static void main(String args[]) {
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(Crypt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(Crypt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(Crypt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(Crypt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new Crypt().setVisible(true);
- }
- });
- private javax.swing.JButton jButton1;
- private javax.swing.JButton jButton2;
- private javax.swing.JButton jButton3;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JScrollPane jScrollPane2;
- private javax.swing.JTextArea jTextArea1;
- private javax.swing.JTextArea jTextArea2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement