public class VigenereCipher {
public static String encrypt(String plaintext, String key) {
// 转换为大写字母并去除空格等非字母字符
plaintext = plaintext.toUpperCase().replaceAll("[^A-Z]", "");
key = key.toUpperCase();
StringBuilder ciphertext = new StringBuilder();
int keyIndex = 0;
for (char plainChar : plaintext.toCharArray()) {
if (!Character.isLetter(plainChar)) {
ciphertext.append(plainChar);
continue;
}
// 计算字母的偏移量
int offset = key.charAt(keyIndex) - 'A';
// 加密字母
char encryptedChar = (char) ((plainChar + offset - 'A') % 26 + 'A');
ciphertext.append(encryptedChar);
// 更新密钥索引
keyIndex = (keyIndex + 1) % key.length();
}
return ciphertext.toString();
}
public static void main(String[] args) {
String plaintext = "HELLO";
String key = "KEY";
String ciphertext = encrypt(plaintext, key);
System.out.println("Plaintext: " + plaintext);
System.out.println("Key: " + key);
System.out.println("Ciphertext: " + ciphertext);
}
}public class VigenereCipher {
public static String encrypt(String plaintext, String key) {
// 转换为大写字母并去除空格等非字母字符
plaintext = plaintext.toUpperCase().replaceAll("[^A-Z]", "");
key = key.toUpperCase();
StringBuilder ciphertext = new StringBuilder();
int keyIndex = 0;
for (char plainChar : plaintext.toCharArray()) {
if (!Character.isLetter(plainChar)) {
ciphertext.append(plainChar);
continue;
}
// 计算字母的偏移量
int offset = key.charAt(keyIndex) - 'A';
// 加密字母
char encryptedChar = (char) ((plainChar + offset - 'A') % 26 + 'A');
ciphertext.append(encryptedChar);
// 更新密钥索引
keyIndex = (keyIndex + 1) % key.length();
}
return ciphertext.toString();
}
public static void main(String[] args) {
String plaintext = "HELLO";
String key = "KEY";
String ciphertext = encrypt(plaintext, key);
System.out.println("Plaintext: " + plaintext);
System.out.println("Key: " + key);
System.out.println("Ciphertext: " + ciphertext);
}
}public class VigenereCipher {
public static String encrypt(String plaintext, String key) {
// 转换为大写字母并去除空格等非字母字符
plaintext = plaintext.toUpperCase().replaceAll("[^A-Z]", "");
key = key.toUpperCase();
StringBuilder ciphertext = new StringBuilder();
int keyIndex = 0;
for (char plainChar : plaintext.toCharArray()) {
if (!Character.isLetter(plainChar)) {
ciphertext.append(plainChar);
continue;
}
// 计算字母的偏移量
int offset = key.charAt(keyIndex) - 'A';
// 加密字母
char encryptedChar = (char) ((plainChar + offset - 'A') % 26 + 'A');
ciphertext.append(encryptedChar);
// 更新密钥索引
keyIndex = (keyIndex + 1) % key.length();
}
return ciphertext.toString();
}
public static void main(String[] args) {
String plaintext = "HELLO";
String key = "KEY";
String ciphertext = encrypt(plaintext, key);
System.out.println("Plaintext: " + plaintext);
System.out.println("Key: " + key);
System.out.println("Ciphertext: " + ciphertext);
}
}public class VigenereCipher {
public static String encrypt(String plaintext, String key) {
// 转换为大写字母并去除空格等非字母字符
plaintext = plaintext.toUpperCase().replaceAll("[^A-Z]", "");
key = key.toUpperCase();
StringBuilder ciphertext = new StringBuilder();
int keyIndex = 0;
for (char plainChar : plaintext.toCharArray()) {
if (!Character.isLetter(plainChar)) {
ciphertext.append(plainChar);
continue;
}
// 计算字母的偏移量
int offset = key.charAt(keyIndex) - 'A';
// 加密字母
char encryptedChar = (char) ((plainChar + offset - 'A') % 26 + 'A');
ciphertext.append(encryptedChar);
// 更新密钥索引
keyIndex = (keyIndex + 1) % key.length();
}
return ciphertext.toString();
}
public static void main(String[] args) {
String plaintext = "HELLO";
String key = "KEY";
String ciphertext = encrypt(plaintext, key);
System.out.println("Plaintext: " + plaintext);
System.out.println("Key: " + key);
System.out.println("Ciphertext: " + ciphertext);
}
}public class VigenereCipher {
public static String encrypt(String plaintext, String key) {
// 转换为大写字母并去除空格等非字母字符
plaintext = plaintext.toUpperCase().replaceAll("[^A-Z]", "");
key = key.toUpperCase();
StringBuilder ciphertext = new StringBuilder();
int keyIndex = 0;
for (char plainChar : plaintext.toCharArray()) {
if (!Character.isLetter(plainChar)) {
ciphertext.append(plainChar);
continue;
}
// 计算字母的偏移量
int offset = key.charAt(keyIndex) - 'A';
// 加密字母
char encryptedChar = (char) ((plainChar + offset - 'A') % 26 + 'A');
ciphertext.append(encryptedChar);
// 更新密钥索引
keyIndex = (keyIndex + 1) % key.length();
}
return ciphertext.toString();
}
public static void main(String[] args) {
String plaintext = "HELLO";
String key = "KEY";
String ciphertext = encrypt(plaintext, key);
System.out.println("Plaintext: " + plaintext);
System.out.println("Key: " + key);
System.out.println("Ciphertext: " + ciphertext);
}
}
// 更新密钥索引
keyIndex = (keyIndex + 1) % key.length();
}
return ciphertext.toString();
}
public static void main(String[] args) {
String plaintext = "HELLO";
String key = "KEY";
String ciphertext = encrypt(plaintext, key);
System.out.println("Plaintext: " + plaintext);
System.out.println("Key: " + key);
System.out.println("Ciphertext: " + ciphertext);
}
}
【以下回答由 GPT 生成】
问题: 请解释一下Vigenere密码是如何工作的?
Vigenere密码是一种多表密码,它通过使用一个字母序列作为密钥,并对明文中的每个字符进行多次重复加密来实现加密。具体来说,Vigenere密码通过使用多个凯撒密码的密钥形式来加密文本。每个字母对应一个凯撒密码,密钥决定了使用哪个凯撒密码。这种密码在多个平文字符之间引入复杂性,使得破解更加困难。
Vigenere密码的加密过程大致分为以下步骤: 1. 将明文转换为大写,并去除空格和其他非字母字符。 2. 将密钥转换为大写。 3. 使用循环来对明文中的每个字符进行加密。 4. 如果字符不是字母,则直接添加到密文中。 5. 对于字母字符,首先计算密钥字符的偏移量。 6. 使用该偏移量来加密明文字符,加密的方式是通过将明文字符的ASCII值加上偏移量,并对26取模得到加密后的ASCII值。然后再将加密后的ASCII值转换回字符。 7. 更新密钥索引以便下一轮加密。 8. 最终,返回密文作为结果。
下面是给出的代码实现了Vigenere密码的加密过程:
public class VigenereCipher {
public static String encrypt(String plaintext, String key) {
// 转换为大写字母并去除空格等非字母字符
plaintext = plaintext.toUpperCase().replaceAll("[^A-Z]", "");
key = key.toUpperCase();
StringBuilder ciphertext = new StringBuilder();
int keyIndex = 0;
for (char plainChar : plaintext.toCharArray()) {
if (!Character.isLetter(plainChar)) {
ciphertext.append(plainChar);
continue;
}
// 计算字母的偏移量
int offset = key.charAt(keyIndex) - 'A';
// 加密字母
char encryptedChar = (char) ((plainChar + offset - 'A') % 26 + 'A');
ciphertext.append(encryptedChar);
// 更新密钥索引
keyIndex = (keyIndex + 1) % key.length();
}
return ciphertext.toString();
}
public static void main(String[] args) {
String plaintext = "HELLO";
String key = "KEY";
String ciphertext = encrypt(plaintext, key);
System.out.println("Plaintext: " + plaintext);
System.out.println("Key: " + key);
System.out.println("Ciphertext: " + ciphertext);
}
}
以上就是Vigenere密码的工作原理和代码实现的解答。
【相关推荐】