在java中,有没有一种方法可以对给定密钥的消息进行加密和解密?
我是编程新手,过去只做过Python。目前,我正在做一项大学作业,要求我实现一个凯撒密码。首先,我得到了一个名为“Cipher”的接口,我的任务是编写一个名为“RotationCipher”的类来实现这个接口 界面:在java中,有没有一种方法可以对给定密钥的消息进行加密和解密?,java,encryption,caesar-cipher,Java,Encryption,Caesar Cipher,我是编程新手,过去只做过Python。目前,我正在做一项大学作业,要求我实现一个凯撒密码。首先,我得到了一个名为“Cipher”的接口,我的任务是编写一个名为“RotationCipher”的类来实现这个接口 界面: public interface Cipher { /** * Encodes the given plain text into a secret cipher text * * @param plainText the plain text to encode *
public interface Cipher {
/**
* Encodes the given plain text into a secret cipher text
*
* @param plainText the plain text to encode
* @return the cipher text
*/
abstract public String encrypt(String plainText);
/**
* Determines the plain text string for a given cipher text.
*
* @param cipherText the cipher text to decode
* @return the plain text original
*/
abstract public String decrypt(String cipherText);
}
abstract class RotationCipher implements Cipher {
protected int shift;
public RotationCipher(int key) {
shift = key;
}
protected String text;
protected abstract String rotate(int shift, String text);
public String encrypt(String plainText) {
String cipherText = rotate(shift, plainText);
return cipherText;
}
public String decrypt(String cipherText) {
shift = shift - 26;
String plainText = rotate(shift, cipherText);
return plainText;
}
}
实现接口的类RotationCipher:
public interface Cipher {
/**
* Encodes the given plain text into a secret cipher text
*
* @param plainText the plain text to encode
* @return the cipher text
*/
abstract public String encrypt(String plainText);
/**
* Determines the plain text string for a given cipher text.
*
* @param cipherText the cipher text to decode
* @return the plain text original
*/
abstract public String decrypt(String cipherText);
}
abstract class RotationCipher implements Cipher {
protected int shift;
public RotationCipher(int key) {
shift = key;
}
protected String text;
protected abstract String rotate(int shift, String text);
public String encrypt(String plainText) {
String cipherText = rotate(shift, plainText);
return cipherText;
}
public String decrypt(String cipherText) {
shift = shift - 26;
String plainText = rotate(shift, cipherText);
return plainText;
}
}
从类RotationCipher(用于加密和解密给定密钥的消息)扩展而来的子类CaesarCipher:
公共类密码扩展了旋转密码{
公共密码(整数密钥){
超级(键);
}
公共字符串旋转(整数移位,字符串文本){
字符串cipherTxt=“”;
字符串“=”;
如果(班次>26){
班次=班次%26;
}else if(移位<0){
班次=(班次%26)+26;
}
int length=text.length();
for(int i=0;i‘z’){
CipherText=CipherText+(char)(ch-(26-shift));
}否则{
密文=密文+c;
}
}else if(字符.isUpperCase(ch)){
字符c=(字符)(ch+shift);
如果(c>‘Z’){
CipherText=CipherText+(char)(ch-(26-shift));
}else if(ch='X'){
CipherText=CipherText+(char)(ch-(26-shift));
}
否则{
密文=密文+c;
}
}
}else if(Character.isleter(ch)=false){
密文=密文+ch;
}
}
返回密文;
}
}
我的讲师还向我提到,编码和解码基本上是相同的操作,但使用的是反转键。我相信“rotate”方法应该通过给定的密钥对给定的文本进行加密和解密。相反,我只使用给定的密钥对给定的文本进行了加密,但我完全不知道如何使用相同的方法进行相反的加密。我有办法做到这一点吗?应该做些什么改变?你的讲师似乎误导了你;解码只需要将
移位
取反即可。例如,如果编码时shift
为10(10
),则解码时shift
必须为负10(-10
)。@ElliotFrisch我的讲师还提供了一个java应用程序来测试该程序,显然测试中还包括了负密钥加密。有没有办法让程序区分用来解密消息的负密钥和用来加密消息的负密钥?我不知道。这对于任何值来说都是一个有趣的属性。rotate方法只是根据当前键值确定的给定数量进行旋转。调用加密/解密时,调用旋转之前调整的值密钥。我认为你的讲师的意图是一个给定的密钥用于加密,它的逆模26是解密。