Java 创建替换密码以将正常字母表字符串映射到混乱字母表字符串并加密消息的最佳方法是什么? 公共静态字符串EncodeString(){ String plaintext=“Hello Jon”; 字符串字母表=“abcdefghijklmnopqrstuvxyz”; 字符串替换=“zyxwvutsrqponmlkjihgfedcba”; 对于(int i=0;i
您的代码给出了一个错误,因为您试图将Java 创建替换密码以将正常字母表字符串映射到混乱字母表字符串并加密消息的最佳方法是什么? 公共静态字符串EncodeString(){ String plaintext=“Hello Jon”; 字符串字母表=“abcdefghijklmnopqrstuvxyz”; 字符串替换=“zyxwvutsrqponmlkjihgfedcba”; 对于(int i=0;i,java,encryption,substitution,alphabet,Java,Encryption,Substitution,Alphabet,您的代码给出了一个错误,因为您试图将明文和alPabe用作数组,但它们不是数组。因此明文[int]无法工作。相反,我将使用字符串.charAt(int)。这允许您在不使用array的情况下检查两个字符是否相等。我认为,通过用户可以更改混乱的字母表,这更容易实现。代码将是: public static String EncodeString() { String plaintext = "Hello Jon"; String alphabet = "
明文
和alPabe
用作数组,但它们不是数组。因此明文[int]
无法工作。相反,我将使用字符串.charAt(int)
。这允许您在不使用array的情况下检查两个字符是否相等。我认为,通过用户可以更改混乱的字母表,这更容易实现。代码将是:
public static String EncodeString() {
String plaintext = "Hello Jon";
String alphabet = "abcdefghijklmnopqrstuvwxyz";
String Substituion = "zyxwvutsrqponmlkjihgfedcba";
for(int i=0; i<plaintext.length(); i++){
for(int j=0; j<alphabet.length(); j++){
if(plaintext[i]==alphabet[j]) {
//not sure what to do here because the code above is giving an error
}
}
}
公共静态字符串EncodeString(){
String plaintext=“Hello Jon”;
String alphabet=“abcdefghijklmnopqrstuvxyz”//添加了空格
字符串替换=“zyxwvutsrqponmlkjihgfedcba”;
字符串加密_message=“”;
对于(int i=0;i来说,这里不需要重新设计轮子。只需使用java.util.Base64类。尽管不太安全,但它在安全性方面仍比您试图使用的要优越得多,而且……编码和使用起来要容易得多:
public static String EncodeString() {
String plaintext = "Hello Jon";
String alphabet = "abcdefghijklmnopqrstuvwxyz "; //space added
String substitution = "zyxwvutsrqponmlkjihgfedcba ";
String encrypted_message = "";
for(int i=0; i<plaintext.length(); i++){
for(int j=0; j<alphabet.length(); j++){
if(plaintext.charAt(i)==alphabet.charAt(j)) {
encrypted_message += Substitution.charAt(j); //This adds the right character of the jumbled aplhabet to the encrypted message
}
}
}
return encrypted_message; //returns the encrypted message
}
控制台输出将为:
public class MyClass {
public static void main(String[] args) {
String message = "\nWhat is your name?\nYour Address?\nYour Phone Number?";
System.out.println("Original Message: --> " + message);
String encodedMessage = encode(message);
System.out.println("Encoded Message: --> " + encodedMessage);
System.out.println("Decoded Message: --> " + decode(encodedMessage));
}
public static String encode(String inputString) {
// Getting MIME encoder
java.util.Base64.Encoder encoder = java.util.Base64.getMimeEncoder();
// Encode supplied String and return.
return encoder.encodeToString(inputString.getBytes());
}
public static String decode(String encodedString) {
// Getting MIME Decoder
java.util.Base64.Decoder decoder = java.util.Base64.getMimeDecoder();
// Decode supplied Encoded String and return.
return new String(decoder.decode(encodedString));
}
}
我将使用额外的参数“password”对原始字母表字符串进行可预测的排列(以防出错),并动态生成一个hashmap,将原始字母表的输入字母映射到生成字母表的输出字母。
Original Message: -->
What is your name?
Your Address?
Your Phone Number?
Encoded Message: --> CldoYXQgaXMgeW91ciBuYW1lPwpZb3VyIEFkZHJlc3M/CllvdXIgUGhvbmUgTnVtYmVyPw==
Decoded Message: -->
What is your name?
Your Address?
Your Phone Number?