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.