我正在尝试使用 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();
}