hi all,how are u? ..i have an encryption project.. i have to implement some of the encryption algorithme in java but i have a problem ..it's the same in all the ciphers ..when i type the key and press encrypt the encrypted text is not right ..i make my calculus on paper and i find out that the ciphered text is not right and i can't find the error in my code ..please help ..that's the code of the shift cipher.

import java.awt.*;
   import javax.swing.*;
   import java.awt.event.*;





   public class Shiftcipher extends JFrame{
   
      JButton encrypt;
      JButton decrypt;
      JTextField toenc;
      JTextField keyfield;
      JTextField res;
      JTextField todec;
      JTextField decrres;
      int key;
   
      public Shiftcipher () {
      
         create ();
      }
   
   
   
      public void create()
      
      {
      
         Container content=getContentPane();
         Color c1=new Color(48, 63, 120);
         content.setBackground(c1);
         content.setLayout(null);
      
         JLabel welcome=new JLabel();
         welcome.setBounds(500,4,500,50);
         welcome.setText("<html><body><font size=20 face=Andalus >Shift Cipher</font></body></html>");
         content.add(welcome);
      
         JLabel text=new JLabel();
         text.setBounds(120,200,200,100);
         text.setText("<html><body><font size=5 face=Andalus >Text To Encrypt</font></body></html>");
         content.add(text);
      
         toenc=new JTextField();
         toenc.setBounds(260,235,150,30);
         content.add(toenc);
      
         JLabel restext=new JLabel();
         restext.setBounds(120,342,200,100);
         restext.setText("<html><body><font size=5 face=Andalus >Encrypted Text</font></body></html>");
         content.add(restext);
      
         JLabel key1=new JLabel();
         key1.setBounds(520,160,50,20);
         key1.setText("<html><body><font size=5 face=Andalus >Key</font></body></html>");
         content.add(key1);
      
         keyfield=new JTextField();
         keyfield.setBounds(550,160,50,20);
         content.add(keyfield);
      
          res=new JTextField();
         res.setBounds(260,375,150,30);
         res.setEditable(false);
         content.add(res);
      
         encrypt=new JButton();
         encrypt.setText("Encrypt");
         encrypt.setForeground(c1);
         encrypt.setBounds(100,300,170,35);
         content.add(encrypt);
      
      
         encrypt.addActionListener(
                              
                              
                              
                              
                              
                              
                                 new ActionListener()
                                 
                                 
                                 
                                 {
                                 
                                 
                                 
                                    public void actionPerformed(ActionEvent event)
                                    
                                    
                                    
                                    
                                    
                                    {
                                       try{
                                         // String [] tab={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
                                          String encres = "";
                                           key=Integer.parseInt(keyfield.getText());
                                          String temp=toenc.getText();
                                          char [] e;
                                          int encryptedtext;
                                          e=temp.toCharArray();
                                          for(int i=0;i<temp.length();i++){
                                          encryptedtext = 65+ (((int)e[i]) + key)%26;
                                          encres += (char)encryptedtext;

                                       }
                                          res.setText(encres);
                                          
                                          
                                       
                                          
                                       
                                       
                                       
                                       }
                                       
                                       
                                       
                                       
                                          catch (Exception e)
                                          
                                          
                                          
                                          
                                          {
                                          }
                                    }
                                 }); 
      
      
      
      
      
      
      
         JLabel enctext=new JLabel();
         enctext.setBounds(750,200,200,100);
         enctext.setText("<html><body><font size=5 face=Andalus >Text To Decrypt</font></body></html>");
         content.add(enctext);
      
         todec=new JTextField();
         todec.setBounds(885,235,150,30);
         content.add(todec);
      
         JLabel resdec=new JLabel();
         resdec.setBounds(750,342,200,100);
         resdec.setText("<html><body><font size=5 face=Andalus >Decrypted Text</font></body></html>");
         content.add(resdec);
      
         decrres=new JTextField();
         decrres.setBounds(885,375,150,30);
         decrres.setEditable(false);
         content.add(decrres);
      
         decrypt=new JButton();
         decrypt.setText("Decrypt");
         decrypt.setForeground(c1);
         decrypt.setBounds(750,300,170,35);
         content.add(decrypt);
      
      
      
         decrypt.addActionListener(
                                     
                                 new ActionListener()
                          
                                 {
                                    public void actionPerformed(ActionEvent event1)
                                
                                    {
                                       try{
                                           key=Integer.parseInt(keyfield.getText());
                                           String decres = "";
                                         
                                          String temp=todec.getText();
                                          char [] p;
                                          int decryptedtext ;
                                          p=temp.toCharArray();
                                          for(int i=0;i<p.length;i++){
                                             decryptedtext = 65 +((int)p[i] - key)%26;
                                          decres += (char)decryptedtext;

                                       }
                                          decrres.setText(decres);
                                          
                                       }
                                       
                                       
                                       
                                          catch (Exception e)
                                          
                                          
                                          
                                          {
                                          }
                                    }
                                 }); 
      
      
      
      
         setTitle("Mozilla FireFox");
         setSize(1610,1600);
         setVisible(true);
      }
   
      public static void main(String []args){
      
         Shiftcipher e=new Shiftcipher();
      }
   }

thnx in advance.

Hello,

I think, the problem is in the encryption and decryption formula. I tried to find it out by googling on it. But i didn't get anything related to the formula that you are using.

So can you please post the encryption and decryption formula here ? and a test calculation.

Let Key = 5 and word to encrypt = 'hello'

Regards,

Hello,

I think, the problem is in the encryption and decryption formula. I tried to find it out by googling on it. But i didn't get anything related to the formula that you are using.

So can you please post the encryption and decryption formula here ? and a test calculation.

Let Key = 5 and word to encrypt = 'hello'

Regards,

Hey again,

I got the answer. Word that you want to encrypt must be entered in UPPERCASE.

Or if you dont want to restrict user to enter word in only uppercase, then use toUpperCase() method (on encryption action) and toLowerCase() method (on decryption action) of String class.

Regards,

hi ..i'm using uppercase words ...but it's not working
the formula is:encrypted(x) = (x + k) mod 26, and,
decrypted(y) = (y − k) mod 26.
a =0;
b=1;
c=2;
d=3;
e=4;
f=5;
g=6;
h=7;
.
.
.
until z=25;

..i will give u and example ...the word "hello" and the key=5
the number if "h"is 7 +5 =13 it should give me "n" .
and thnx for help

Hey again,

Ok, got you. First of all, 7 + 5 = 12 so it should be 'M' LOL :P

Here the solution, Change the formulas to :

Encryption : 65+(((int)e-65) + key)%26
Decryption : 65+(((int)p-65) - key)%26

Tested, Working fine and according to the table.
Input Word : HELLO / Encrypted Word : MJQQT

Regards

thank you very much ...it worked ...but i still have a problem ...when i enter a space in the word that i want to encrypt ..it give me a character in the encrypted result ....what should i do ?:S ..and thnx in advance

hi ,,,can you please give me an answer for my question ....and thnx in advance

hi ,,,can you please give me an answer for my question ....and thnx in advance

Hello again,

Its really simple, Just write a if statement inside the loop to check (p == ' ') . If the condition returns true then skip that char and set encryptedtext variable to ' ' (SPACE) instead of doing the calculation.

If you still cant get it, Please let me know.

Regards,

thnx alot ..it worked ....when i decrypt the word become the plain text and without spaces ..thank you very much ...
i still have some questions :S but i'm embaressed to ask you ..:S

thnx alot ..it worked ....when i decrypt the word become the plain text and without spaces ..thank you very much ...
i still have some questions :S but i'm embaressed to ask you ..:S

lol, Its ok, I will be glad to help you. :D

Regards,

ok ..thank you very much ...i want to ask you where can i find a source code in java for the hill cipher ...coz i have a project to impliment all the ciphers:S ..i implemented some of them ..but i still have alot of ciphers ....and thnx in advance.

This article has been dead for over six months. Start a new discussion instead.