RSA加密未正确加密

时间:2013-12-10 04:46:27

标签: java encryption rsa

我正在尝试创建我的第一个公钥加密程序,最近测试了代码的加密部分失败了。这可能是一个非常简单的错误,但请耐心等待一下。我试过加密“hello”这个词,程序返回[D @ 34005e1,多次翻译。下面是我的课程的代码,任何帮助将不胜感激。

主:

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

1 个答案:

答案 0 :(得分:1)

这段代码根本就错了。首先,你不能使用双打。当你执行诸如电源之类的操作时,双数不能保存超过64位的信息,当数字溢出时也会丢失信息。同样你使用有符号整数,所以最多你可以使用RSA31这绝对没用并且可以被打破在一微秒内。但即使使用RSA31,你也应该执行模数指数,这不是指数后跟一个模数,因为对于真正的RSA,当你使用大数字时,比如1024比特,指数的结果将是2 ^ 100位,而不是你的电脑可以存放。在使用整数时,指数的结果也很容易溢出。 您的代码中还有其他问题,但首先是一些具有模幂运算的多精度整数库。