Java 如何在尝试解密消息时找出加密密钥是什么
所以我试图在这里运行失败的代码,我发现自己在寻找用于加密消息的密钥,但什么也没有得到。我正在寻找这段代码是如何工作的。我应该得到一个已解密的加密消息,并得到用于加密消息的两个密钥。在我的测试方法中,我已经包含了需要解密的消息,但是它们中的一些被进一步加密了。例如,我已准备好解密这些消息:Java 如何在尝试解密消息时找出加密密钥是什么,java,encryption,cryptography,Java,Encryption,Cryptography,所以我试图在这里运行失败的代码,我发现自己在寻找用于加密消息的密钥,但什么也没有得到。我正在寻找这段代码是如何工作的。我应该得到一个已解密的加密消息,并得到用于加密消息的两个密钥。在我的测试方法中,我已经包含了需要解密的消息,但是它们中的一些被进一步加密了。例如,我已准备好解密这些消息: String decrypt1 = cb.decrypt("UUU VVV EGHI"); String decrypt2 = cb.decrypt("Pi cddc qt x
String decrypt1 = cb.decrypt("UUU VVV EGHI");
String decrypt2 = cb.decrypt("Pi cddc qt xc iwt rdcutgtcrt gddb lxiw ndjg wpi dc udg p hjgegxht epgin. NTAA ADJS!");
String decrypt3 = cb.decryptMod("Pi cddc qt xc iwt rdcutgtcrt gddb lxiw ndjg wpi dc udg p hjgegxht epgin. NTAA ADJS!");
String decTwoKeys = cb.decryptTwoKeys("Hfs cpwewloj loks cd Hoto kyg Cyy.");
String brake = cb.halfOfString("Qbkm Zgis", 1);
String decrypt4 = cb.decryptMod(fr.asString());
但是decryptTwoKeys似乎无法使HalfofString方法正常工作(我得到的解析字符串混合了索引1和索引2中的字母),也无法打印出解密加密消息所需的两个密钥。我应该从索引0开始,每隔一行计数一个字母,也应该每隔一行从索引1中获取一个字母,然后在解密隐藏消息后将两个新字符串编译成一个。相反,两把钥匙都是0。我知道程序可能需要更长的字符串才能找到这两个键,但是当我使用更长的字符串时,例如当我加载文本文件时,我会收到一个错误,说Java lang object:out of bounds
。我确实得到了一些结果,但在对数据进行一定次数的解析后,程序崩溃了。我还想知道什么密钥被用来加密消息
String decrypt2 = cb.decrypt("Pi cddc qt xc iwt rdcutgtcrt gddb lxiw ndjg wpi dc udg p hjgegxht epgin. NTAA ADJS!");
String decrypt3 = cb.decryptMod("Pi cddc qt xc iwt rdcutgtcrt gddb lxiw ndjg wpi dc udg p hjgegxht epgin. NTAA ADJS!");
上述位工作正常,并解密到正确的消息
String brake = cb.halfOfString("Qbkm Zgis", 1);
上面的这一位在本例中工作得很好,因为它确实打印出bmZi
,但在decryptTwoKeys方法中调用时似乎不起作用
那么,我如何找出用于加密原始消息的密钥是什么,第二,我如何找出用于加密的密钥在字符串中是什么
String decrypt2 = cb.decrypt("Pi cddc qt xc iwt rdcutgtcrt gddb lxiw ndjg wpi dc udg p hjgegxht epgin. NTAA ADJS!");
String decrypt3 = cb.decryptMod("Pi cddc qt xc iwt rdcutgtcrt gddb lxiw ndjg wpi dc udg p hjgegxht epgin. NTAA ADJS!");
任何帮助都将不胜感激
以下是我目前的代码:
import edu.duke.FileResource;
public class CaesarBreaker {
/**
* Fint max index in an array
* @param freqs
* @return
*/
public int maxIndex(int[] freqs) {
int max = freqs[0];
for (int i = 1; i < freqs.length; i++) {
if (freqs[i] > max) {
max = freqs[i];
}
}
//System.out.println(max);
return freqs[max];
//return max;
}
/**
*
* @param message
* @return
*/
public int[] countLetters(String message) {
String abc = "abcdefghijklmnopqrstuvwxyz";
int[] counts = new int[26];
for (int k = 0; k<message.length(); k++) {
char ch = Character.toLowerCase(message.charAt(k));
int dex = abc.indexOf(ch);
if (dex != -1) {
counts[dex] += 1;
}
}
//System.out.println(counts);
return counts;
}
/**
* Decrypt message yhal has been in big letters and encrypted with one key
* @param encrypted
* @return
*/
public String decrypt (String encrypted) {
CaesarCipher cc = new CaesarCipher();
int[] freqs = countLetters(encrypted);
int maxDex = maxIndex(freqs);
int dkey = maxDex - 4;
if(maxDex < 4) {
dkey = 26 - ( 4 - maxDex);
}
//System.out.println(cc.encrypt(encrypted, 25-dkey));
return cc.encrypt(encrypted, 25-dkey); // we can use encryptMod method
}
/**
* Decrypt message yhal has been in big and small letters and encrypted with one key
* @param encrypted
* @return
*/
public String decryptMod (String encrypted) {
CaesarCipherMy ccM = new CaesarCipherMy();
int[] freqs = countLettersMod(encrypted);
int maxDex = maxIndex(freqs);
int dkey = maxDex - 4;
if(maxDex < 4) {
dkey = 26 - ( 4 - maxDex);
}
//System.out.println(ccM.encryptMod(encrypted, 33-dkey));
return ccM.encryptMod(encrypted, 33-dkey); // we can use encryptMod method
}
public int[] countLettersMod(String message) {
String abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int[] counts = new int[52];
for (int k = 0; k<message.length(); k++) {
//char ch = Character.toLowerCase(message.charAt(k));
char ch = message.charAt(k);
int dex = abc.indexOf(ch);
if (dex != -1) {
counts[dex] += 1;
}
}
//System.out.println(counts);
return counts;
}
/**
* In order to decrypt the encrypted String, it may be easier to split the String
* into two Strings, one String of all the letters encrypted with key1
* and one String of all the letters encrypted with key2 . Then use the algorithm from the
* lesson to determine the key for each String, and then use those keys and the two key
* encryption method to decrypt the original encrypted message.
* For example, if the encrypted message was “Qbkm Zgis” , then you would split this String into
* two Strings: “Qk gs” , representing the characters in the odd number positions and “bmZi”
* representing the characters in the even number positions. Then you would get the key for each
* half String and use the two key encryption method to find the message. Note this example is so
* small it likely won’t find the keys, but it illustrates how to take the Strings apart.
*
* This method attempts to determine the two keys used to encrypt the message,
* prints the two keys, and then returns the decrypted String with those two keys.
* More specifically, this method should:
* ○ Calculate a String of every other character starting with the first character of the
* encrypted String by calling halfOfString.
* ○ Calculate a String of every other character starting with the second character of
* the encrypted String.
* ○ Then calculate the key used to encrypt each half String.
* ○ You should print the two keys found.
* ○ Calculate and return the decrypted String using the encryptTwoKeys method
* from your CaesarCipher class, again making sure it is in the same folder as your
* CaesarBreaker class.
*
* @param encrypted
* @return
*/
public String decryptTwoKeys (String encrypted) {
CaesarCipherMy ccM = new CaesarCipherMy();
String firstHalf = halfOfString(encrypted, 0);
String secondHalf = halfOfString(encrypted, 1);
System.out.println("firstHalf" + "\t" + firstHalf + "\t" + "secondHalf" + "\t" + secondHalf);
int firstKey = getKey(firstHalf);
int secondKey = getKey(secondHalf);
System.out.println("firstKey" + "\t" + firstKey + "\t" + "secondKey" + "\t" + secondKey);
System.out.println(ccM.encryptTwoKeys(encrypted, firstKey, secondKey));
return ccM.encryptTwoKeys(encrypted, firstKey, secondKey);
}
/**
* This method should return a new String that is every other character from message
* starting with the start position. For example, the call halfOfString(“Qbkm Zgis”, 0)
* returns the String “Qk gs” and the call halfOfString(“Qbkm Zgis”, 1) returns the String
* “bm Zi” . Be sure to test this method with a small example.
*
* @param message
* @param start
* @return
*/
public String halfOfString(String message, int start) {
if (start != 1 || start != 0) {
StringBuilder sb = new StringBuilder();
for (char c : message.toCharArray()) {
int idx = message.indexOf(c);
if(idx%2 == start) {
sb = sb.append(c);
}
}
return sb.toString();
}
return null;
}
/**
* This method should call countLetters to get an array of the letter frequencies in String s
* and then use maxIndex to calculate the index of the largest letter frequency, which is
* the location of the encrypted letter ‘e’, which leads to the key, which is returned.
*
* @param s
* @return
*/
public int getKey(String s) {
int[] freqs = countLettersMod(s);
return maxIndex(freqs);
}
public void testCaesarBreaker() {
CaesarBreaker cb = new CaesarBreaker();
FileResource fr = new FileResource();
CaesarCipherMy ccM = new CaesarCipherMy();
//("FIRST LEGION ATTACK EAST FLANK!", 23)
//String decrypt = cb.decrypt("CFOPQ IBDFLK XQQXZH BXPQ CIXKH!");
//String ret4 = ccM.encrypt("XXX YYY HJKL", 23);
String decrypt1 = cb.decrypt("UUU VVV EGHI");
String decrypt2 = cb.decrypt("Pi cddc qt xc iwt rdcutgtcrt gddb lxiw ndjg wpi dc udg p hjgegxht epgin. NTAA ADJS!");
String decrypt3 = cb.decryptMod("Pi cddc qt xc iwt rdcutgtcrt gddb lxiw ndjg wpi dc udg p hjgegxht epgin. NTAA ADJS!");
String decTwoKeys = cb.decryptTwoKeys("Hfs cpwewloj loks cd Hoto kyg Cyy.");
String brake = cb.halfOfString("Qbkm Zgis", 1);
String decrypt4 = cb.decryptMod(fr.asString());
//String decrypt5 = cb.decryptMod(fr.asString());
//String getKey = cb.getKey();
//System.out.println(ret4);
//System.out.println(decrypt5);
System.out.println(decrypt4);
System.out.println(decrypt1);
System.out.println(decrypt2);
System.out.println(decrypt3);
System.out.println(brake);
//System.out.println(decTwoKeys);
}
}
导入edu.duke.FileResource;
公营飞机{
/**
*数组中的Fint max索引
*@param-freqs
*@返回
*/
公共整数最大索引(整数[]频率){
int max=频率[0];
对于(int i=1;i最大值){
最大值=频率[i];
}
}
//系统输出打印项次(最大值);
返回频率[最大值];
//返回最大值;
}
/**
*
*@param消息
*@返回
*/
公共int[]计数字母(字符串消息){
字符串abc=“abcdefghijklmnopqrstuvxyz”;
int[]计数=新的int[26];
对于(int k=0;k您的halfOfString
方法中有很多错误。此方法应该可以工作:
public static String halfOfString(String message, int start) {
StringBuilder result = new StringBuilder();
for (int i = start; i < message.length(); i+=2) {
result.append(message.charAt(i));
}
return result;
}
公共静态字符串(字符串消息,int start){
StringBuilder结果=新建StringBuilder();
for(int i=start;i
不幸的是,我发现您的代码非常混乱,很可能在其他方法中还有一些错误