0

Hi I'm a student learning about JAVA Cipher Programming. I'm making a JAVA project that send public keys each other and is using socket. But in socket programming, I can send only string and int. So I send public key from server to client. But client received it for string, not public key. I found KeyFactory method. I used KeyFactory for formatting from string to public key, but i failed. Only what I can see was InvalidKeySpecException. I ask this question because I don't know what is the wrong line.

P.S. I'm sorry, it will be difficult for you to understand this question. I'm not good at English because my native language is not English

below is my source code

public class TestSpace {

    public static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        @SuppressWarnings("resource")
        Formatter formatter = new Formatter(sb);
        for (byte b : bytes) {
            formatter.format("%02x", b);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        try {

            KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
            kpg.initialize(1024);
            KeyPair keyPair = kpg.genKeyPair();
            PublicKey pubKey = keyPair.getPublic();
            PrivateKey priKey = keyPair.getPrivate();
            byte[] publicKeyBytes = pubKey.getEncoded();
            byte[] privateKeyBytes = priKey.getEncoded();
            System.out.println("original public key : " + bytesToHex(publicKeyBytes));
            System.out.println("original private key : " + bytesToHex(privateKeyBytes));
            String temp1 = publicKeyBytes.toString();
            String temp2 = privateKeyBytes.toString();
            System.out.println("byte->string public key : " + temp1);
            System.out.println("byte->string private key : " + temp2);
            publicKeyBytes = temp1.getBytes();
            privateKeyBytes = temp2.getBytes();
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(publicKeyBytes));
            priKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKeyBytes));
            publicKeyBytes = pubKey.getEncoded();
            privateKeyBytes = priKey.getEncoded();
            System.out.println("string->byte public key : " + bytesToHex(publicKeyBytes));
            System.out.println("string->byte private key : " + bytesToHex(privateKeyBytes));

        }catch( NoSuchAlgorithmException|InvalidKeySpecException e) {
            e.printStackTrace();
        }
    }
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • Duplicate of https://stackoverflow.com/questions/43372052/convert-string-to-private-and-public-key-rsa/43372169 – Sheshank S. Jun 15 '18 at 13:20
  • No time but `sb.append(formatter.format("%02x", b));` – Joop Eggen Jun 15 '18 at 13:25
  • Welcome to Stack Overflow! Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. Use the [edit] link to improve your *question* - do not add more information via comments. Thanks! – GhostCat Jun 15 '18 at 13:27
  • Your question is missing a clear problem description: as in expected versus actual behavior. – GhostCat Jun 15 '18 at 13:27
  • This is a bit of a rookie mistake: `publicKeyBytes.toString()`, print it out to see what it does. If you need a string representing the byte array, base 64 encode it (at least it has build in support within Java) and decode it again before use (actually, it seems you printed it out and didn't understand the consequences). – Maarten Bodewes Dec 04 '18 at 12:41

0 Answers0