我有一个项目,必须在其中加密字符串,然后再解密。我得到了加密部分,但是我找不到解密它的方法。
加密文件
file = Input.readFile("txt.txt").toUpperCase();
ArrayList<String> mylist = new ArrayList<String>();
mylist.add("\uD83C\uDCA1"); mylist.add("\uD83C\uDCB1"); mylist.add("\uD83C\uDCC1"); mylist.add("\uD83C\uDCD1");
mylist.add("\uD83C\uDCA2"); mylist.add("\uD83C\uDCB2"); mylist.add("\uD83C\uDCC2"); mylist.add("\uD83C\uDCD2");
mylist.add("\uD83C\uDCA3"); mylist.add("\uD83C\uDCB3"); mylist.add("\uD83C\uDCC3"); mylist.add("\uD83C\uDCD3");
mylist.add("\uD83C\uDCA4"); mylist.add("\uD83C\uDCB4"); mylist.add("\uD83C\uDCC4"); mylist.add("\uD83C\uDCD4");
mylist.add("\uD83C\uDCA5"); mylist.add("\uD83C\uDCB5"); mylist.add("\uD83C\uDCC5"); mylist.add("\uD83C\uDCD5");
mylist.add("\uD83C\uDCA6"); mylist.add("\uD83C\uDCB6"); mylist.add("\uD83C\uDCC6"); mylist.add("\uD83C\uDCD6");
mylist.add("\uD83C\uDCA7"); mylist.add("\uD83C\uDCB7"); mylist.add("\uD83C\uDCC7"); mylist.add("\uD83C\uDCD7");
mylist.add("\uD83C\uDCA8"); mylist.add("\uD83C\uDCB8"); mylist.add("\uD83C\uDCC8"); mylist.add("\uD83C\uDCD8");
mylist.add("\uD83C\uDCA9"); mylist.add("\uD83C\uDCB9"); mylist.add("\uD83C\uDCC9"); mylist.add("\uD83C\uDCD9");
mylist.add("\uD83C\uDCAA"); mylist.add("\uD83C\uDCBA"); mylist.add("\uD83C\uDCCA"); mylist.add("\uD83C\uDCDA");
mylist.add("\uD83C\uDCAB"); mylist.add("\uD83C\uDCBB"); mylist.add("\uD83C\uDCCB"); mylist.add("\uD83C\uDCDB");
mylist.add("\uD83C\uDCAD"); mylist.add("\uD83C\uDCBD"); mylist.add("\uD83C\uDCCD"); mylist.add("\uD83C\uDCDD");
mylist.add("\uD83C\uDCAE"); mylist.add("\uD83C\uDCBE"); mylist.add("\uD83C\uDCCE"); mylist.add("\uD83C\uDCDE");
//Encypt
//going backwards
for(int i = file.length(); i > 0 ; i--){
index ++;
//shuffle the list
//Collections.shuffle(mylist);
char symbol = file.charAt(file.length() - index);
//change the ascii number by 1-10
int ascii = ((int)symbol);
//if ascii is between A-Z or a-z display a card
if( ((ascii >= (int)'A' && ascii <= (int)'Z'))) {
build += mylist.get(ascii - 65);
}
//if the ascii number is divisible by 2, display a 0, if not a 1
else{
if(ascii % 2 == 0){
build += 0;
}else{
build += 1;
}
}
}
try{
Files.write(Paths.get("encryptedFile.txt"), build.getBytes());
}catch(Exception e){
System.out.println("Oh NO!");
}
System.out.println("\nYour encrypted message is: \n" + build);
System.out.println("---------------------------------------------\t");
//End Encypt
解密
String file = Input.readFile("encryptedFile.txt");
String password = "",build2="";
ArrayList<String> mylist = new ArrayList<String>();
mylist.add("\uD83C\uDCA1"); mylist.add("\uD83C\uDCB1"); mylist.add("\uD83C\uDCC1"); mylist.add("\uD83C\uDCD1");
mylist.add("\uD83C\uDCA2"); mylist.add("\uD83C\uDCB2"); mylist.add("\uD83C\uDCC2"); mylist.add("\uD83C\uDCD2");
mylist.add("\uD83C\uDCA3"); mylist.add("\uD83C\uDCB3"); mylist.add("\uD83C\uDCC3"); mylist.add("\uD83C\uDCD3");
mylist.add("\uD83C\uDCA4"); mylist.add("\uD83C\uDCB4"); mylist.add("\uD83C\uDCC4"); mylist.add("\uD83C\uDCD4");
mylist.add("\uD83C\uDCA5"); mylist.add("\uD83C\uDCB5"); mylist.add("\uD83C\uDCC5"); mylist.add("\uD83C\uDCD5");
mylist.add("\uD83C\uDCA6"); mylist.add("\uD83C\uDCB6"); mylist.add("\uD83C\uDCC6"); mylist.add("\uD83C\uDCD6");
mylist.add("\uD83C\uDCA7"); mylist.add("\uD83C\uDCB7"); mylist.add("\uD83C\uDCC7"); mylist.add("\uD83C\uDCD7");
mylist.add("\uD83C\uDCA8"); mylist.add("\uD83C\uDCB8"); mylist.add("\uD83C\uDCC8"); mylist.add("\uD83C\uDCD8");
mylist.add("\uD83C\uDCA9"); mylist.add("\uD83C\uDCB9"); mylist.add("\uD83C\uDCC9"); mylist.add("\uD83C\uDCD9");
mylist.add("\uD83C\uDCAA"); mylist.add("\uD83C\uDCBA"); mylist.add("\uD83C\uDCCA"); mylist.add("\uD83C\uDCDA");
mylist.add("\uD83C\uDCAB"); mylist.add("\uD83C\uDCBB"); mylist.add("\uD83C\uDCCB"); mylist.add("\uD83C\uDCDB");
mylist.add("\uD83C\uDCAD"); mylist.add("\uD83C\uDCBD"); mylist.add("\uD83C\uDCCD"); mylist.add("\uD83C\uDCDD");
mylist.add("\uD83C\uDCAE"); mylist.add("\uD83C\uDCBE"); mylist.add("\uD83C\uDCCE"); mylist.add("\uD83C\uDCDE");
build2 = "";
for(int i = 0; i < file.length(); i++){
char symbol = file.charAt(i);
int ascii = ((int)symbol);
int pos = mylist.indexOf();
build2 += pos + ",";
}
System.out.println("\nYour decrypted message is: \n" + build2);
try{
Files.write(Paths.get("dencryptedFile.txt"), build2.getBytes());
}catch(Exception e){
System.out.println("Oh NO!");
}
//End Decypt
现在原始文件是
“ A B C”
加密后变成
“ 00”
我想要
“ 00”
转回
“ A B C”
但是我不知道怎么做。
https://repl.it/@MatthewZeman/EncryptionCode-加密 https://repl.it/@MatthewZeman/DecryptionCode-解密
答案 0 :(得分:0)
在下面,您会在注释中找到具有解释说明的工作代码。
import java.util.*;
class EncryptDecrypt {
public static void main(String[] args) {
// List to convert ascii characters to cards
ArrayList<String> mylist = new ArrayList<String>();
mylist.add("\uD83C\uDCA1"); mylist.add("\uD83C\uDCB1");
mylist.add("\uD83C\uDCC1"); mylist.add("\uD83C\uDCD1");
mylist.add("\uD83C\uDCA2"); mylist.add("\uD83C\uDCB2");
mylist.add("\uD83C\uDCC2"); mylist.add("\uD83C\uDCD2");
mylist.add("\uD83C\uDCA3"); mylist.add("\uD83C\uDCB3");
mylist.add("\uD83C\uDCC3"); mylist.add("\uD83C\uDCD3");
mylist.add("\uD83C\uDCA4"); mylist.add("\uD83C\uDCB4");
mylist.add("\uD83C\uDCC4"); mylist.add("\uD83C\uDCD4");
mylist.add("\uD83C\uDCA5"); mylist.add("\uD83C\uDCB5");
mylist.add("\uD83C\uDCC5"); mylist.add("\uD83C\uDCD5");
mylist.add("\uD83C\uDCA6"); mylist.add("\uD83C\uDCB6");
mylist.add("\uD83C\uDCC6"); mylist.add("\uD83C\uDCD6");
mylist.add("\uD83C\uDCA7"); mylist.add("\uD83C\uDCB7");
mylist.add("\uD83C\uDCC7"); mylist.add("\uD83C\uDCD7");
mylist.add("\uD83C\uDCA8"); mylist.add("\uD83C\uDCB8");
mylist.add("\uD83C\uDCC8"); mylist.add("\uD83C\uDCD8");
mylist.add("\uD83C\uDCA9"); mylist.add("\uD83C\uDCB9");
mylist.add("\uD83C\uDCC9"); mylist.add("\uD83C\uDCD9");
mylist.add("\uD83C\uDCAA"); mylist.add("\uD83C\uDCBA");
mylist.add("\uD83C\uDCCA"); mylist.add("\uD83C\uDCDA");
mylist.add("\uD83C\uDCAB"); mylist.add("\uD83C\uDCBB");
mylist.add("\uD83C\uDCCB"); mylist.add("\uD83C\uDCDB");
mylist.add("\uD83C\uDCAD"); mylist.add("\uD83C\uDCBD");
mylist.add("\uD83C\uDCCD"); mylist.add("\uD83C\uDCDD");
mylist.add("\uD83C\uDCAE"); mylist.add("\uD83C\uDCBE");
mylist.add("\uD83C\uDCCE"); mylist.add("\uD83C\uDCDE");
// Example string (can be read from file if needed)
String text = "A B C";
// The encrypted string
String encrypted = "";
// Encrypt
// Note: You can use "i" instead of using another variable "index"
for(int i = 0; i < text.length() ; i++){
// Get next character from text (or file)
char symbol = text.charAt(i);
// Convert character to int
int ascii = ((int)symbol);
//if ascii is between A-Z or a-z display a card
if( ((ascii >= (int)'A' && ascii <= (int)'Z'))) {
encrypted += mylist.get(ascii - 65);
} else if(ascii % 2 == 0) { // Using "else if" here makes the code much cleaner
encrypted += "0"; // Here you should append a character "0" instead of the integer 0
} else{
encrypted += "1"; // Here you should append a character "1" instead of the integer 1
}
}
System.out.println("\nYour encrypted message is: \n" + encrypted);
System.out.println("---------------------------------------------\t");
// The decrypted text
String decrypted = "";
// Decrypting this is a little more difficult for multiple reasons.
// 1. The cards are 2 character symbols. Meaning we have to check if the next character is a card or not
// 2. Then, once we know if it is a card or not, we need to take apropriate action
// 3. Inherently we loose some information while encrypting. Therefore we will never get back the exact same string
// Storage to store one character if we encounter a card
String charStore = "";
// Loop through text
for(int i = 0; i < encrypted.length(); i++){
// Get next character
char nextChar = encrypted.charAt(i);
if(charStore.length() == 0) {
// Our character store is empty, so we check what the next character is
if(nextChar == "0".charAt(0)) {
// We found our next character to be a "0". So we convert it to a space
// This is where we lost information. we don't know exactly which character was here before
decrypted += " ";
} else if (nextChar == "1".charAt(0)) {
// We found our next character to be a "1". So we convert it to a !
// This is where we also lost information. we don't know exactly which character was here before
decrypted += "!";
} else {
// The character is neither a "0" nor a "1", so it has to be part of a chard, which is 2 characters in length
// We store the current character in our storage "charStore"
charStore += nextChar;
}
} else {
// Our character store contained at least one character.
// So we can safely assume that together with the next character we have a card symbol
// Concatenate stored and current character
charStore += nextChar;
// Search for the card in the list
// Remember that you subtracted 65 from previously, so we have to add 65 here.
// Also: You forgot the argument inside the "indexOf" in your code
decrypted += (char)(mylist.indexOf(charStore) + 65);
// Reset the character store
charStore = "";
}
}
System.out.println("\nYour decrypted message is: \n" + decrypted);
}
}