我已经完成了我的凯撒密码,现在我遇到了无法获得
z
而不是|
的问题。我使用 US-ASCII。
这是我的代码:
公共静态无效主{ Final String GERMAN_LANGUAGE_PATTERN = "Werden zwei Glasstaebe mit einem Wolltuch gerieben, dann kann man feststellen, dass sich die beiden Staebe gegenseitig abstossen.Wird das gleiche Experiment mit zwei Kunststoffstaeben wiederholt, dann bleibt das Ergebnis gleich, auch diese be iden Staebe stossen sich gegenseitig ab。 Im Gegensatz dazu ziehen sich ein Glas und ein Kunststoffstab gegenseitig an. Diese mit den Gesetzen der Mechanik nicht zu erklaerende Erscheinung fuehrt man auf Ladungen zurueck. Da sowohl Anziehung als auch Abstossung auftritt, muessen zwei verschiedene Arten von Ladungen Existieren . Man unterscheidet daher Positive und负拉敦根。”; 最终字符串 ENCRYPTED_MESSAGE = "ugjt iwv!fw jcuv fgp eqfg igmpcemv wpf fkt uq twjo wpf gjtg gtyqtdgp.ykg fw ukgjuv, kuv fkgugu xgtuejnwguugnwpiuxgthcjtgp ugjt ngkejv |w mpcemgp.mqornk|kg tvgtg xgthcjtgp ygtfgp kp cpfgtgp xgtcpuvcnvwpigp pcgjgt dgvtcejvgv。"; 私有静态最终字符分隔符 = ' ';
public static void main(String[] args) {
int[] values = {1, 3, 5, 7, 3};
// int[] values2 = {4, 0, 3, 4, 10};
int maxIndex = getIndexOfMaximumEntry(values);
System.out.println(maxIndex);
CaesarChiffre caesarChiffre = new CaesarChiffre();
// Get the histogram of the German language pattern
int[] histogram = getHistogram(caesarChiffre.GERMAN_LANGUAGE_PATTERN);
// Find the index of the maximum entry in the histogram
int significantIndex = getIndexOfMaximumEntry(histogram);
// Get the character at the significantIndex
char significantLetter = (char) significantIndex;
// Calculate quantity and quota
int quantity = histogram[significantIndex];
double quota = ((double) quantity / caesarChiffre.GERMAN_LANGUAGE_PATTERN.length()) * 100;
// Print the results
System.out.println("Most significant letter: " + significantLetter);
System.out.println("Quantity: " + quantity + " times (" + quota + "% of the whole text).");
// Return the significant letter
char result = getSignificantLetter(caesarChiffre.GERMAN_LANGUAGE_PATTERN);
// b) Get the significant letters for the encrypted text and language pattern
char sigOfChiffre = getSignificantLetter(caesarChiffre.ENCRYPTED_MESSAGE);
char sigOfPattern = getSignificantLetter(caesarChiffre.GERMAN_LANGUAGE_PATTERN);
// c) Calculate the shift
int shift = sigOfChiffre - sigOfPattern;
// d) Print the intermediate decoding results
System.out.println("Most significant letter in the pattern text: " + sigOfPattern);
System.out.println("Most significant letter in the encrypted text: " + sigOfChiffre);
System.out.println("Resulting shift: " + shift);
// e) Return the shift
System.out.println("Result of getShift: " + shift);
// 7. Call the decode method from the main method
String decodedText = decode(caesarChiffre.ENCRYPTED_MESSAGE, caesarChiffre.GERMAN_LANGUAGE_PATTERN);
// 8. Print the encrypted and decoded texts
System.out.println("Unreadable, encrypted input text:");
System.out.println(caesarChiffre.ENCRYPTED_MESSAGE);
System.out.println("Readable, decoded output text:");
System.out.println(decodedText);
// 9. Add a comment with the decrypted text and the key to the end of the main method
// Decrypted text: [Your decrypted text]
// Key: Shifted by 2
}
public static int[] getHistogram(String text) {
int[] histogram = new int[128];
String[] words = text.split(String.valueOf(SEPARATOR));
for (String word : words) {
String lowercaseWord = word.toLowerCase();
for (int i = 0; i < lowercaseWord.length(); i++) {
char c = lowercaseWord.charAt(i);
histogram[c]++;
}
}
return histogram;
}
public static int getIndexOfMaximumEntry(int[] values) {
int maxIndex = 0;
for (int i = 1; i < values.length; i++) {
if (values[i] > values[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
}
public static char getSignificantLetter(String text) {
int[] histogram = getHistogram(text);
int significantIndex = getIndexOfMaximumEntry(histogram);
return (char) significantIndex;
}
public static int getShift(String encryptedText, String languagePattern) {
char sigOfChiffre = getSignificantLetter(encryptedText);
char sigOfPattern = getSignificantLetter(languagePattern);
return sigOfChiffre - sigOfPattern;
}
// f) Method to decode the text
// f) Method to decode the text
public static String decode(String encryptedText, String languagePattern) {
// g) Get the shift value
int shift = getShift(encryptedText, languagePattern);
// h) Convert encryptedText to char array
char[] lettersEncryptedText = encryptedText.toCharArray();
// i) Shift characters in the array
for (int i = 0; i < lettersEncryptedText.length; i++) {
char c = lettersEncryptedText[i];
// Check if the character is an ASCII letter or a printable character
if (Character.isLetter(c)) {
// Adjust the shift calculation for both uppercase and lowercase letters
char base = (c >= 'a' && c <= 'z') ? 'a' : 'A';
lettersEncryptedText[i] = (char) ((c - shift - base + 26) % 26 + base);
}
}
// j) Convert the char array to a String
String decoded = new String(lettersEncryptedText);
// Return the decoded text
return decoded;
}
}
我需要
sehr gut! du hast den code geknackt und dir so ruhm und ehre erworben. wie du siehst, ist dieses verschluesselungsverfahren sehr leicht zu knacken. kompliziertere verfahren werden in anderen veranstaltungen naeher betrachtet.
而不是
sehr gut! du hast den code geknackt und dir so ruhm und ehre erworben. wie du siehst, ist dieses verschluesselungsverfahren sehr leicht |u knacken. kompli|iertere verfahren werden in anderen veranstaltungen naeher betrachtet.
这是一个例子。这仅适用于大写值。
String encrypt(char[] a, int n) {
for (int i = 0, c, m = a.length; i < m; i++) {
c = a[i];
if (c < 'A' || c > 'Z') continue;
if ((a[i] = (char) (c + n)) > 'Z') a[i] -= 26;
}
return new String(a);
}
String decrypt(char[] a, int n) {
for (int i = 0, c, m = a.length; i < m; i++) {
c = a[i];
if (c < 'A' || c > 'Z') continue;
if ((a[i] = (char) (c - n)) < 'A') a[i] += 26;
}
return new String(a);
}
输出
encrypt = QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD
decrypt = THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG