维吉尼亚加密
Vigenère encryption
我一直在用凯撒密码和维吉尼亚密码进行一个项目。我已经想出了凯撒密码,但我正在为 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;
}
}
问题出在 encrypt1
函数的这一行:
int keyIndex = alphabet.get(passwordPos++ % password.length());
此处您试图在 密码 中查找密钥,但实际上是从 字母表 中获取的。
你想要的是从密码中找到相关字符(到达末尾时循环),然后找出该字符在你的字母表中的索引,这样你就可以获取平面文本并将其移动( index) 字母表中的许多字符。
为此,您可以使用此代码:
char pwChar = password.charAt(passwordPos++ % password.length());
int keyIndex = alphabet.indexOf(pwChar);
当我更改此行和 运行 你的代码时 SLIME
作为密钥,GREEN
作为明文,我得到结果 YcMQR
,这就是你说你期待。
我一直在用凯撒密码和维吉尼亚密码进行一个项目。我已经想出了凯撒密码,但我正在为 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;
}
}
问题出在 encrypt1
函数的这一行:
int keyIndex = alphabet.get(passwordPos++ % password.length());
此处您试图在 密码 中查找密钥,但实际上是从 字母表 中获取的。 你想要的是从密码中找到相关字符(到达末尾时循环),然后找出该字符在你的字母表中的索引,这样你就可以获取平面文本并将其移动( index) 字母表中的许多字符。
为此,您可以使用此代码:
char pwChar = password.charAt(passwordPos++ % password.length());
int keyIndex = alphabet.indexOf(pwChar);
当我更改此行和 运行 你的代码时 SLIME
作为密钥,GREEN
作为明文,我得到结果 YcMQR
,这就是你说你期待。