Java 如何创建替换关键字密码
我正在尝试开发一种替换密码,它使用关键字创建一个新的密码字母表。我是Java新手(我相信你会知道的!),我正在寻找它 我很难理解我需要做什么 我的理解如下: 例如,如果关键字是Java 如何创建替换关键字密码,java,encryption,keyword,substitution,Java,Encryption,Keyword,Substitution,我正在尝试开发一种替换密码,它使用关键字创建一个新的密码字母表。我是Java新手(我相信你会知道的!),我正在寻找它 我很难理解我需要做什么 我的理解如下: 例如,如果关键字是javben,我应该首先在纯文本字符串数组中查找“j”的索引,即9。然后我想将明文[9]转换为密文[0],并将其他元素移动1。因此,这一过程的第一步将导致: cipherText[] = {"j","a","b","c","d","e","f","g","h","i","k","l","m","n","o","p","q"
javben
,我应该首先在纯文本字符串数组中查找“j”的索引,即9。然后我想将明文[9]转换为密文[0],并将其他元素移动1。因此,这一过程的第一步将导致:
cipherText[] = {"j","a","b","c","d","e","f","g","h","i","k","l","m","n","o","p","q","r","s","t","u","v","w","r","x","y","z"}
然后我会找到“a”,它已经在它应该在的地方了,所以我需要解释这一点,而不是以某种方式改变它。下一个字符是“v”,因此该过程将继续
在改变密码中的所有内容后,我应该得到:
plainText []= {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","r","x","y","z"}
cipherText[]= {"j","a","v","b","e","n","c","d","f","g","h","i","k","l","m","o","p","q","r","s","t","u","w","r","x","y","z"}
正如您所看到的,我有理由相信我理解要经历的过程,但是我真的很难理解这需要的代码。救命啊
import java.util.Scanner;
import java.io.*;
/**
* This program uses a keyword for a simple substitution cipher.
*
* @author Bryan
* @version Programming Project
*/
public class Cipher
{
// The main method removes duplicate characters in a word input by the user.
public static void main(String[] args) throws IOException
{
// Creatae a new scanner object for keyboard input
Scanner keyboard = new Scanner(System.in);
// prompt the user to enter a word
System.out.println("Please enter your keyword: ");
// and get their input
String input = keyboard.nextLine();
// the keyword will be built up here
String keyword = "";
while(input.length() > 0)
{
// get the first letter
char letter = input.charAt(0);
// if the letter is not already in the output
if (keyword.indexOf(letter) == -1)
{
// add it to the end
keyword = keyword + letter;
}
// that letter is processed : discard it
input = input.substring(1);
}
//this is just to confirm the duplicate letters in the keyword are removed
System.out.println(keyword);
getFile();
}
/**
* This asks the user to specify a filename which is then
* read into the program for enciphering
*/
public static void getFile()throws IOException
{
// Creatae a new scanner object for keyboard input
Scanner keyboard = new Scanner(System.in);
// Get the file name
System.out.println("Enter the file name: ");
String filename = keyboard.nextLine();
//Open the file
File file = new File(filename);
Scanner inputFile = new Scanner(file);
// Read the lines from the file until no more are left
while (inputFile.hasNext())
{
//Read the next line
String allText = inputFile.nextLine();
// Display the text
System.out.println(allText);
}
//Close the file
inputFile.close();
}
public static void alphabet()
{
String[] plainText = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
String[] cipherText = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
}
}
这一个非常简单,只需设置一个函数,对于关键字中的每个字母,只需将其从字母表数组中取出,然后将两个数组与开头的字母数组和后面没有这些字母的字母表相加。例如:
String[] cipherKeyWord(String keyWord, String[] alphabet){
ArrayList<String> finalCipher = (ArrayList) Arrays.asList(keyWord.split("(?!^)"));
//^ This splits it into a string of every word using regular expressions
ArrayList<String> newAlphabet = (ArrayList) Arrays.asList(alphabet);
newAlphabet.removeAll(finalCipher);
finalCipher.addAll(newAlphabet);
return finalCipher.toArray(new String[finalCipher.size()]);
}
String[]密码关键字(String关键字,String[]字母表){
ArrayList finalCipher=(ArrayList)Arrays.asList(关键字.split((?!^)”);
//^这将使用正则表达式将其拆分为每个单词的字符串
ArrayList newAlphabet=(ArrayList)Arrays.asList(alphabet);
新字母表。删除所有字母(finalCipher);
finalCipher.addAll(新字母表);
返回finalCipher.toArray(新字符串[finalCipher.size()]);
}
我使用以下代码完成了替换密码
import java.util.*;
class SubCipher
{
public static void main(String args[])
{
String plainText = ",.<>;':\"[]{}-=_+)(*&^%$#\"@!),998683,1,x3x33,10~1,1,10~2,2,20";
String strcipherText = Encrypt(plainText);
String strdecryptedText = Decrypt(strcipherText);
System.out.println("Plain Text :"+plainText);
System.out.println("Encrypted Text :"+strcipherText);
System.out.println("Decrypted Text :"+strdecryptedText);
}
private static String Encrypt(String text)
{
byte[] textBytes = text.getBytes();
for (int i = 0; i < textBytes.length; i++)
{
int currentByteValue = (int)textBytes[i];
textBytes[i] = (byte)(currentByteValue > 255 ? currentByteValue - 255 + 2 : currentByteValue + 2);
}
String strbyte=new String(textBytes);
return strbyte;
}
private static String Decrypt(String text)
{
byte[] textBytes = text.getBytes();
for (int i = 0; i < textBytes.length; i++)
{
int currentByteValue = (int)textBytes[i];
textBytes[i] = (byte)(currentByteValue < 0 ? currentByteValue + 255 - 2 : currentByteValue - 2);
}
String strbyte=new String(textBytes);
return strbyte;
}
}
import java.util.*;
类次iphone
{
公共静态void main(字符串参数[])
{
字符串纯文本=“,.;”:\“[]{}-=\+”(&^%$\\”@!),998683,1,x3x33,10~1,1,10~2,2,20”;
字符串striphertext=加密(明文);
字符串strdecryptedText=解密(striphertext);
System.out.println(“纯文本:+纯文本”);
System.out.println(“加密文本:+striphertext”);
System.out.println(“解密文本:+strdecryptedText”);
}
私有静态字符串加密(字符串文本)
{
byte[]textBytes=text.getBytes();
对于(int i=0;i255?currentByteValue-255+2:currentByteValue+2);
}
String strbyte=新字符串(textBytes);
返回标准红细胞;
}
私有静态字符串解密(字符串文本)
{
byte[]textBytes=text.getBytes();
对于(int i=0;i
包类;
公共类替换密码类{
公共静态void main(字符串[]args){
字符纯文本[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','r','x','y','z'};
字符密文[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','r','x','y','z'};
字符串pt=“haci”;
对于(int a=0;a
您的流程太复杂了。为什么不简单地用你的关键词(javben),然后附加字母表中的每个字母(a,b,c…),除非它包含在关键词中?这是一个很好的建议,真的帮助我简化了问题。谢谢
package Classes;
public class SubstitutionCipherClass {
public static void main(String[] args) {
char plainText[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'r', 'x', 'y', 'z'};
char cipherText[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'r', 'x', 'y', 'z'};
String pt = "haci";
for (int a = 0; a < pt.length(); a++) {
for (int i = 0; i < plainText.length; i++) {
if(plainText[i] == (pt.charAt(a))){
System.out.println(cipherText[i]);
}
}
}
}
}