Note that there are some explanatory texts on larger screens.

plurals
  1. PODecrypted string not the same as pre encrypted string
    primarykey
    data
    text
    <p>I have the following pieces of code:</p> <p><strong>Globals</strong></p> <pre><code>public static PublicKey pubKey; public static PrivateKey privKey; public static Cipher cip; </code></pre> <p><strong>Main</strong></p> <pre><code>public static void main(String[] args) throws Exception { //Generate the keys KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(1024); KeyPair kp = kpg.genKeyPair(); Key publicKey = kp.getPublic(); Key privateKey = kp.getPrivate(); KeyFactory fact = KeyFactory.getInstance("RSA"); cip = Cipher.getInstance("RSA/ECB/NoPadding"); // Store Public Key. X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec( publicKey.getEncoded()); FileOutputStream fos = new FileOutputStream("public.key"); fos.write(x509EncodedKeySpec.getEncoded()); fos.close(); // Store Private Key. PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec( privateKey.getEncoded()); fos = new FileOutputStream("private.key"); fos.write(pkcs8EncodedKeySpec.getEncoded()); fos.close(); //Get the public and private keys out of their files getPubAndPrivateKey(); //Check if the keys gotten out of the files are the same as the generated files (this returns truetrue) System.out.print(publicKey.equals(pubKey)); System.out.print(privateKey.equals(privKey)); byte[] text = "This is my super secret secret".getBytes(); encryptToFile("encrypted.txt", text ); decryptToFile("encrypted.txt", "decrypted.txt"); } </code></pre> <p><strong>Getting the keys from the files</strong></p> <pre><code>private static void getPubAndPrivateKey() throws IOException, Exception { // Read Public Key. File filePublicKey = new File("public.key"); FileInputStream fis = new FileInputStream("public.key"); byte[] encodedPublicKey = new byte[(int) filePublicKey.length()]; fis.read(encodedPublicKey); fis.close(); // Read Private Key. File filePrivateKey = new File("private.key"); fis = new FileInputStream("private.key"); byte[] encodedPrivateKey = new byte[(int) filePrivateKey.length()]; fis.read(encodedPrivateKey); fis.close(); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec( encodedPublicKey); pubKey = keyFactory.generatePublic(publicKeySpec); PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec( encodedPrivateKey); privKey = keyFactory.generatePrivate(privateKeySpec); } </code></pre> <p><strong>Encrypting</strong></p> <pre><code> public static void encryptToFile(String fileName, byte[] data) throws IOException { try { cip.init(Cipher.ENCRYPT_MODE, privKey); byte[] cipherData = cip.doFinal(data); String encryptedData = cipherData.toString(); BufferedWriter out = new BufferedWriter(new FileWriter(fileName)); out.write(encryptedData); out.close(); } catch (Exception e) { throw new RuntimeException("Spurious serialisation error", e); } } </code></pre> <p><strong>Decrypting</strong></p> <pre><code>private static void decryptToFile(String string, String string2) throws Exception { try { File encryptedFile = new File("encrypted.txt"); byte[] encrypted = getContents(encryptedFile).getBytes(); cip = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cip.init(Cipher.DECRYPT_MODE, pubKey); byte[] cipherData = cip.doFinal(encrypted); String decryptedData = cipherData.toString(); BufferedWriter out = new BufferedWriter(new FileWriter( "decrypted.txt")); out.write(decryptedData); out.close(); } catch (Exception e) { throw e; } } </code></pre> <p><strong>Things I already checked</strong></p> <ul> <li>The data used in the decryption is the same as in the encrypted file</li> <li>The generated keys are the same as the ones gotten from the file</li> <li>The encryption and decryption both don't give errors</li> </ul> <p><strong>Results</strong><br> Original string:<br> My super secret secret<br> The encryption results in:<br> [B@1747b17 The decryption results in: [B@91a4fb</p>
    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.
 

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