Java crashes socket sever when trying to make private Key

146 views Asked by At

I am trying to get a file which is created in a different main method, open it, get the bytes inside and then turn them bytes into a private key. When I run the code I get the server exception and the socket ends. This is the output of the code.

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.");
    }

}

The output is the username, so it fetches the file i.e dave.prv I used the system.out.println(".") to work out which line of code is the one breaking it and its the line where its got the // around.

Here is the stack trace

java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : DerInputStream.getLength(): lengthTag=109, too big. at java.base/sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:250) at java.base/java.security.KeyFactory.generatePrivate(KeyFactory.java:390) at Client.main(Client.java:61) Caused by: java.security.InvalidKeyException: IOException : DerInputStream.getLength(): lengthTag=109, too big. at java.base/sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:133) at java.base/sun.security.pkcs.PKCS8Key.(PKCS8Key.java:94) at java.base/sun.security.rsa.RSAPrivateCrtKeyImpl.(RSAPrivateCrtKeyImpl.java:130) at java.base/sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(RSAPrivateCrtKeyImpl.java:85) at java.base/sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java:355) at java.base/sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:246) ... 2 more

0

There are 0 answers