自定义加密解密功能无法正确解密

时间:2013-12-11 00:28:30

标签: java cryptography rsa public-key-encryption

我非常擅长加密和使用BigIngeger值,并且一直致力于基于RSA的加密系统。 keygen方法和类工作正常以及加密方法,但解密方法拒绝解码输入的值。我意识到我可能错过了一个明显的错误,但任何帮助都会受到赞赏。

主:

package MAE;

import java.util.Scanner;
import java.lang.Math;
import java.math.BigInteger;

public class Main 
{
    public static void main(String[] args)
    {
        Scanner Input = new Scanner(System.in);

        int choice;

        boolean exit = true;

        while(exit == true)
        {
            System.out.println("Please select a option from the menu below.");
            System.out.println("1. Generate Key");
            System.out.println("2. Encrypt Message");
            System.out.println("3. Decrypt Message");
            System.out.println("4. Exit");

            choice = Input.nextInt();

            switch(choice)
            {
            case 1:
                keyGen();
                break;
            case 2:
                encrypt();
                break;
            case 3:
                decrypt();
                break;
            case 4:
                exit = false;
                break;
            }
        }
    }

    public static void keyGen()
    {
        Scanner Input = new Scanner(System.in);
        encryptionAlgorithm eKey = new encryptionAlgorithm();

        System.out.println("Public Key-(" + eKey.getEValue() + ", " + eKey.getNValue() + ")");
        System.out.println("Private Key-(" + eKey.getDValue() + ", " + eKey.getNValue() + ")");
    }

    public static void encrypt()
    {
        String alphabet[] = new String[] {"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", ".", " "};

        Scanner Input =new Scanner(System.in);

        System.out.println("Enter Encryption Key First Number:");
        BigInteger eKey1 = Input.nextBigInteger();
        System.out.println("Enter Encryption Key Second Number");
        BigInteger eKey2 = Input.nextBigInteger();
        Input.nextLine();
        System.out.println("Enter message to Encrypt, omitting any puncuation besides periods.");
        String message = Input.nextLine().toLowerCase();
        String messageChars[] = new String[message.length()];
        BigInteger messageValues[] = new BigInteger[message.length()];
        double previousValue = 0;
        BigInteger messageEncrypted[] = new BigInteger[message.length()];

        for(int x = 0; x < message.length(); x++)
        {
            messageChars[x] = Character.toString(message.charAt(x));
        }

        for(int x = 0; x < message.length(); x++)
        {
            for(int y = 0; y < alphabet.length; y++)
            {
                if(messageChars[x].equals(alphabet[y]))
                {
                    messageValues[x] = BigInteger.valueOf(y + 5);
                }
            }
        }

        for(int x = 0; x < messageValues.length; x++)
        {
            messageEncrypted[x] = messageValues[x].modPow(eKey1, eKey2).add(BigInteger.valueOf((long) previousValue));
            previousValue = ((messageValues[x].intValue() - 1) * (messageValues[x].intValue() + 1)) % messageValues[x].intValue();
        }

        for(int x = 0; x < messageEncrypted.length; x++)
        {
            System.out.print(messageEncrypted[x] + ", ");
        }
    }

    public static void decrypt()
    {
        Scanner Input = new Scanner(System.in);
        String alphabet[] = new String[] {"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", ".", " "};

        System.out.println("Enter decryption key first number.");
        int dKey1 = Input.nextInt();
        System.out.println("Enter decryption key second number.");
        int dKey2 = Input.nextInt();
        Input.nextLine();
        System.out.println("Enter message as numbers seperated by a '-'.");
        String messageWhole = Input.nextLine();

        String messageSegmented[] = messageWhole.split("-");
        int messageValues[] = new int[messageSegmented.length];
        int previousValue = 0;

        for(int x = 0; x < messageSegmented.length; x++)
        {
            messageValues[x] = Integer.parseInt(messageSegmented[x]);
        }

        BigInteger tempval = new BigInteger("0");
        int plaintextValues[] = new int[messageSegmented.length];
        String plaintext[] = new String[messageSegmented.length];


        for(int x = 0; x < messageValues.length; x++)
        {
            tempval = BigInteger.valueOf(messageValues[x]);
            tempval = tempval.modPow(BigInteger.valueOf(dKey1), BigInteger.valueOf(dKey2));
            plaintextValues[x] = tempval.intValue() - previousValue;
            previousValue = plaintextValues[x];
        }

        for(int x = 0; x < plaintextValues[x]; x++)
        {
            System.out.print(plaintextValues[x]);
        }
        for(int x = 0; x < plaintextValues[x]; x++)
        {
            System.out.print(alphabet[plaintextValues[x]]);
        }
    }
}

encryptionAlgorithm:

package MAE;

import java.util.Scanner;
import java.util.Random;

public class encryptionAlgorithm 
{
    public static double p;
    public static double q;
    public static double n;
    public static double r;
    public static double k;
    public static double eKey;
    public static double dKey;

    public encryptionAlgorithm()
    {
        Random random = new Random();
        boolean isPPrime = true;
        boolean isQPrime = true;
        double d = 2;

        Scanner Input = new Scanner(System.in);

        System.out.println("Please enter a prime number. Larger numbers are more secure.");
        p = Input.nextDouble();

        do
        {
            isPPrime = true;

            for(d = 2; d * d <= p && isPPrime == true; d++)
            {
                if(p % d == 0)
                {
                    isPPrime = false;
                }
            }

            if(isPPrime == false)
            {
                System.out.println("This number is not prime. Please enter another number.");
                p = Input.nextDouble();
            }
        } while(isPPrime == false);

        d = 2;

        System.out.println("Please enter another prime number. Larger numbers are more secure.");
        q = Input.nextDouble();

        do
        {   
            while(q == p)
            {
                System.out.println("This number is identical to the first entered number. Please enter another number.");
                q = Input.nextDouble();
            }

            isQPrime = true;

            for(d = 2; d * d <= q && isQPrime == true; d++)
            {
                if(q % d == 0)
                {
                    isQPrime = false;
                }
            }

            if(isQPrime == false)
            {
                System.out.println("This number is not prime. Please enter another number.");
                q = Input.nextDouble();
            }
        } while(isQPrime == false);

        n = p * q;
        r = (p - 1) * (q - 1); 

        int randPrime = random.nextInt(5);
        int x = 4;
        int primeCounter = 0;

        while(primeCounter < randPrime)
        {
            if(isPrime(x) == true)
            {
                primeCounter++;
            }

            x++;
        }

        x--;

        eKey = x;

        while((x * dKey) % r != 1)
        {
            dKey++;
            System.out.println(dKey);
        }
    }

    public boolean isPrime(int y)
    {
        for(int x = 2; x * x <= y; x++)
        {
            if(y % x == 0)
            {
                return false;
            }
        }

        return true;
    }

    public double getPValue()
    {
        return p;
    }

    public double getQValue()
    {
        return q;
    }

    public double getNValue()
    {
        return n;
    }

    public double getRValue()
    {
        return r;
    }

    public double getKValue()
    {
        return k;
    }

    public double getEValue()
    {
        return eKey;
    }

    public double getDValue()
    {
        return dKey;
    }
}

0 个答案:

没有答案