Java 随机替换密码

Java 随机替换密码,java,encryption,Java,Encryption,我正在用Java制作一个随机替换密码。基本上,程序会要求你输入一个句子,你输入这个句子,然后用一个随机生成的字母表对它进行加密。用户可以选择加密或解密。加密的密码文本随后显示在屏幕上。如果用户选择这样做,程序将解密密码并显示原始纯文本消息 以下是我目前掌握的情况: Random gen = new Random(); PrintWriter write = new PrintWriter(new File("CryptCode.txt")); char[] chars =

我正在用Java制作一个随机替换密码。基本上,程序会要求你输入一个句子,你输入这个句子,然后用一个随机生成的字母表对它进行加密。用户可以选择加密或解密。加密的密码文本随后显示在屏幕上。如果用户选择这样做,程序将解密密码并显示原始纯文本消息

以下是我目前掌握的情况:

    Random gen = new Random();
    PrintWriter write = new PrintWriter(new File("CryptCode.txt"));
    char[] chars = new char[] {'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'};
                                //'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'};
    char[] cryptList = new char[26];

    int tracker = 0;
    while(tracker < 26)
    {
        int num = gen.nextInt(26);
        if(cryptList[num] == '\u0000')
        {
        cryptList[num] = chars[tracker];
        tracker++;
        }
    }

    for(int i = 0; i < 26; i++)
    {
        write.println(chars[i] + " " + cryptList[i]);
    }
    write.close();
Random gen=new Random();
PrintWriter write=新的PrintWriter(新文件(“CryptCode.txt”);
char[]chars=新字符[]{'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'};
//‘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’;
char[]cryptList=新字符[26];
int-tracker=0;
而(跟踪器<26)
{
int num=gen.nextInt(26);
如果(密码列表[num]='\u0000')
{
密码列表[num]=chars[tracker];
跟踪器++;
}
}
对于(int i=0;i<26;i++)
{
write.println(chars[i]+“”+cryptList[i]);
}
write.close();

这只会生成随机字母表。不过,我不知道如何实现实际的加密方法。我可以自己处理文件IO和对用户的提示。我只是不明白如何创建一个替换算法。非常感谢您的帮助。一旦我有了加密方法,我可能会想出解密方法,但现在我不知道如何继续。谢谢

替换算法意味着用某个字符替换一个字符

你可以用多种方式

1.substitute the character with nth character from now.
     example: a is replaced by e
              b is replaced by f
2.substitute the character with (n+i)th character from now. to replace abf
     example: a is replaced by f(a=1,n=5)
              b is replaced by g(b=2,n=5)
              f is replaced by m(f=3,n=5)

通常每个人都使用rot13作为替换密码的一种,替换为字符串的第13个字符

我以前做过,我就是这样做的:

private static char[] alphabet = {'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'};

public static char[] shiftAlphabet(int shift)
{
    char[] newAlpha = new char[26];
    for (int i = 0; i < 26; i++)
    {
        if(((i + shift) < 26) && ((i + shift) >= 0))
        {
            newAlpha[i]  = alphabet[i + shift];
        }
        else if ((i + shift) >= 26)
        {
            newAlpha[i] = alphabet[i + shift - 26];
        }
    }
    return newAlpha;
}
public static String encrypt(String s, int shift)
{
    String e = "";
    for(int i = 0; i < s.length(); i++)
    {
        char letter = s.charAt(i);
        if (letter != ' ')
        {
            int f = find(alphabet, letter);
            if(((f + shift) < 26) && ((f + shift) >= 0))
            {
                letter  = alphabet[f + shift];
            }
            else if ((f + shift) >= 26)
            {
                letter = alphabet[f + shift - 26];
            }
            e = e + String.valueOf(letter);
        }
        else 
        {
            e = e + " ";
        }
    }
    return e;
}
public static int find(char[] c, char c2)
{
    int w = 0;
    for(int i = 0; i < c.length; i++)
    {
        if(c[i] == (c2))
            w = i;
    }
    return w;
}
private static char[]字母表={'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’};
公共静态字符[]移位Talphabet(int移位)
{
char[]newAlpha=新字符[26];
对于(int i=0;i<26;i++)
{
如果(((i+shift)<26)和((i+shift)>=0))
{
newAlpha[i]=字母表[i+shift];
}
如果((i+shift)>=26,则为其他情况
{
newAlpha[i]=字母[i+shift-26];
}
}
返回newAlpha;
}
公共静态字符串加密(字符串s,int-shift)
{
字符串e=“”;
对于(int i=0;i=0))
{
字母=字母表[f+shift];
}
如果((f+shift)>=26,则为其他情况
{
字母=字母[f+shift-26];
}
e=e+String.valueOf(字母);
}
其他的
{
e=e+“”;
}
}
返回e;
}
公共静态int find(char[]c,char c2)
{
int w=0;
for(int i=0;i

第一个方法将字母表移位一个整数,第二个方法使用最后一个方法加密消息,该方法查找字母表中字母的位置(返回0到25的值)。因此,使用这些方法,在主方法中,您可以生成一个从1到26的随机数,并将其用作“shift”变量,然后提示用户输入他/她想要加密的消息

我有一种不好的感觉,您生成的字典给出了给定字符处于给定位置的不均匀概率。我可能只会使用java.util.Collections.shuffle(列表)。这也会使你的代码更干净。我已经做了一个凯撒替换密码,这就是代码的作用。我没有整数移位,而是生成了一个完全随机的字母表。例如,这个随机字母表的前五个字母可能是“hwreu”。在这一代之后,用户输入中的每个“a”都被“h”替换,每个“b”都被“w”替换,依此类推。我没有在实际的字母表中实现转换,而是创建了一个新的字母表。在那一点上,我如何实现加密有什么想法吗?ThanksHow你会创建那个随机字母表吗?你能把代码贴在这里吗。从那以后我也许能帮你。