Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am attempting to create my first public key encryption program an have recently tested the encryption section of code unsuccessfully. This is probably a very simple error, but bear with me for a moment. I have tried encrypting the word "hello" and the program return the translation of [D@34005e1, multiple times. Below is the code for my classes, any help would be appreciated.

Main:

package MAE;

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

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.getDValue() + ", " + eKey.getNValue() + ")");
        System.out.println("Private Key-(" + eKey.getEValue() + ", " + 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 Digit:");
        double eKey1 = Input.nextDouble();
        System.out.println("Enter Encryption Key Second Digit");
        double eKey2 = Input.nextDouble();
        Input.nextLine();
        System.out.println("Enter message to Encrypt, omitting any puncuation besides periods.");
        String message = Input.nextLine();
        String messageChars[] = new String[message.length()];
        int messageValues[] = new int[message.length()];
        double previousValue = 0;
        double messageEncrypted[] = new double[message.length()];
        double tempval = 0;

        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] = y;
                }
            }
        }

        for(int x = 0; x < messageValues.length; x++)
        {
            previousValue = (messageValues[x] - 1 * messageValues[x] + 1) % messageValues[x];
            tempval = Math.pow(messageValues[x], eKey1);
            messageEncrypted[x] = (tempval % eKey2) + previousValue;
        }

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

    public static void decrypt()
    {

    }
}

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);
        double x = r + 1;

        float v = 2;

        while(k == 0)
        {
            while(v * v <= x)
            {
                if(x % v == 0)
                {
                    k = x;
                }

                v++;
            }

            x += r;
        }

        k += r * random.nextInt(3);

        for(int c = 2; c <= k; c++)
        {
            if(k % c == 0)
            {
                eKey = c;
                dKey = k/eKey;
            }
        }
    }

    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;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
127 views
Welcome To Ask or Share your Answers For Others

1 Answer

This code is fundamentally wrong. First of all you cant use doubles. Doubles are not capable of holding more than 64 bits of information, and lose information when you perform operations such as power, when the number overflows.Also you use signed integers, so at best you can have RSA31 which is absolutely useless and can be broken in a microsecond. But even to use RSA31 you should perform modular exponent, which is not exponent followed by a modulo, because for real RSA when you use large numbers, 1024 bits for say, the result of an exponent would be 2^100 bit, not something your computer can store. In your usage with integers, the result of the exponent also easily overflows. There are other problems in your code, but first you some multi-precision integer library, with modular exponentiation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...