RSA 将字符串转换为 BigInteger

RSA turning string to 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)

看看你的那部分代码

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

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

Translates the decimal String representation of a BigInteger into a BigInteger. The String representation consists of an optional minus sign followed by a sequence of one or more decimal digits. The character-to-digit mapping is provided by Character.digit. The String may not contain any extraneous characters (whitespace, for example).

"test" 当然不是 BigInteger 的字符串表示形式。

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

Translates a byte array containing the two's-complement binary representation of a BigInteger into a BigInteger. The input array is assumed to be in big-endian byte-order: the most significant byte is in the zeroth element.

这个代码

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

将打印 1952805748

尝试 BigInteger message = new BigInteger(bytes); 而不是 BigInteger message = new BigInteger(s);