我一直在与Caesar密码和Vigenère密码进行一个项目。我已经找到了凯撒,但我正在与Vigenère密码挣扎。
我的加密代码分为两部分:encrypt()
和encrypt1()
。 encrypt()
方法提取每个字符并将其传递给encrypt1()
,其中所有加密基本上都发生。我在这里做了代码,但它没有给我任何好的结果。
例如,如果new VigenereCipher("SLIME").encrypt("GREEN")
其中"SLIME"
是密钥,而"GREEN"
是应加密为YcMQR
的字符串。但是我的代码给出了?&|{
。
考虑到字母表对象涵盖了所有内容= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 1234567890!@#$%^&*()_+-=[]{}\\|;:'\",./?<>"
可以请有人帮助我吗?
public class VigenereCipher extends SymmetricCipher {
protected String password;
protected int passwordPos;
public VigenereCipher(String password, Alphabet alphabet)
{
super(alphabet);
this.password = password;
}
public VigenereCipher(String password)
{
super(Alphabet.DEFAULT);
this.password = password ;
}
public String getPassword()
{
return this.password;
}
public String encrypt(String s)
{
passwordPos = 0;
String encrypted = "";
for(int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
encrypted += encrypt1(c);
}
return encrypted;
}
protected char encrypt1(char c)
{
//Alphabet temp = new Alphabet(s);
int index = 0;
char result = 0;
index = alphabet.indexOf(c); //Found index of a character
if(index != -1)
{
int keyIndex = alphabet.get(passwordPos++ % password.length());
result = alphabet.get((keyIndex + index) % alphabet.length());
}
else
throw new NotInAlphabetException(c, alphabet);
return result;
}
public String decrypt(String c)
{
return c;
}
protected char decrypt1(char c)
{
return c;
}
public String toString()
{
return "Vigenere Cipher (password =\'"+this.password+"\')";
}
}
public abstract class SymmetricCipher extends Cipher {
protected Alphabet alphabet;
public SymmetricCipher (Alphabet alphabet)
{
this.alphabet = alphabet;
}
public int wrapInt(int i)
{
int index = 0;
if (i >= alphabet.length())
index = Math.abs(i) % alphabet.length();
else if (i < 0)
{
int temp = Math.abs(i) % alphabet.length();
index = alphabet.length() - temp;
}
else
index = i;
return index;
}
public int rotate(int index, int shift)
{
int result = 0;
if (shift > 0)
{
result = (index + shift) % alphabet.length();
}
else if (shift < 0)
{
if(index < Math.abs(shift))
{
int temp = Math.abs(index + shift);
result = alphabet.length() - temp;
}
else
result = index + shift ;
}
return result;
}
public Alphabet getAlphabet()
{
return this.alphabet;
}
public String encrypt(String s)
{
String string = "";
char c = 0;
char encrypted = 0;
for (int i = 0; i < s.length(); i++)
{
c = s.charAt(i);
encrypted = encrypt1(c);
string += encrypted;
}
return string;
}
public String decrypt(String s) throws NotInAlphabetException
{
String string = "";
char c = 0;
char encrypted = 0;
for (int i = 0; i < s.length(); i++)
{
c = s.charAt(i);
encrypted = encrypt1(c);
string += encrypted;
}
return string;
}
protected abstract char encrypt1(char c);
protected abstract char decrypt1(char c);
}
public class Alphabet {
private String symbols;
public static final Alphabet DEFAULT = new Alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 1234567890!@#$%^&*()_+-=[]{}\\|;:'\",./?<>");
public Alphabet(String symbols)
{
this.symbols = symbols;
}
public int indexOf(char c)
{
Alphabet temp = new Alphabet(symbols);
for(int i = 0; i < symbols.length(); i++)
{
if(c == symbols.charAt(i))
return symbols.indexOf(c) ;
}
throw new NotInAlphabetException (c, temp);
}
public char get(int i)
{
Alphabet temp = new Alphabet(symbols);
char c = 0;
if (i > this.symbols.length())
throw new NotInAlphabetException (c, temp);
else
return symbols.charAt(i);
}
public int length()
{
return symbols.length();
}
public String getSymbols()
{
return symbols;
}
public String toString()
{
return "Alphabet("+this.symbols+")";
}
public boolean equals(Object other)
{
if(other instanceof Alphabet)
{
Alphabet temp = (Alphabet) other;
return this.symbols.equals(temp.symbols);
}
else
return false;
}
}
答案 0 :(得分:1)
问题出在encrypt1
函数的这一行:
int keyIndex = alphabet.get(passwordPos++ % password.length());
在这里,您尝试在密码中查找密钥,但实际上是从字母中获取密钥。 你想要的是从密码中找到相关的字符(当你到达结尾时循环),然后找出你的字母表中该字符的索引,这样你就可以获取平面文本并将其移动( index)字母表中的许多字符。
为此,您可以使用以下代码:
char pwChar = password.charAt(passwordPos++ % password.length());
int keyIndex = alphabet.indexOf(pwChar);
当我更改此行并以SLIME
作为键并以GREEN
作为明文运行代码时,我得到结果YcMQR
,这就是您所说的预期。< / p>