【发布时间】:2012-11-04 10:44:08
【问题描述】:
我正在尝试使用 java 方法解密密码加密,但是我的代码似乎没有正确返回。我试图扭转加密过程,但我看不出我做错了什么。抱歉,我希望这不是一个愚蠢的问题。
public void decrypt()
{
String cipherText = this.message;
String key = this.KEY;
String alphabet = "abcdefghijklmnopqrstuvwxyz";
int alphabetSize = alphabet.length();
int textSize = cipherText.length();
int keySize = key.length();
StringBuilder decryptedText = new StringBuilder(textSize);
for (int i = 0; i < textSize; i++)
{
char encyrptChar = cipherText.charAt(i); // get the current character to be shifted
char keyChar = key.charAt(i % keySize); // use key again if the end is reached
int plainPos = alphabet.indexOf(encyrptChar); // plain character's position in alphabet string
// decrypt the input text
int keyPos = alphabet.indexOf(keyChar); // key character's position in alphabet
int shiftedPos = plainPos-keyPos;
shiftedPos += alphabetSize;
decryptedText.append(alphabet.charAt(shiftedPos));
}
this.message = decryptedText.toString();
}
【问题讨论】:
-
代码的逻辑是什么,你想做什么?
-
我昨天已经看到了同样的问题……看来作业的截止日期快到了;)
-
如果来自 Coursera,此类问题应关闭并删除:coursera.org/about/honorcode
-
@das_weezul 我见过一个在网络上运行的,并且忽略了不可打印的字符。不用说,它们都是 vigenere,但仍然互不相容。
-
我没有参加 coursera 课程。请不要这样指责我。
标签: java encryption vigenere