如何在JAVA中编写仿射密码解密逻辑?

如何在JAVA中编写仿射密码解密逻辑?,java,encryption,Java,Encryption,你好,我正在用JAVA编写仿射密码。我已经成功地编写了加密代码,但现在我对解密的逻辑一无所知 下面是我的加密逻辑: void encryption() { char character; int plainTextLength=input.length(); int a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9,j=10,k=11,l=12,m=13,n=14, o=15,p=16,q=17,r=18,s=19,t=20,u=2

你好,我正在用JAVA编写仿射密码。我已经成功地编写了加密代码,但现在我对解密的逻辑一无所知

下面是我的加密逻辑:

void encryption()
{
    char character; 
    int plainTextLength=input.length();
    int a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9,j=10,k=11,l=12,m=13,n=14,
        o=15,p=16,q=17,r=18,s=19,t=20,u=21,v=22,w=23,x=24,y=25,z=26;

    System.out.print("Cipher text is:" );

    for (int in = 0; in < plainTextLength; in++)
    {
        character = input.charAt(in);

        if (Character.isLetter(character))
        {
            character = (char)((firstKey*(character - 'a') + secondKey) % 26 + 'a');
        }
        System.out.print(character);
    }
    System.out.println();       
}
void加密()
{
字符;
int plainTextLength=input.length();
INTA=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9,j=10,k=11,l=12,m=13,n=14,
o=15,p=16,q=17,r=18,s=19,t=20,u=21,v=22,w=23,x=24,y=25,z=26;
系统输出打印(“密文为:”);
for(int in=0;in
这是我的加密逻辑: character=(char)((firstKey*(character-'a')+secondKey)%26+'a')


解密逻辑是什么。我完全糊涂了?

一般的仿射密码解密公式非常简单:

其中a是您的
firstKey
b是您的
secondKey

因此,可以通过以下方式实现加密/解密:

private static int firstKey = 5;
private static int secondKey = 19;
private static int module = 26;

public static void main(String[] args) {
    String input = "abcdefghijklmnopqrstuvwxyz";
    String cipher = encrypt(input);
    String deciphered = decrypt(cipher);
    System.out.println("Source:    " + input);
    System.out.println("Encrypted: " + cipher);
    System.out.println("Decrypted: " + deciphered);
}

static String encrypt(String input) {
    StringBuilder builder = new StringBuilder();
    for (int in = 0; in < input.length(); in++) {
        char character = input.charAt(in);
        if (Character.isLetter(character)) {
            character = (char) ((firstKey * (character - 'a') + secondKey) % module + 'a');
        }
        builder.append(character);
    }
    return builder.toString();
}

static String decrypt(String input) {
    StringBuilder builder = new StringBuilder();
    // compute firstKey^-1 aka "modular inverse"
    BigInteger inverse = BigInteger.valueOf(firstKey).modInverse(BigInteger.valueOf(module));
    // perform actual decryption
    for (int in = 0; in < input.length(); in++) {
        char character = input.charAt(in);
        if (Character.isLetter(character)) {
            int decoded = inverse.intValue() * (character - 'a' - secondKey + module);
            character = (char) (decoded % module + 'a');
        }
        builder.append(character);
    }
    return builder.toString();
}

通用仿射密码解密公式非常简单:

其中a是您的
firstKey
b是您的
secondKey

因此,可以通过以下方式实现加密/解密:

private static int firstKey = 5;
private static int secondKey = 19;
private static int module = 26;

public static void main(String[] args) {
    String input = "abcdefghijklmnopqrstuvwxyz";
    String cipher = encrypt(input);
    String deciphered = decrypt(cipher);
    System.out.println("Source:    " + input);
    System.out.println("Encrypted: " + cipher);
    System.out.println("Decrypted: " + deciphered);
}

static String encrypt(String input) {
    StringBuilder builder = new StringBuilder();
    for (int in = 0; in < input.length(); in++) {
        char character = input.charAt(in);
        if (Character.isLetter(character)) {
            character = (char) ((firstKey * (character - 'a') + secondKey) % module + 'a');
        }
        builder.append(character);
    }
    return builder.toString();
}

static String decrypt(String input) {
    StringBuilder builder = new StringBuilder();
    // compute firstKey^-1 aka "modular inverse"
    BigInteger inverse = BigInteger.valueOf(firstKey).modInverse(BigInteger.valueOf(module));
    // perform actual decryption
    for (int in = 0; in < input.length(); in++) {
        char character = input.charAt(in);
        if (Character.isLetter(character)) {
            int decoded = inverse.intValue() * (character - 'a' - secondKey + module);
            character = (char) (decoded % module + 'a');
        }
        builder.append(character);
    }
    return builder.toString();
}

哦,谢谢,伙计,这是我唯一不知道如何计算第一个关键点的倒数的问题。我不熟悉java。你真的救了我。非常感谢……不客气。请将答案标记为接受,以供将来可能面临相同问题的任何人使用。哦,谢谢,伙计,这是我唯一不知道如何计算第一个关键点的倒数的问题。我不熟悉java。你真的救了我。非常感谢……不客气。对于将来可能面临相同问题的任何人,请将答案标记为已接受