How do I encrypt a string?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    How do I encrypt a string?

    Has anyone got a function (or class) that will take a string and encrypt it
    into printable characters, then a similar function that will decrypt it
    again, without the need to call external programs (eg: pgp, etc)?

    Ideally the number of characters in the encrypted strng should not be the
    same as the decrypted version either.

    Thanks,
    Chris


  • ZeldorBlat

    #2
    Re: How do I encrypt a string?

    Try the mcrypt library or the rot13() function.

    Comment

    • BKDotCom

      #3
      Re: How do I encrypt a string?

      what kind of key do you need?
      a simple encrypt/decrypt key, or a public/private key?

      here's blowfish encryption using the mcrypt library

      // returns base64_encoded string
      function encrypt_blowfis h($key,$data)
      {
      $mode = MCRYPT_MODE_CBC ;
      $iv_size = mcrypt_get_iv_s ize(MCRYPT_BLOW FISH, $mode);
      $iv = mcrypt_create_i v($iv_size, MCRYPT_RAND);
      $encrypted = $iv.mcrypt_encr ypt(MCRYPT_BLOW FISH, $key, $data,
      $mode, $iv);
      $encrypted = base64_encode($ encrypted);
      return $encrypted;
      }

      // encrypted should be base64_encoded
      function decrypt_blowfis h($key,$encrypt ed)
      {
      $mode = MCRYPT_MODE_CBC ;
      $iv_size = mcrypt_get_iv_s ize(MCRYPT_BLOW FISH, $mode);
      $iv = mcrypt_create_i v($iv_size, MCRYPT_RAND);
      $encrypted = base64_decode($ encrypted);
      $decrypted = substr(trim(mcr ypt_decrypt(MCR YPT_BLOWFISH, $key,
      $encrypted, $mode, $iv),"\0"),$iv_ size);
      return $decrypted;
      }

      Comment

      Working...