自动密钥密码. [英] Autokey cipher code.
问题描述
我有无法正常工作的自动密钥密码-它以密文形式提供密文.
有人可以帮忙吗?
I have Autokey cipher code which is not working correctly - it is giving cipher text as its plain text.
Can someone help?
import java.util.Scanner;
public class k {
public static void main(String[] args){
String cip=k.encrypt();
String pln=k.decrypt(cip);
}
private static String encrypt() {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Enter the plaintext");
String plainText = input.nextLine();
int len = plainText.length(), asciiValue, newValue, letterValue, x = 0, counter = 0, nexti = 0;
String ciphertext = new String();
char current;
plainText = plainText.toUpperCase(); //it makes it easier to have it all in one case
String keyword = "secret";
keyword = keyword.toUpperCase();
for (int i = 0; i < keyword.length(); i++)
{
current = plainText.charAt(i);
if (Character.isSpace(current))
{
ciphertext += ' ';
i++;
current = plainText.charAt(i);
}
asciiValue = ((int)current);
//if it's an uppercase letter, encode it
if (asciiValue >= 65 && asciiValue <= 90)
{
letterValue = asciiValue - 65;
newValue = letterValue + (((int)(keyword.charAt(counter))) - 65);
newValue %= 26;
ciphertext += (char)(newValue + 65);//add it to the ciphertext
counter++;
}
nexti = i;
}
x = 0;
for (int i = nexti + 1; i < len; i++)
{
char temp = ' ';
current = plainText.charAt(i);
if (Character.isSpace(current))
{
ciphertext += ' ';
i++;
current = plainText.charAt(i);
}
asciiValue = ((int)current);
//if it's an uppercase letter, encode it
if (asciiValue >= 65 && asciiValue <= 90)
{
letterValue = asciiValue - 65;
temp = ciphertext.charAt(x);
while (temp == ' ')
{
x++;
temp = ciphertext.charAt(x);
}
newValue = letterValue + (((int)(temp)) - 65);//add the shift
newValue %= 26;
ciphertext += (char)(newValue + 65);//add it to the ciphertext
x++;
}
temp = ' ';
}
System.out.println("Cipher text = "+ciphertext);
return ciphertext;
}
private static String decrypt(String ciphertext) {
int x = 0, len = ciphertext.length(), asciiValue, newValue, letterValue, y = 0;
String plaintext = new String();
char current, temp = ' ';
ciphertext = ciphertext.toLowerCase();//it makes it easier to have it all in one case
String keyy = "secret";
int keywordLength = keyy.length();
for (int i = 0; i < keywordLength; i++)
{
if (x < ciphertext.length())
{
plaintext += ciphertext.charAt(x);
x++;
if (Character.isSpace(ciphertext.charAt(x - 1)))
{
plaintext += ciphertext.charAt(x);
x++;
}
y = x;
}
}
x = 0;
for (int i = 0; i < (len - y); i++)
{
current = ciphertext.charAt(i + y);
if (Character.isSpace(current))
plaintext += ' ';
else
{
asciiValue = ((int)current);
if (asciiValue >= 97 && asciiValue <= 122)
{
letterValue = asciiValue - 97;
temp = ciphertext.charAt(x);
while (temp == ' ')
{
x++;
temp = ciphertext.charAt(x);
}
newValue = letterValue - (((int)(temp)) - 97); //take off the shift
newValue %= 26;
//if we've gone below 0, we add 26, which has the effect of wrapping around to the end of the alphabet
if (newValue < 0)
newValue += 26;
plaintext += (char)(newValue + 97);//add it to the plaintext
x++;
}
}
temp = ' ';
}
System.out.println("Plaintext text = "+plaintext);
return plaintext;
}
}
推荐答案
您的代码不会不给出密文.它在某些字符之后挣扎并且胡说八道.
Your code does NOT give out the cipher text. It struggles after some characters and gives nonsense.
// in function decrypt() around line 100
for (int i = 0; i < keywordLength; i++)
{
if (x < ciphertext.length())
{
plaintext += ciphertext.charAt(x);
x++;
if (Character.isSpace(ciphertext.charAt(x - 1)))
{
plaintext += ciphertext.charAt(x);
x++;
}
y = x;
}
}
我猜只要关键字比密文长就行了.
还请考虑一下OOP的概念.看来您还没有真正理解OOP的含义.我在private static String encrypt()
上苦苦挣扎-这意味着它不获取任何参数,但会返回一个值.这样的功能很奇怪.
输入文本和结果输出应该在您的主程序中完成,只对方法进行加密和解密.保持它们的功能!不要给他们做几项任务. 1个任务对应1个功能.
另外,请不要使这些方法静态化-初始化一个对象(可能被命名为DeEncrpyter
甚至是"K"<-KAPITAL K即!!!)并使用它.
I guess that works as long as the keyword is longer than the cipher text.
Also please think about the concept of OOP. It seems that you''ve not really understood what OOP means. I struggled upon the private static String encrypt()
- which means it does not get any arguments but delivers a value back. Pretty strange for such a function.
Entering text and result output should be done in your main, leaving just the encryption and decryption to your methods. keep them functional! don''t give them several tasks to do. 1 task for 1 function.
Also please don''t make the methods static - init an object (might be named DeEncrpyter
or even "K" <- KAPITAL K that is!! ) and work with it.
这篇关于自动密钥密码.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!