0

我正在尝试获取一个以不同的主要方法创建的文件,打开它,获取其中的字节,然后将它们转换为私钥。当我运行代码时,我得到服务器异常并且套接字结束。这是代码的输出。

wecjlq
Creating Key Files
Enter Message
dasdas
.
..
...
....
Cannot connect to server.

    
        public class Client {
public static void main(String [] args) throws Exception {
    String host = "localhost"; // hostname of server
    int port = 8080;           // port of server

    try {
        Socket s = new Socket(host, port);
        char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
        StringBuilder sb = new StringBuilder(26);
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            char c = chars[random.nextInt(chars.length)];
            sb.append(c);
        }
        String output = sb.toString();
        System.out.println(output);
        RSAKeyGen.main(new String [] {output});
        DataOutputStream dos = new DataOutputStream(s.getOutputStream());
    
        Scanner scan = new Scanner(System.in);  // Create a Scanner object
        System.out.println("Enter Message");

        String message = scan.nextLine();  // Read user input
        scan.close();
        dos.writeUTF("User ID " + output + ": " + message);
        MessageDigest m = MessageDigest.getInstance("SHA-256");
        m.update(message.getBytes(), 0, message.length());
        BigInteger x = new BigInteger(1,m.digest());
        
        //GET RSA MODULUS N AND PRIVATE EXPONENT D
        System.out.println(".");
        byte[] keyBytes = Files.readAllBytes(Paths.get(output+".prv"));
        System.out.println("..");
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
        System.out.println("...");
        KeyFactory rsaFact = KeyFactory.getInstance("RSA");
        System.out.println("....");
        RSAPrivateKey key = (RSAPrivateKey) rsaFact.generatePrivate(spec);
         key.getModulus();
        
       
        
        System.out.println(".....");
        System.out.println(x);
    } catch (Exception e) {
        System.err.println("Cannot connect to server.");
    }

}

输出是用户名,所以它获取文件,即 dave.prv 我使用 system.out.println(".") 来计算哪一行代码是破坏它的代码行以及它得到 //大约。

这是堆栈跟踪

java.security.spec.InvalidKeySpecException:java.security.InvalidKeyException:IOException:DerInputStream.getLength():lengthTag = 109,太大。在 java.base/sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:250) 在 java.base/java.security.KeyFactory.generatePrivate(KeyFactory.java:390) 在 Client.main(Client.java:61 ) 引起:java.security.InvalidKeyException: IOException : DerInputStream.getLength(): lengthTag=109,太大。在 java.base/sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:133) 在 java.base/sun.security.pkcs.PKCS8Key.(PKCS8Key.java:94) 在 java.base/sun.security。 rsa.RSAPrivateCrtKeyImpl.(RSAPrivateCrtKeyImpl.java:130) 在 java.base/sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(RSAPrivateCrtKeyImpl.java:85) 在 java.base/sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java :355) 在 java.base/sun.security。

4

0 回答 0