Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>[B@cb7e2c</code> is not the output of your encryption. It's the result of trying to print or call <code>toString()</code> on a byte[] object. (For example, look at the result of <code>System.out.println(new byte[0]);</code>) </p> <p>Try feeding the encrypted byte[] directly back into the decryption function, and use <code>new String(dec)</code> to print the results. If you want to view/save the encrypted data as a string, encode it as hex or base64.</p> <p>Here's the distinction. <code>byte[]</code> means an array of bytes. It is binary data, a series of 8 bit signed numbers. If you're used to working with ascii only, the distinction between a series of <code>byte</code>s and a <code>String</code> might seem trivial, but there are many ways to represent strings in binary. The encryption and decryption you're doing doesn't care what the string looks like or if the data represents a string at all; it's just looking at the bits. </p> <p>If you want to encrypt a string, you'll need to convert it to a series of bytes. At the other end, once you've decrypted the bytes that make up a the string, you'll need to convert them back. <code>myString.getBytes()</code> and <code>new String(myBytea)</code> are often effective, but a bit sloppy since they just use default encoding. If Alice's system used utf-8 and Bob's used utf-16, her message wouldn't make much sense to him. So it's always best to specify the character encoding using, for instance, <code>myString.getBytes("utf-8")</code> and <code>new String(myBytea,"utf-8")</code>.</p> <p>Here are a few functions from a project I'm working on, along with a demonstration <code>main</code> function:</p> <pre><code>import java.security.InvalidKeyException; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.SecureRandom; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.xml.bind.DatatypeConverter; public class RSAExample { private static byte[] h2b(String hex){ return DatatypeConverter.parseHexBinary(hex); } private static String b2h(byte[] bytes){ return DatatypeConverter.printHexBinary(bytes); } private static SecureRandom sr = new SecureRandom(); public static KeyPair newKeyPair(int rsabits) throws NoSuchAlgorithmException { KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); generator.initialize(rsabits, sr); return generator.generateKeyPair(); } public static byte[] pubKeyToBytes(PublicKey key){ return key.getEncoded(); // X509 for a public key } public static byte[] privKeyToBytes(PrivateKey key){ return key.getEncoded(); // PKCS8 for a private key } public static PublicKey bytesToPubKey(byte[] bytes) throws InvalidKeySpecException, NoSuchAlgorithmException{ return KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(bytes)); } public static PrivateKey bytesToPrivKey(byte[] bytes) throws InvalidKeySpecException, NoSuchAlgorithmException{ return KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(bytes)); } public static byte[] encryptWithPubKey(byte[] input, PublicKey key) throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException { Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, key); return cipher.doFinal(input); } public static byte[] decryptWithPrivKey(byte[] input, PrivateKey key) throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException { Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.DECRYPT_MODE, key); return cipher.doFinal(input); } public static void main(String[] args) throws Exception { KeyPair kp = newKeyPair(1&lt;&lt;11); // 2048 bit RSA; might take a second to generate keys PublicKey pubKey = kp.getPublic(); PrivateKey privKey = kp.getPrivate(); String plainText = "Dear Bob,\nWish you were here.\n\t--Alice"; byte[] cipherText = encryptWithPubKey(plainText.getBytes("UTF-8"),pubKey); System.out.println("cipherText: "+b2h(cipherText)); System.out.println("plainText:"); System.out.println(new String(decryptWithPrivKey(cipherText,privKey),"UTF-8")); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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