您好我是Eclipse的新手,我目前遇到了Caesar Cipher的问题。我编写的代码大部分是唯一的问题是让X,Y,Z再次开始“环绕”。我相信我应该使用%运算符,我不知道如何实现它。
答案 0 :(得分:1)
如果一切都是小写,你可以为每个角色做这样的事情。
if (character > 'z') {
character -= 'z';
}
另一种方法是使用与Wikipedia article相同的方法。
character = (character - 'a' + const_to_add) % 26 + 'a';
答案 1 :(得分:0)
您好...我已经为凯撒密码创建了一个Java客户端服务器应用程序...我创建了一个可以正确解密文本的新公式... 对不起只有小写..!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class ceasarserver extends JFrame implements ActionListener
{
static String cs="abcdefghijklmnopqrstuvwxyz";
static JLabel l1,l2,l3,l5,l6;
JTextField t1;
JButton close,b1;
static String en;
int num=0;
JProgressBar progress;
ceasarserver()
{
super("SERVER");
JPanel p=new JPanel(new GridLayout(10,1));
l1=new JLabel("");
l2=new JLabel("");
l3=new JLabel("");
l5=new JLabel("");
l6=new JLabel("Enter the Key...");
t1=new JTextField(30);
progress = new JProgressBar(0, 20);
progress.setValue(0);
progress.setStringPainted(true);
close=new JButton("Close");
close.setMnemonic('C');
close.setPreferredSize(new Dimension(300, 25));
close.addActionListener(this);
b1=new JButton("Decrypt");
b1.setMnemonic('D');
b1.addAction
Listener(this);
p.add(l1);
p.add(l2);
p.add(l3);
p.add(l6);
p.add(t1);
p.add(b1);
p.add(progress);
p.add(l5);
p.add(close);
add(p);
setVisible(true);
pack();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==close)
System.exit(0);
else if(e.getSource()==b1)
{
int key=Integer.parseInt(t1.getText());
String d="";
int i=0,j,k;
while(i<en.length())
{
j=cs.indexOf(en.charAt(i));
k=(j+(26-key))%26;
d=d+cs.charAt(k);
i++;
}
while (num < 21)
{
progress.setValue(num);
try {
Thread.sleep(100);
} catch (InterruptedException ex) { }
progress.setValue( num );
Rectangle progressRect = progress.getBounds();
progressRect.x=0;
progressRect.y=0;
progress.paintImmediately( progressRect );
num++;
}
l5.setText("Decrypted text: "+d);
}
}
public static void main(String args[])throws IOException
{
new ceasarserver();
String strm =new String();
ServerSocket
ss=new ServerSocket(4321);
l1.setText("Secure data transfer Server Started....");
Socket s=ss.accept();
l2.setText("Client Connected !");
while(true)
{
Scanner br1= new Scanner(s.getInputStream());
en=br1.nextLine();
l3.setText("Client:"+en);
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class ceasarclient extends JFrame
{
String cs="abcdefghijklmnopqrstuvwxyz";
static JLabel l1,l2,l3,l4,l5;
JButton b1,b2,b3;
JTextField t1,t2;
JProgressBar progress;
int num=0;
String en="";
ceasarclient(final Socket s)
{
super("CLIENT");
JPanel p=new JPanel(new GridLayout(10,1));
setSize(500,500);
t1=new JTextField(30);
b1=new JButton("Send");
b1.setMnemonic('S');
b2=new JButton("Close");
b2.setMnemonic('C');
l1=new JLabel("Welcome to Secure Data transfer!");
l2=new JLabel("Enter the word here...");
l3=new JLabel("");
l4=new JLabel("Enter the Key:");
b3=new JButton("Encrypt");
b3.setMnemonic('E');
t2=new JTextField(30);
progress = new JProgressBar(0, 20);
progress.setValue(0);
progress.setStringPainted(true);
p.add(l1);
p.add(l2);
p.add(t1);
p.add(l4);
p.add(t2);
p.add(b3);
p.add(progress);
p.add(b1);
p.add(l3);
p.add(b2);
add(p);
setVisible(true);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try{
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println(en);
}catch(Exception ex){};
l3.setText("Encrypted Text Sent.");
}
});
b3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String strw=t1.getText();
int key=Integer.parseInt(t2.getText());
int i=0,j,k;
while(i<strw.length())
{
j=cs.indexOf(strw.charAt(
i));
k=(j+key)%26;
en=en+cs.charAt(k);
i++;
}
while (num < 21)
{
progress.setValue(num);
try {
Thread.sleep(100);
} catch (InterruptedException exe) { }
progress.setValue( num );
Rectangle progressRect = progress.getBounds();
progressRect.x=0;
progressRect.y=0;
progress.paintImmediately( progressRect );
num++;
}
}
});
b2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
pack();
}
public static void main(String args[])throws IOException
{
final Socket s =new Socket(InetAddress.getLocalHost(),4321);
new ceasarclient(s);
}
}