- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用移位密码来解码消息。我的一些角色翻译得很好,但其他的则不然。我无法弄清楚问题是什么。
public class ShiftCipher {
public static void main(String[] args) {
System.out.println(cipher("F30MDAAFEMA1MI0EF0D9", 14));
}
static String cipher(String msg, int shift){
String characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
String DecryptedMessege = "";
String DecryptedChar = "";
int trueShift;
boolean in = false; //Debugging
for(int x = 0; x < msg.length(); x++){
for (int y = 0; y < characters.length(); y++ ) {
if (msg.charAt(x) == characters.charAt(y)){
if (y+shift <= characters.length()){
trueShift = y + shift;
in = true; //Debugging
}
else {
trueShift = shift - (characters.length() - y);
in = false; //Debugging
}
DecryptedChar = new StringBuilder().append(characters.charAt(trueShift)).toString();
System.out.println(DecryptedChar + " " + in + " " + trueShift); //Debugging
}
}
DecryptedMessege = DecryptedMessege + DecryptedChar;
}
return DecryptedMessege;
}
}
一些早期字母被错误地移至 -1。输出应为“THE ROOTS OF WESTERN”,但改为“TGD ROOTS OE WDSTDRN”。
有人知道为什么这不起作用吗?欢迎任何意见。
最佳答案
使用模 %
(整数除法的余数):模 4 会计算 0, 1, 2, 3, 0, 1, 2, 3, ...
下面的内容比 if-then-else 更容易。
trueShift = (y + shift) % characters.length();
或者如果 y+shift 可能为负(那么 trueShift 也会变为负),更好:
int n = characters.length();
trueShift = (y + shift + n) % n;
在您的其他部分 trueShift
不是对称的(单射,不是双射):decrypt(encrypt(s)) != s。如果 0,1,2,3 映射到 0,1,1,0 就会出现问题。
static String cipher(String msg, int shift){
String characters = "01234556789ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
int n = characters.length();
StringBuilder decryptedMessage = new StringBuilder();
for (int x = 0; x < msg.length(); x++) {
char ch = msg.charAt(x);
int y = characters.indexOf(ch);
if (y != -1) {
int trueShift = (y + shift + n) % n;
ch = characters.charAt(trueShift);
}
decryptedMessage.append(ch);
}
return decryptedMessage.toString();
}
关于java - 移位密码错误地移位了一些字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49081138/
我想将这个无符号数:1479636484000 向右移动 7 位。这在 JavaScript 中可能吗? 两者 1479636484000 >> 7 和 1479636484000 >>> 7 返回错
鉴于以下代码: import matplotlib.pyplot as plt import numpy as np x = [1.0, 1.1, 2.0, 5.7] y = np.arange(le
我有一个低级键盘钩子(Hook),目前允许我从任何应用程序(包括游戏)中控制媒体播放器。 它通过查看捕获的特定击键来工作。 我想扩展它以查找键的组合。我可以对一些逻辑进行硬编码,但我觉得必须有一种更合
我需要一些帮助来理解这段C代码。我不知道这里的“L”和“\”是什么?请也说明一点:) #define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>2
我正在查看一段代码: int result = 0 ; char byte = foo[j] for (i = 7 ; i>0 ; i--) { byte = (byte & ~0x1)>>1
我们有一个项目要求我们编写一个程序,允许用户输入一系列数字“将数字读入数组以进行进一步处理,用户通过输入负数表示他们已完成(负数不用于计算),在读取所有数字后执行以下操作,总结输入的#,计算输入的#,
锁定。有disputes about this question’s content正在解决中。它目前不接受新的答案或互动。 def menu(): choice = input("Pres
为什么如果 int x = -1 // binary: 11111111111111111111111111111111 x = x >>> 31; 我们有 000000000000000000000
我的问题其实应该很简单:我有一个玩家对象数组。(玩家[])我想要一个函数来旋转这个数组直到一个索引: public void rotateArray(Object[] array, int index
我有一个编码为 boost 动态位集的数字列表。我根据此列表中的任何数字可以采用的最大值动态选择此位集的大小。所以假设我有从 0 到 7 的数字,我只需要三位,我的字符串 0,2,7 将被编码为000
我能想到一些令人讨厌的低效方法来完成这项任务,但我想知道最好的方法是什么。 例如,我想复制一个字节中从第 3 位开始的 10 个字节,并像往常一样复制到一个指针。 有没有比一次复制一个移位字节更好的方
我正在尝试为该问题添加更多规则,并且该规则一直给我带来这种转变/减少冲突的能力,我不知道为什么会这样做,并且在过去的24小时内我一直在尝试解决问题 FuncDecl : RetTyp
This question already has answers here: Why does it make a difference if left and right shift are us
我在 Perl 中遇到这个问题已经有几天了,在搜索了无数的手册页、perldocs 和谷歌搜索了太多的搜索词之后,希望这里有人能帮助我。 我得到两个表示十六进制值的字符串,即“FFFF”,而不是 Pe
我有一个主 div,两个 div 水平并排放置在这个父 div 中。 .parent{ height: 360px; margin-top: 0px; bo
我想 float 我的元素列表并从第二个元素创建一个移动效果。 如何避免第二个 .item 之后的“清除”行为? .shift { float: right; width: 50%;
我正在使用 SSE3 优化我的代码。代码中有一点迫使我将 vector 中的所有元素移动一个元素 v[0] = 0 //v is some char* and N = v.size() for(i
.file "calcnew.c" .text .globl calcnew .type calcnew, @function calcnew:
我有一个点对象: class Point { final int x,y; ... } 因为这些点将在我的代码中到处使用/创建,所以我想开始使用 guavas 缓存。不幸的是
x = "Foo 890 bar *()" 如何将包括 "*()" 在内的小写字母“未移位”返回到 890?期望的结果: foo 890 bar 890 不需要的: x.lower() => "foo
我是一名优秀的程序员,十分优秀!