2013-12-10 64 views
-1

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

主营:

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

使用不平凡的键,你会溢出双精度,提供不正确的结果。您应该改用java.math.BigInteger。 –

回答

1

这段代码是根本错误的。首先你不能使用双打。双数不能保存超过64位的信息,并且当数字溢出时执行诸如电源等操作时会丢失信息。您还使用带符号整数,所以充其量,您可以拥有绝对无用且可被破坏的RSA31在微秒。但即使使用RSA31,您也应该执行模指数,它不是指数后跟模数,因为对于真正的RSA,当您使用大数字时,1024位表示指数的结果为2^100位,而不是您的电脑可以存储。在使用整数时,指数的结果也很容易溢出。 您的代码中还存在其他问题,但首先您需要一些多精度整数库,并使用模幂运算。