RSA加密不正确加密 [英] RSA Encryption Not encrypting correctly

查看:210
本文介绍了RSA加密不正确加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建我的第一个公钥加密程序,最近测试了代码的加密部分失败。这可能是一个非常简单的错误,但是忍受我一会儿。我已经尝试加密单词hello,程序返回[D @ 34005e1,多次的翻译]。以下是我的课程的代码,任何帮助将不胜感激。



主要:

 code>包MAE; 

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

public class Main
{
public static void main(String [] args)
{
扫描仪输入=新的扫描仪(System.in);

int选择;

boolean exit = true;

while(exit == true)
{
System.out.println(请从下面的菜单中选择一个选项);
System.out.println(1。生成键);
System.out.println(2。Encrypt Message);
System.out.println(3。解密消息);
System.out.println(4。Exit);

choice = Input.nextInt();

开关(选择)
{
案例1:
keyGen();
break;
case 2:
encrypt();
break;
案例3:
decrypt();
break;
案例4:
exit = false;
break;
}
}
}

public static void keyGen()
{
扫描仪输入=新的扫描仪(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

扫描仪输入=新扫描仪(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(输入消息加密,省略除句点之外的任何惩罚);
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; (int x = 0; x< message.length(); x ++)
{
messageChars [x] = Character.toString(message.charAt(x))

); (int x = 0; x< message.length(); x ++)
{
for(int y = 0; y<如果(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:

  ; 

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();
boolean isPPrime = true;
boolean isQPrime = true;
double d = 2;

扫描仪输入=新扫描仪(System.in);

System.out.println(请输入素数,较大数字更安全。);
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(这个数字不是素数,请输入另一个数。);
p = Input.nextDouble();
}
} while(isPPrime == false);

d = 2;

System.out.println(请输入另一个素数,较大的数字更安全。
q = Input.nextDouble();

do
{
while(q == p)
{
System.out.println(这个数字与第一个输入的数字相同请输入另一个数字。);
q = Input.nextDouble();
}

isQPrime = true; (d = 2; d * d< = q&&& isQPrime == true; d ++)

如果(q%d == 0)
{
isQPrime = false;
}
}

if(isQPrime == false)
{
System.out.println(这个数字不是素数,请输入另一个数。);
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); (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;
}
}


解决方案

代码根本错了。首先你不能使用双打。双倍数据不能持有64位以上的信息,并且当您执行诸如电源的操作时丢失信息,当数字溢出时。此外,您使用带符号的整数,所以最多可以让RSA31绝对没用,可以被破坏在一个微秒。但是即使使用RSA31,您应该执行模数指数,这不是指数,后跟模数,因为对于实际的RSA,当您使用大数字时,1024位表示,指数的结果将为2 ^ 100位,而不是您的电脑可以存储在整数的使用中,指数的结果也容易溢出。
您的代码还有其他问题,但首先是一些多精度整数库,具有模幂运算。


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

解决方案

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.

这篇关于RSA加密不正确加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆