Encryption help Needed

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

    Encryption help Needed

    I'm having trouble decrypting a file I encrypted and wrote to the
    server - the following code displays the $decrypted_stri ng variable
    but the data is still encrypted - any help would be appreciated:

    $handle = fopen("/home/public_html/testfolder/test.txt","r");
    $buffer = fgets($handle, 4096);
    fclose($handle) ;

    // Encryption/decryption key
    $key = "twenty one years ago";

    // Encryption Algorithm
    $cipher_alg = MCRYPT_RIJNDAEL _128;

    // Create the initialization vector for added security.
    $iv = mcrypt_create_i v(mcrypt_get_iv _size($cipher_a lg,
    MCRYPT_MODE_ECB ), MCRYPT_RAND);

    $decrypted_stri ng = mcrypt_decrypt( $cipher_alg, $key, $buffer,
    MCRYPT_MODE_CBC , $iv);

    print "Decrypted string: $decrypted_stri ng";

  • Ralph Freshour

    #2
    Re: Encryption help Needed

    Yes they are both defined - I did the echo for both and they displayed
    on the web page.

    I was wondering if maybe that iv parameter is the problem? does it
    generate one data when I encrypt it and then another when I decrypt
    it? The reason I wonder is that I can encrypt and decrypt
    successfully if I do it in the same php script but if I encrypt only,
    save the encryption string to disk and then use another php script to
    open the file and decrypt it, it doesn't decrypt, it stays
    scrambled???

    If you run this you'll see that it works to encrypt and decrypt but if
    I try to use just the part to encrypt and put the parts to decrypt
    only in another script it won't decrypt:

    /* Open the cipher */
    // $td = mcrypt_module_o pen ('rijndael-256', '', 'ofb', '');
    $td = mcrypt_module_o pen ('rijndael-256', '', 'ecb', '');

    /* Create the IV and determine the keysize length */
    $iv = mcrypt_create_i v (mcrypt_enc_get _iv_size($td),
    MCRYPT_DEV_RAND OM);
    $ks = mcrypt_enc_get_ key_size ($td);

    /* Create key */
    $key = "follow the yellow brick road";

    /* Intialize encryption */
    mcrypt_generic_ init ($td, $key, $iv);

    /* Encrypt data */
    $encrypted = mcrypt_generic ($td, 'secret 2 password');

    /* Terminate encryption handler */
    mcrypt_generic_ deinit ($td);

    /* Initialize encryption module for decryption */
    mcrypt_generic_ init ($td, $key, $iv);

    /* Decrypt encrypted string */
    $decrypted = mdecrypt_generi c ($td, $encrypted);

    /* Terminate decryption handle and close module */
    mcrypt_generic_ deinit ($td);
    mcrypt_module_c lose ($td);

    /* Show string */
    print trim ($decrypted)."\ n";


    $handle = fopen("/home/rfresh/public_html/ssfiles/mysqlpw.enc","w ");
    fputs($handle, $encrypted. "\n");
    fclose($handle) ;



    On Fri, 01 Aug 2003 10:37:08 +0200, stephan beal
    <stephan@wander inghorse.net> wrote:
    [color=blue]
    >Ralph Freshour wrote:[color=green]
    >> $cipher_alg = MCRYPT_RIJNDAEL _128;[/color]
    >
    >Are you 100% that MCRYPT_RIJNDAEL _128 is defined for your system? Try:
    >
    >echo MCRYPT_RIJNDAEL _128;
    >[color=green]
    >> // Create the initialization vector for added security.
    >> $iv = mcrypt_create_i v(mcrypt_get_iv _size($cipher_a lg,
    >> MCRYPT_MODE_ECB ), MCRYPT_RAND);[/color]
    >
    >Same for MCRYPT_MODE_ECB .
    >
    >i ask because certain constants are only defined is specific libs are linked
    >in or if PHP was configured with them.[/color]

    Comment

    • ArSeNiK

      #3
      Re: Encryption help Needed

      hi mate,

      here is some func to encode / decode just change cypher type :

      DEFINE ("Keysize","hel lo world !");

      function enc($data) {
      $td = mcrypt_module_o pen (MCRYPT_TripleD ES, "", MCRYPT_MODE_ECB ,
      "");
      $iv = mcrypt_create_i v (mcrypt_enc_get _iv_size ($td), MCRYPT_RAND);
      $encdata = mcrypt_ecb (MCRYPT_TripleD ES,(Keysize), $data,
      MCRYPT_ENCRYPT, $iv);
      $hextext=bin2he x($encdata);
      return $hextext;
      }

      function dec($data) {
      $td = mcrypt_module_o pen (MCRYPT_TripleD ES, "", MCRYPT_MODE_ECB ,
      "");
      $iv = mcrypt_create_i v (mcrypt_enc_get _iv_size ($td), MCRYPT_RAND);
      $dectext = trim(mcrypt_ecb (MCRYPT_TripleD ES,(Keysize),
      hex2bin($data), MCRYPT_DECRYPT, $iv));

      return $dectext;
      }

      function hex2bin($data) {
      $len = strlen($data);
      return pack("H" . $len, $data);
      }

      hope it help :)

      ++


      "Ralph Freshour" <ralph@primemai l.com> a écrit dans le message de
      news:i6gjiv8719 96hphta21cvbq05 b87mn68rk@4ax.c om...[color=blue]
      > I'm having trouble decrypting a file I encrypted and wrote to the
      > server - the following code displays the $decrypted_stri ng variable
      > but the data is still encrypted - any help would be appreciated:
      >
      > $handle = fopen("/home/public_html/testfolder/test.txt","r");
      > $buffer = fgets($handle, 4096);
      > fclose($handle) ;
      >
      > // Encryption/decryption key
      > $key = "twenty one years ago";
      >
      > // Encryption Algorithm
      > $cipher_alg = MCRYPT_RIJNDAEL _128;
      >
      > // Create the initialization vector for added security.
      > $iv = mcrypt_create_i v(mcrypt_get_iv _size($cipher_a lg,
      > MCRYPT_MODE_ECB ), MCRYPT_RAND);
      >
      > $decrypted_stri ng = mcrypt_decrypt( $cipher_alg, $key, $buffer,
      > MCRYPT_MODE_CBC , $iv);
      >
      > print "Decrypted string: $decrypted_stri ng";
      >[/color]


      Comment

      • Ralph Freshour

        #4
        Re: Encryption help Needed

        Thanks - that worked great!!!!


        On Sat, 2 Aug 2003 02:22:20 +0200, "ArSeNiK" <ArSeNiK@BisKot t.Com>
        wrote:
        [color=blue]
        >hi mate,
        >
        >here is some func to encode / decode just change cypher type :
        >
        >DEFINE ("Keysize","hel lo world !");
        >
        >function enc($data) {
        > $td = mcrypt_module_o pen (MCRYPT_TripleD ES, "", MCRYPT_MODE_ECB ,
        >"");
        > $iv = mcrypt_create_i v (mcrypt_enc_get _iv_size ($td), MCRYPT_RAND);
        > $encdata = mcrypt_ecb (MCRYPT_TripleD ES,(Keysize), $data,
        >MCRYPT_ENCRYPT , $iv);
        > $hextext=bin2he x($encdata);
        > return $hextext;
        >}
        >
        >function dec($data) {
        > $td = mcrypt_module_o pen (MCRYPT_TripleD ES, "", MCRYPT_MODE_ECB ,
        >"");
        > $iv = mcrypt_create_i v (mcrypt_enc_get _iv_size ($td), MCRYPT_RAND);
        > $dectext = trim(mcrypt_ecb (MCRYPT_TripleD ES,(Keysize),
        >hex2bin($data) , MCRYPT_DECRYPT, $iv));
        >
        > return $dectext;
        >}
        >
        >function hex2bin($data) {
        > $len = strlen($data);
        > return pack("H" . $len, $data);
        >}
        >
        >hope it help :)
        >
        >++
        >
        >
        >"Ralph Freshour" <ralph@primemai l.com> a écrit dans le message de
        >news:i6gjiv871 996hphta21cvbq0 5b87mn68rk@4ax. com...[color=green]
        >> I'm having trouble decrypting a file I encrypted and wrote to the
        >> server - the following code displays the $decrypted_stri ng variable
        >> but the data is still encrypted - any help would be appreciated:
        >>
        >> $handle = fopen("/home/public_html/testfolder/test.txt","r");
        >> $buffer = fgets($handle, 4096);
        >> fclose($handle) ;
        >>
        >> // Encryption/decryption key
        >> $key = "twenty one years ago";
        >>
        >> // Encryption Algorithm
        >> $cipher_alg = MCRYPT_RIJNDAEL _128;
        >>
        >> // Create the initialization vector for added security.
        >> $iv = mcrypt_create_i v(mcrypt_get_iv _size($cipher_a lg,
        >> MCRYPT_MODE_ECB ), MCRYPT_RAND);
        >>
        >> $decrypted_stri ng = mcrypt_decrypt( $cipher_alg, $key, $buffer,
        >> MCRYPT_MODE_CBC , $iv);
        >>
        >> print "Decrypted string: $decrypted_stri ng";
        >>[/color]
        >[/color]

        Comment

        Working...