title: Convert base64-encoded string to normal string in swift 3
body: <p>I want to convert following base64-encoded <code>String</code> in Swift 3:</p>
  
  <pre><code>dfYcSGpvBqyzvkAXkdbHDA==
  </code></pre>
  
  <p>to its equivalant <code>String</code>:</p>
  
  <pre><code>uöHjo¬³¾@‘ÖÇ
  </code></pre>
  
  <p>Following websites do the job very fine:</p>
  
  <p><a href="http://www.motobit.com/util/base64-decoder-encoder.asp" rel="nofollow noreferrer">http://www.motobit.com/util/base64-decoder-encoder.asp</a></p>
  
  <p><a href="http://www.utilities-online.info/base64/#.WG-FwrFh2Rs" rel="nofollow noreferrer">http://www.utilities-online.info/base64/#.WG-FwrFh2Rs</a></p>
  
  <p>So does the PHP's function <code>base64_decode</code>. The <a href="http://php.net/manual/en/function.base64-decode.php" rel="nofollow noreferrer">documentation</a> of this function says: </p>
  
  <blockquote>
    <p>Returns FALSE if input contains character from outside the base64
    alphabet.</p>
  </blockquote>
  
  <p>But I am unable to do the same in Swift 3. Following code doesn't do the job too:</p>
  
  <pre><code>func convertBase64ToNormalString(base64String:String)-&gt;String!{
     let decodedData = Data(base64Encoded: base64String, options:    Data.Base64DecodingOptions())
     let bytes = decodedData?.bytes
     return String(bytes: bytes, encoding: NSUTF8StringEncoding)
  }
  </code></pre>
  
  <p>Here is the contextual information about why I need to convert the base64 string into an string:
  My Php developer wants me to send all APIs params values encrypted with AES algorithm. For that, I am using <a href="https://github.com/krzyzanowskim/CryptoSwift#aes" rel="nofollow noreferrer">this lib</a>.
  He has given me AES key in Hex format (I mentioned in my <a href="https://stackoverflow.com/questions/41485494/convert-hex-encoded-string-to-string-in-swift-3">last question</a>) and iv in base64 (given above) and he instructed me decode this base64 key before using because he was also doing the same in his PHP code. Here is his PHP code of encryption and decryption:</p>
  
  <pre><code>function encryptParamAES($plaintext, $encryptionEnabled = true) {
      if (! $encryptionEnabled) {
          return $plaintext;
      }
      // --- ENCRYPTION ---
  
      // Constants =========================================================
      // the key should be random binary, use scrypt, bcrypt or PBKDF2 to
      // convert a string into a key
      // key is specified using hexadecimal
      $key = pack ( 'H*', "dcb04a9e103a5cd8b53763051cef09bc66abe029fdebae5e1d417e2ffc2a07a4" );
      // create a random IV to use with CBC encoding
      $iv_size = 16;
      $iv = base64_decode ( "dfYcSGpvBqyzvkAXkdbHDA==" );
  
      // End Of constants ===================================================
  
      // echo "IV: " . base64_encode ( $iv ) . "\n&lt;br&gt;";
      // echo "IV Size: " . $iv_size . "\n&lt;br&gt;";
  
      // show key size use either 16, 24 or 32 byte keys for AES-128, 192
      // and 256 respectively
  
      // $key_size = strlen ( $key );
      // echo "Key size: " . $key_size . "\n&lt;br&gt;&lt;br&gt;";
  
      // creates a cipher text compatible with AES (Rijndael block size = 128)
      // to keep the text confidential
      // only suitable for encoded input that never ends with value 00h
      // (because of default zero padding)
      $ciphertext = mcrypt_encrypt ( MCRYPT_RIJNDAEL_128, $key, $plaintext, MCRYPT_MODE_CBC, $iv );
  
      // prepend the IV for it to be available for decryption
      $ciphertext = $iv . $ciphertext;
  
      // encode the resulting cipher text so it can be represented by a string
      $ciphertext_base64 = base64_encode ( $ciphertext );
  
      return $ciphertext_base64;
  }
  
  
  function decryptParamAES($ciphertext_base64, $encryptionEnabled = true) {
      if (! $encryptionEnabled) {
          return $ciphertext_base64;
      }
      // --- DECRYPTION ---
  
      // Constants =========================================================
      // the key should be random binary, use scrypt, bcrypt or PBKDF2 to
      // convert a string into a key
      // key is specified using hexadecimal
      $key = pack ( 'H*', "dcb04a9e103a5cd8b53763051cef09bc66abe029fdebae5e1d417e2ffc2a07a4" );
      // create a random IV to use with CBC encoding
      $iv_size = 16;
      $iv = base64_decode ( "dfYcSGpvBqyzvkAXkdbHDA==" );
  
      // End Of constants ===================================================
  
      $ciphertext_dec = base64_decode ( $ciphertext_base64 );
  
      // retrieves the IV, iv_size should be created using mcrypt_get_iv_size()
      $iv_dec = substr ( $ciphertext_dec, 0, $iv_size );
  
      // retrieves the cipher text (everything except the $iv_size in the front)
      $ciphertext_dec = substr ( $ciphertext_dec, $iv_size );
  
      // may remove 00h valued characters from end of plain text
      $plaintext_dec = mcrypt_decrypt ( MCRYPT_RIJNDAEL_128, $key, $ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec );
      return rtrim ( $plaintext_dec );
  }
  </code></pre>
  
  <p>I just saw this PHP code and wondered, why he is not using $iv as <code>mcrypt_decrypt</code> function's last param!! Will update you on it. But still question remains the same, PHP function <code>base64_decode</code> doesn't return <code>FALSE</code> for the above base64 string!
  I tested this function myself by terminal command: <code>php test.php</code>. Here test.php contains following code:</p>
  
  <pre><code>&lt;?php
  $iv = base64_decode ( "dfYcSGpvBqyzvkAXkdbHDA==" );
  echo $iv;
  ?&gt;
  </code></pre>
  
  <p>And the output was: <code>u?Hjo???@???</code></p>