title: How to use openssl to output encrypted/decrypted message to stdout
body: <p>I am just trying to encrypt/decrypt a password with the shell (non-interactively - it's for an automated script). I am following this example:
  <a href="https://superuser.com/a/20552/362669">https://superuser.com/a/20552/362669</a></p>
  
  <p>I tried converting it to this so that it doesn't use a file output.bin to store the encrypted text:</p>
  
  <pre><code>#!/usr/bin/env bash
  
  cd `dirname "$BASH_SOURCE"`
  
  # generate a 2048-bit RSA key and store it in key.txt
  openssl genrsa -out key.txt 2048
  
  # encrypt "hello world" using the RSA key in key.txt
  encrypted="$(echo "hello world" | openssl rsautl -inkey key.txt -encrypt)"
  
  echo "encrypted: $encrypted"
  
  # decrypt the message and output to stdout
  decrypted="$(echo "$encrypted" | openssl rsautl -inkey key.txt -decrypt)"
  
  echo "decrypted: $decrypted";
  </code></pre>
  
  <p>but all I get is this garbully-guk:</p>
  
  <pre><code>Generating RSA private key, 2048 bit long modulus
  ........................................................................................................................+++
  ............................+++
  e is 65537 (0x10001)
  ��◆J��┌ܥײ��R▒��%⎽F��    1l�}�%��?�0���+��%���C�8|_/!�A"Ꜵ:�������.��W2Pras��1���� ��(�a
  ��]�[�남␍◆�=┬─�з≤�ɦ�;�└�1MFP��^␋�@D�    �T_⎺F�Eπ�2��U2Ÿ┌π��N│�� ⎽��_\2�� 8V��%��(�^���␍4�#�π���*^D ���/�└�
  RSA ⎺⎻␊⎼▒├␋⎺┼ ␊⎼⎼⎺⎼
  4662363756:␊⎼⎼⎺⎼:04FFF06B:⎼⎽▒ ⎼⎺┤├␋┼␊⎽:CRYPTO_␋┼├␊⎼┼▒┌:␉┌⎺␌┐ ├≤⎻␊ ␋⎽ ┼⎺├ 02:/B┤␋┌␍R⎺⎺├/L␋␉⎼▒⎼≤/C▒␌␤␊⎽/␌⎺└.▒⎻⎻┌␊.│␉⎽/S⎺┤⎼␌␊⎽/┌␋␉⎼␊⎽⎽┌/┌␋␉⎼␊⎽⎽┌-22.260.1/┌␋␉⎼␊⎽⎽┌-2.6/␌⎼≤⎻├⎺/⎼⎽▒/⎼⎽▒_⎻┐1.␌:185:
  4662363756:␊⎼⎼⎺⎼:04FFF072:⎼⎽▒ ⎼⎺┤├␋┼␊⎽:CRYPTO_␋┼├␊⎼┼▒┌:⎻▒␍␍␋┼± ␌␤␊␌┐ °▒␋┌␊␍:/B┤␋┌␍R⎺⎺├/L␋␉⎼▒⎼≤/C▒␌␤␊⎽/␌⎺└.▒⎻⎻┌␊.│␉⎽/S⎺┤⎼␌␊⎽/┌␋␉⎼␊⎽⎽┌/┌␋␉⎼␊⎽⎽┌-22.260.1/┌␋␉⎼␊⎽⎽┌-2.6/␌⎼≤⎻├⎺/⎼⎽▒/⎼⎽▒_␊▒≤.␌:580:
  ␍␊␌⎼≤⎻├␊␍: 
  ▒┌␊│⎽-└▒␌:␋┼├␊⎼⎺⎽ ▒┌␊│$ 
  </code></pre>
  
  <p>and my shell session is basically messed up. </p>
  
  <p>Anyone know what that is? Maybe it's outputting characters that the shell can't handle?</p>
  
  <p>Update: if I don't log the encrypted value, then I get this:</p>
  
  <pre><code>Generating RSA private key, 2048 bit long modulus
  ........................+++
  ..........+++
  e is 65537 (0x10001)
  RSA operation error
  4558829164:error:04FFF06B:rsa routines:CRYPTO_internal:block type is not 02:/BuildRoot/Library/Caches/com.apple.xbs/Sources/libressl/libressl-22.260.1/libressl-2.6/crypto/rsa/rsa_pk1.c:185:
  4558829164:error:04FFF072:rsa routines:CRYPTO_internal:padding check failed:/BuildRoot/Library/Caches/com.apple.xbs/Sources/libressl/libressl-22.260.1/libressl-2.6/crypto/rsa/rsa_eay.c:580:
  decrypted: 
  </code></pre>