java - RSA 将字符串转换为 BigInteger

标签 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)

最佳答案

看看你的代码的那部分

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

关于java - RSA 将字符串转换为 BigInteger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28838136/

相关文章:

java - 在 AutoCompleteTextView 中选择一个选项时不切换到另一个 Activity

java - 对 Java 聊天应用程序进行单元测试

Netbeans 上的 java.sql.SQLException : Parameter index out of range (5 > number of parameters, 是 4)

java - Java 的 RSA_PKCS1_OAEP_PADDING 等效项

c++ - 你能帮我了解一下在 C++ 中使用 rsa.h 进行 openssl 公钥加密吗?

java - El Gamal 数字签名构建莫名其妙地失败

java - 为什么 Java 中的 BigInteger.isProbablePrime() 函数对负数返回 true?

java - 查看 Java 中包含的算法的实现细节

c# - 是否可以在没有昂贵的 OID 查找的情况下使用 RsaCryptoServiceProvider 调用 SignHash 和 VerifyHash?

java - BigInteger 的对数