蓝鹈鹕 Java 第 15 课 Encryption/Decryption
Blue Pelican Java Lesson 15 Encryption/Decryption
我的 Encryption/Decryption 项目有问题,这是项目要求的:
You have just been hired by the CIA as a programmer in the encryption department. Your job is to write a class called Crypto. One method, encrypt, will accept a String that represents the sentence to be encrypted. It will return a String that is the sentence with all v’s (big or small) replaced with “ag’,r”, all m’s (big or small) with “ssad”, all g’s (big or small) with “jeb..w”, and all b’s (big or small) with “dug?/”.
The class contains another method, decrypt, that accepts a String that represents the sentence to be decrypted. In this method the reverse process described above is performed. It returns a String that is the original sentence before encryption.
Use the following Tester class to insure that your methods work.
我的测试仪 class 在下面,我不认为这有任何问题:
//import java.io.*;
import java.util.*;
//import java.io.*;
//import java.util.*;
public class CryptoTester {
public static void main(String args[]) {
Scanner kbReader = new Scanner(System.in);
System.out.print("Enter a sentence that is to be encrypted:");
String sntnc = kbReader.nextLine();
System.out.println("Original Sentence = " + sntnc);
Crypto myCryptObj = new Crypto();
String encryptdSntnc = myCryptObj.encrypt(sntnc);
System.out.println("Encrypted sentence = " + encryptdSntnc);
String decryptdSntnc = myCryptObj.decrypt(encryptdSntnc);
System.out.println("Decrypted sentence = " + decryptdSntnc);
}
}
我的 Encryption/decryption class 是我遇到一些重大问题的地方,它确实加密但不是正确的方式,它也没有解密:
class Crypto {
public String s;
public String acceptor(String sntnc) {
String s;
s = sntnc;
return null;
}
public String encrypt(String sntnc) {
sntnc = sntnc.replaceAll("V", "ag',r");
sntnc = sntnc.replaceAll("v", "ag',r");
sntnc = sntnc.replaceAll("M", "ssad");
sntnc = sntnc.replaceAll("m", "ssad");
sntnc = sntnc.replaceAll("G", "jeb..w");
sntnc = sntnc.replaceAll("g", "jeb..w");
sntnc = sntnc.replaceAll("B", "dug>?/");
sntnc = sntnc.replaceAll("b", "dug>?/");
return sntnc;
}
public String decrypt(String sntnc) {
s = sntnc.replaceAll("ag',r", "V");
s = sntnc.replaceAll("ag',r", "V");
s = sntnc.replaceAll("ssad", "M");
s = sntnc.replaceAll("ssad", "m");
s = sntnc.replaceAll("jeb..w", "g");
s = sntnc.replaceAll("jeb..w", "G");
s = sntnc.replaceAll("dug>?/", "B");
s = sntnc.replaceAll("dug>?/", "b");
return sntnc;
}
}
这是它在控制台中打印出来的内容:
Enter a sentence that is to be encrypted: this is a very big morning
Original sentence: this is a very big morning
Encrypted sentence: this is a ajedug>?/..w',rery dug>?..w ssadorninjedug>?/..w
Decrypted sentence:this is a ajedug>?/..w',rery dug>?..w ssadorninjedug>?/..w
首先,你不应该使用 replaceAll()
。正如 its documentation 所示,该方法将正则表达式作为第一个参数,而不是文字子字符串。 replace()
为所欲为。
其次,该算法没有得到很好的描述,并且不可逆(因为没有办法知道,例如,如果 dug>?/
最初是 b
或 B
).
可以肯定的是,根据您对算法的解释,v
将首先被替换为 ag',r
,然后替换中包含的 g
将被替换为 jeb..w
.
所以,如果要逆向做算法,需要先把jeb..w
换成g
,再把ag',r
换成v
。
我的 Encryption/Decryption 项目有问题,这是项目要求的:
You have just been hired by the CIA as a programmer in the encryption department. Your job is to write a class called Crypto. One method, encrypt, will accept a String that represents the sentence to be encrypted. It will return a String that is the sentence with all v’s (big or small) replaced with “ag’,r”, all m’s (big or small) with “ssad”, all g’s (big or small) with “jeb..w”, and all b’s (big or small) with “dug?/”.
The class contains another method, decrypt, that accepts a String that represents the sentence to be decrypted. In this method the reverse process described above is performed. It returns a String that is the original sentence before encryption.
Use the following Tester class to insure that your methods work.
我的测试仪 class 在下面,我不认为这有任何问题:
//import java.io.*;
import java.util.*;
//import java.io.*;
//import java.util.*;
public class CryptoTester {
public static void main(String args[]) {
Scanner kbReader = new Scanner(System.in);
System.out.print("Enter a sentence that is to be encrypted:");
String sntnc = kbReader.nextLine();
System.out.println("Original Sentence = " + sntnc);
Crypto myCryptObj = new Crypto();
String encryptdSntnc = myCryptObj.encrypt(sntnc);
System.out.println("Encrypted sentence = " + encryptdSntnc);
String decryptdSntnc = myCryptObj.decrypt(encryptdSntnc);
System.out.println("Decrypted sentence = " + decryptdSntnc);
}
}
我的 Encryption/decryption class 是我遇到一些重大问题的地方,它确实加密但不是正确的方式,它也没有解密:
class Crypto {
public String s;
public String acceptor(String sntnc) {
String s;
s = sntnc;
return null;
}
public String encrypt(String sntnc) {
sntnc = sntnc.replaceAll("V", "ag',r");
sntnc = sntnc.replaceAll("v", "ag',r");
sntnc = sntnc.replaceAll("M", "ssad");
sntnc = sntnc.replaceAll("m", "ssad");
sntnc = sntnc.replaceAll("G", "jeb..w");
sntnc = sntnc.replaceAll("g", "jeb..w");
sntnc = sntnc.replaceAll("B", "dug>?/");
sntnc = sntnc.replaceAll("b", "dug>?/");
return sntnc;
}
public String decrypt(String sntnc) {
s = sntnc.replaceAll("ag',r", "V");
s = sntnc.replaceAll("ag',r", "V");
s = sntnc.replaceAll("ssad", "M");
s = sntnc.replaceAll("ssad", "m");
s = sntnc.replaceAll("jeb..w", "g");
s = sntnc.replaceAll("jeb..w", "G");
s = sntnc.replaceAll("dug>?/", "B");
s = sntnc.replaceAll("dug>?/", "b");
return sntnc;
}
}
这是它在控制台中打印出来的内容:
Enter a sentence that is to be encrypted: this is a very big morning Original sentence: this is a very big morning Encrypted sentence: this is a ajedug>?/..w',rery dug>?..w ssadorninjedug>?/..w Decrypted sentence:this is a ajedug>?/..w',rery dug>?..w ssadorninjedug>?/..w
首先,你不应该使用 replaceAll()
。正如 its documentation 所示,该方法将正则表达式作为第一个参数,而不是文字子字符串。 replace()
为所欲为。
其次,该算法没有得到很好的描述,并且不可逆(因为没有办法知道,例如,如果 dug>?/
最初是 b
或 B
).
可以肯定的是,根据您对算法的解释,v
将首先被替换为 ag',r
,然后替换中包含的 g
将被替换为 jeb..w
.
所以,如果要逆向做算法,需要先把jeb..w
换成g
,再把ag',r
换成v
。