title: Encrypting a string using OpenSSL AES256 with a shared password and a specific salt value
body: <p>In my c# program (shown below) I'm using the following way to encrypt a string before transmitting it over the internet to another server for further processing. Now I have to make a bash script that does the exact same thing, but I've nearly never touched a linux server before, let stand using encryption in linux.</p>
  
  <p>Currently I've managed to get as far as possible as this, but since it isn't getting accepted on the remote server, I guess I've probably forgotton something important.</p>
  
  <pre><code>encodedValue=$(echo $stringValue| openssl enc -aes-256-cbc -a -pass pass:$sharedSecret)
  encodedValue=$( echo $encodedValue| tr -d ' ' )
  
  # - base64 it
  encodedValue=$( echo $(echo $encodedValue| base64) | tr -d ' ' )
  </code></pre>
  
  <p>As far as I already see the differences, I'm not giving the Salt to the openssl encoding, but I haven't found any way to do this using OpenSSL.</p>
  
  <p>Can anybody point me in the good direction or show me what I'm doing wrong?</p>
  
  <p>The original working c# code is shown below:</p>
  
  <pre><code>RijndaelManaged aesAlg = null;
  
  try
  {
      var key = new Rfc2898DeriveBytes(sharedSecret, Salt);
  
      aesAlg = new RijndaelManaged();
      aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
  
      ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
      using (var msEncrypt = new MemoryStream())
      {
          msEncrypt.Write(BitConverter.GetBytes(aesAlg.IV.Length), 0, sizeof(int));
          msEncrypt.Write(aesAlg.IV, 0, aesAlg.IV.Length);
  
          using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
          {
              using (var swEncrypt = new StreamWriter(csEncrypt))
              {
                  swEncrypt.Write(plainText);
              }
          }
          outStr = Convert.ToBase64String(msEncrypt.ToArray());
      }
  }
  </code></pre>