在java中的仿射密码也加密整数

时间:2014-04-03 15:23:25

标签: validation

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();
}

我从这个网站获得了这个代码 但上面的代码并没有减少整数.. 请帮忙

上面是我从这个网站获得的代码。 但这不会解密整数。 请帮忙。

1 个答案:

答案 0 :(得分:0)

我添加了另外两个功能,包括incrypt和解密工作的整数。当你想要加密,解密整数时,你可以使用它们,当你想要加/小写字母时你可以使用以前的函数

//代码

import java.math.BigInteger;

public class main{

    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);
        System.out.println("\n");

        //for Integer
        String input1 = "826429837598327598327549832";
        String cipher1 = encryptInt(input1);
        String deciphered1 = decryptInt(cipher1);
        System.out.println("Source:    " + input1);
        System.out.println("Encrypted: " + cipher1);
        System.out.println("Decrypted: " + deciphered1);
    }

    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();
    }

    //ENCRIPTION DECRIPTION FOR INTEGERS
    static String encryptInt(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 - '0') + secondKey) % module + '0');
            //}
            builder.append(character);
        }
        return builder.toString();
    }

    static String decryptInt(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 - '0' - secondKey + module);
                character = (char) (decoded % module + '0');
            //}
            builder.append(character);
        }
        return builder.toString();
    }
}