开发者

RSA Encryption Problem

Basically, I'm trying to have an encrypted data flow between Java client and a c# server. Before jumping into the deep water of having a multi platform encryption working, I'm trying to make a simple encryption app but I'm stuck at the very beginning.

I have the following simple code:

String text = "hello";
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
        kpg.initialize(2048);
        KeyPair kp = kpg.genKeyPair();
        Key publicKey = kp.getPublic();
        Key privateKey = kp.getPrivate();

        Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] cipherData = cipher.doFinal(text.getBytes());

        cipher = Cipher.getInstance("RSA/ECB/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] textData = cipher.doFinal(text.getBytes());

        String decrypted 开发者_JAVA技巧= new String(textData);
        System.out.println(decrypted);

No exception is thrown but I don't get the original "hello" text after the decryption. Any ideas? 10x a lot


This looks fishy:

byte[] textData = cipher.doFinal(text.getBytes());

Did you mean:

byte[] textData = cipher.doFinal(cipherData);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜