Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Java has got the Java Cryptography Extension Framework, which is just designed for these things.</p> <p>BouncyCastle is a Cryptography Provider for this framework. This means, it provides your Java Cryptography Extension with implementations of cryptography algorithms.</p> <p>You'll find the basic classes for this in the packages <code>java.security</code> and <code>javax.crypto</code></p> <p>To decrypt your message with a public key you could try the following:</p> <pre><code>// Use RSA/NONE/NoPadding as algorithm and BouncyCastle as crypto provider Cipher asymmetricCipher = Cipher.getInstance("RSA/NONE/NoPadding", "BC"); // asume, that publicKeyBytes contains a byte array representing // your public key X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes); KeyFactory keyFactory; keyFactory = KeyFactory.getInstance(publicKeySpec.getFormat()); Key key = keyFactory.generatePublic(publicKeySpec); // initialize your cipher asymmetricCipher.init(Cipher.DECRYPT_MODE, key); // asuming, cipherText is a byte array containing your encrypted message byte[] plainText = asymmetricCipher.doFinal(cipherText); </code></pre> <p>Please note, that this example is <strong>very</strong> basic and lacks several try catch blocks. Also, you should not use an asymmetric cipher without padding as this makes you vulnerable to replay attacks. You may also encounter issues with the key length. In some Java packages, the maximum allowed key length is restricted. This may be solved by using the unlimited strength policy files.</p> <p>I hope, this helps you in getting started with the Java cryptography.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload