RSA将字符串转换为BigInteger

时间:2015-03-03 17:28:27

标签: java rsa biginteger

我正在使用RSA算法程序并试图让它接受一个字符串作为消息而不是数字。我以为我的转换是正确的,但是当我跑步时出现错误,有人能看出原因吗?

    import java.math.BigInteger;
    import java.security.SecureRandom;


    public class rsa {

       //declaring random for use in pub and private gen 
       private final static SecureRandom random = new SecureRandom();

       //declarring bigInts
       private BigInteger privateKey;
       private BigInteger publicKey;
       private BigInteger modulus;

       // generate an N-bit (roughly) public and private key
       rsa(int N) {
          BigInteger p = BigInteger.probablePrime(N/2, random);
          BigInteger q = BigInteger.probablePrime(N/2, random);
          BigInteger phi = p.subtract(BigInteger.valueOf(1));
          phi = phi.multiply(q.subtract(BigInteger.valueOf(1)));

          modulus    = p.multiply(q);                                  
          publicKey  = new BigInteger("65537");     // common value in practice = 2^16 + 1
          privateKey = publicKey.modInverse(phi);
       }


       //encrypting function
       BigInteger encrypt(BigInteger message) {
          return message.modPow(publicKey, modulus);
       }

       //decrypting function
       BigInteger decrypt(BigInteger encrypted) {
          return encrypted.modPow(privateKey, modulus);
       }

       //printing values
       public String toString() {
          String s = "";
          s += "public = " + publicKey  + "\n";
          s += "private = " + privateKey + "\n";
          s += "modulus = " + modulus;
          return s;
       }

       public static void main(String[] args) {

          //declaring n (numbrer of bytes)
          int N = 128;

          //new object key RSA
          rsa key = new rsa(N);

          //printing the key
          System.out.println("key = " + key);

          // create message by converting string to integer
           String s = "test";
           byte[] bytes = s.getBytes();
           BigInteger message = new BigInteger(s);

          //encryting message
          BigInteger encrypt = key.encrypt(message);

          //decrypting encryption
          BigInteger decrypt = key.decrypt(encrypt);

          //printing values
          System.out.println("message   = " + message);
          System.out.println("encrpyted = " + encrypt);
          System.out.println("decrypted = " + decrypt);
       }
    }

这就是我得到的错误

Exception in thread "main" java.lang.NumberFormatException: For input string: "test"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.math.BigInteger.<init>(BigInteger.java:461)
at java.math.BigInteger.<init>(BigInteger.java:597)
at rsa.main(rsa.java:64)

2 个答案:

答案 0 :(得分:3)

查看代码的那一部分

String s = "test";
byte[] bytes = s.getBytes();
BigInteger message = new BigInteger(s);

现在让我们看一下constructor of BigInteger which accept a String的文档。

  

将BigInteger的十进制字符串表示形式转换为   BigInteger的。 String表示由可选的减号组成   符号后跟一个或多个十进制数字的序列。该   字符到数字的映射由Character.digit提供。字符串   可能不包含任何无关的字符(例如,空格)。

"test"当然不是BigInteger的String表示形式。

好像你想打电话给constructor passing in a byte array

  

转换包含二进制补码二进制的字节数组   将BigInteger表示为BigInteger。输入数组是   假设是大端字节顺序:最重要的字节是   在第0个元素中。

此代码

String s = "test";
byte[] bytes = s.getBytes();
BigInteger message = new BigInteger(bytes);
System.out.println(message);

将打印1952805748

答案 1 :(得分:1)

尝试使用BigInteger message = new BigInteger(bytes);代替BigInteger message = new BigInteger(s);