Welcome Guest, Not a member yet? Register   Sign In
encrypt on client side decrypt on server side
#1

[eluser]freezy[/eluser]
hello every one...
i try to do an encryption on client side and do dectryptio on server side and vice versa. i found interesting AES implementation written on javascript and php. I modify the example on php ('<!-- test harness... -->'), and trying to encrypt on client and decrypt on server and vice versa. It work well!!!, I can copy and paste encrypted texts between this script and the JavaScript version and they work perfectly.

I copy paste the code to the CI controller class, and make a modification to the script and run the page. The page load well, but looks like the encrypt and the decrypt process doesn't. Please...., anyone can help me with this???

this is the non CI class code
Code:
<?php

     //php AES implementation
?>
<!-- test harness... -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;title&gt;AES in PHP test harness&lt;/title&gt;
&lt;/head&gt;

&lt;!--- the AES javascript impl ----&gt;
&lt;!--- [removed][removed] ---&gt;

&lt;!--- page script ----&gt;
&lt;!---
[removed]
function encrypt_on_client(plaintext, password, nBits){
    var aa = AESEncryptCtr(plaintext, password, nBits);
    document.frm.cipher.value = aa;
}
[removed]
---&gt;

&lt;body&gt;
&lt;? $pw = isset($_POST['pw']) ? stripslashes($_POST['pw']) : "L0ck it up saf3";
   $pt = isset($_POST['pt']) ? stripslashes($_POST['pt']) : "pssst ... đon't tell anyøne!";
   $encr = isset($_POST['encr']) ? $_POST['cipher'] : '';
   $decr = isset($_POST['decr']) ? AESDecryptCtr($_POST['cipher'], $pw, 256) : '';
?&gt;  

&lt;form name="frm" id="frm" method="post" action="./aes.php"&gt; &lt;!-- same-document reference --&gt;
  <table>  
    <tr>
      <td>Password:</td>
      <td>&lt;input type="text" name="pw" size="16" value="&lt;?= $pw ?&gt;"&gt;&lt;/td>
    </tr>
    <tr>
      <td>Plaintext:</td>
      <td>&lt;input type="text" name="pt" size="40" value="&lt;?= $pt ?&gt;"&gt;&lt;/td>
    </tr>
    <tr>
      <td></td>
      <td>&lt;input type="text" name="cipher" size="80" value="&lt;?=$encr?&gt;"&gt;&lt;/td>
    </tr>
    <tr>
      <td></td>
      <td>&lt;input type="text" size="40" value="&lt;?= $decr ?&gt;"&gt;&lt;/td>
    </tr>
  </table>
  &lt;input type="button" name="encr" value="Encrypt it:"&gt;&lt;!--- onclick="encrypt_on_client(document.frm.pt.value, document.frm.pw.value, 256)" ---&gt;
  &lt;input type="submit" name="decr" value="Decrypt it:"&gt;
  &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

this is the CI controller
Code:
&lt;?php
class Blah extends Controller {
      
    function Blah(){
        parent::Controller();
        $this->load->helper('url');
    }

    function blah_blah(){
        $pw = $this->input->post('pw');
        $pw = !$pw? "L0ck it up saf3" : $pw;
      
           $pt = $this->input->post('pt');
        $pt = !$pt? "pssst ... đon't tell anyone!" : $pt;
      
           $encr = $this->input->post('encr');
        if ($encr){ $encr = $this->AESEncryptCtr($pt, $pw, 256); }
        else{ $encr = $this->input->post('cipher'); }
        
           $decr = $this->input->post('decr');
        if ($decr){ $decr = $this->AESDecryptCtr($_POST['cipher'], $pw, 256); }
        else { $decr = $this->input->post('plain'); }
      
        $data = array(
            'pw'=>$pw,
            'pt'=>$pt,
            'encr'=>$encr,
            'decr'=>$decr,
            'server_side'=>$this->AESEncryptCtr($pt, $pw, 256)
            );
          
        $this->load->view('users/login', $data);
    }

    //AES implementation php code
}

and the CI view
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;title&gt;AES in PHP test harness&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;!--- the AES javascript impl ----&gt;
&lt;!--- [removed][removed] ---&gt;

&lt;form name="frm" id="frm" method="post" action=""&gt;
  <table>  
    <tr>
      <td>Password:</td>
      <td>&lt;input type="text" name="pw" size="16" value="&lt;?= $pw ?&gt;"&gt;&lt;/td>
    </tr>
    <tr>
      <td>Plaintext:</td>
      <td>&lt;input type="text" name="pt" size="300" value="&lt;?= $pt ?&gt;"&gt;&lt;/td>
    </tr>
    <tr>
      <td>&lt;input type="submit" name="encr" value="Encrypt it:"&gt;&lt;/td>
      <td>&lt;input type="text" name="cipher" size="300" value="&lt;?= $encr ?&gt;"&gt;&lt;/td>
    </tr>
    <tr>
      <td>&lt;input type="submit" name="decr" value="Decrypt it:"&gt;&lt;/td>
      <td>&lt;input type="text" name="plain" size="300" value="&lt;?= $decr ?&gt;"&gt;&lt;/td>
    </tr>
  </table>
&lt;/form&gt;
&lt;input type="button" value="enc client side"/&gt;&lt;!--- onclick="[removed] encry()" ----&gt;

&lt;!--- on page script ----&gt;
[removed]
function encry(){
    document.getElementsByName('cipher')[0].value = AESEncryptCtr(document.getElementsByName('pt')[0].value, document.getElementsByName('pw')[0].value, 256);
}
[removed]
&lt;!--- End on page script ----&gt;
<p>

&lt;!--- use for copy and paste encrypted text ----&gt;
[removed]
&lt;!--- doc.write('this is client site (copy to above): ' + AESEncryptCtr(document.getElementsByName('pt')[0].value, document.getElementsByName('pw')[0].value, 256) + '<br>'); ---&gt;
[removed]

&lt;!--- use for copy and paste encrypted text ----&gt;
&lt;?= 'this is server site (copy to above) : ' . $server_side . '<br>' ?&gt;
</p>

&lt;/body&gt;
&lt;/html&gt;
#2

[eluser]pistolPete[/eluser]
The forum software did strip out some javascript, so the source code is not complete:
Code:
[removed]

Quote:The page load well, but looks like the encrypt and the decrypt process doesn’t.
Do you get any error messages? What did you expect, what was the result?

Some general comment:
Why do you want to encrypt the client <-> server communication using AES and javascript? Why don't you just use SSL?
#3

[eluser]Rob Steele[/eluser]
That's what i'd do personally
#4

[eluser]freezy[/eluser]
Quote:Some general comment:
Why do you want to encrypt the client <-> server communication using AES and javascript? Why don’t you just use SSL?

Quote:That’s what i’d do personally
yeah you right guys, absolutely aggree. we can use SSL to implement secure connection. can't tell you guys why i can't use SSL, i'm sorry , thats G** D*** too embarrassin to tell here.

Quote:The forum software did strip out some javascript, so the source code is not complete:
Quote:[removed]

the removed script doesn't have special meaning, just a simple script (e.g import the js).
this is the non CI class code
Code:
&lt;?php

     //php AES implementation
?&gt;
&lt;!-- test harness... --&gt;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;title&gt;AES in PHP test harness&lt;/title&gt;
&lt;/head&gt;
&lt;script src="aes.js"&gt;&lt;/script&gt;
&lt;script&gt;
   function encrypt_on_client(plaintext, password, nBits){
      var aa = AESEncryptCtr(plaintext, password, nBits);
      document.frm.cipher.value = aa;
   }
   function decrypt_on_server(){
      document.frm.submit();
   }
&lt;/script&gt;

&lt;body&gt;
&lt;? $pw = isset($_POST['pw']) ? stripslashes($_POST['pw']) : "L0ck it up saf3";
   $pt = isset($_POST['pt']) ? stripslashes($_POST['pt']) : "pssst ... đon't tell anyøne!";
   $encr = isset($_POST['encr']) ? $_POST['cipher'] : '';
   $decr = isset($_POST['decr']) ? AESDecryptCtr($_POST['cipher'], $pw, 256) : '';
?&gt;  

&lt;form name="frm" id="frm" method="post" action="./aes.php"&gt; &lt;!-- same-document reference --&gt;
  <table>  
    <tr>
      <td>Password:</td>
      <td>&lt;input type="text" name="pw" size="16" value="&lt;?= $pw ?&gt;"&gt;&lt;/td>
    </tr>
    <tr>
      <td>Plaintext:</td>
      <td>&lt;input type="text" name="pt" size="40" value="&lt;?= $pt ?&gt;"&gt;&lt;/td>
    </tr>
    <tr>
      <td></td>
      <td>&lt;input type="text" name="cipher" size="80" value="&lt;?=$encr?&gt;"&gt;&lt;/td>
    </tr>
    <tr>
      <td></td>
      <td>&lt;input type="text" size="40" value="&lt;?= $decr ?&gt;"&gt;&lt;/td>
    </tr>
  </table>
  &lt;input type="button" name="encr" value="Encrypt it:" onclick="encrypt_on_client(document.frm.pt.value, document.frm.pw.value, 256)"&lt;
  &lt;input type="submit" name="decr" value="Decrypt it:"&gt;
  &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
this is the CI View
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;title&gt;AES in PHP test harness&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;script src="&lt;?=site_url('AES.js')?&gt;"&gt;&lt;/script&gt;
&lt;form name="frm" id="frm" method="post" action=""&gt;
  <table>  
    <tr>
      <td>Password:</td>
      <td>&lt;input type="text" name="pw" size="16" value="&lt;?= $pw ?&gt;"&gt;&lt;/td>
    </tr>
    <tr>
      <td>Plaintext:</td>
      <td>&lt;input type="text" name="pt" size="300" value="&lt;?= $pt ?&gt;"&gt;&lt;/td>
    </tr>
    <tr>
      <td>&lt;input type="submit" name="encr" value="Encrypt it:"&gt;&lt;/td>
      <td>&lt;input type="text" name="cipher" size="300" value="&lt;?= $encr ?&gt;"&gt;&lt;/td>
    </tr>
    <tr>
      <td>&lt;input type="submit" name="decr" value="Decrypt it:"&gt;&lt;/td>
      <td>&lt;input type="text" name="plain" size="300" value="&lt;?= $decr ?&gt;"&gt;&lt;/td>
    </tr>
  </table>
&lt;/form&gt;
&lt;input type="button" value="enc client side" onclick="[removed] encry()"/&lt;

&lt;script src="aes.js"&gt;&lt;/script&gt;
   &lt;script&gt;
   function encrypt_on_client(plaintext, password, nBits){
   var aa = AESEncryptCtr(plaintext, password, nBits);
   document.frm.cipher.value = aa;
   }
   function decrypt_on_server(){
   document.frm.submit();
   }
&lt;/script&gt;
&lt;!--- use for copy and paste encrypted text, replace the doc with document ----&gt;
&lt;script&gt;doc.write('this is client side (copy to above): ' + AESEncryptCtr(document.getElementsByName('pt')[0].value, document.getElementsByName('pw')[0].value, 256) + '&lt;br&gt;');&lt;/script&gt;
&lt;!--- use for copy and paste encrypted text ----&gt;
&lt;?= 'this is server side (copy to above) : ' . $server_side . '<br>' ?&gt;
</p>

&lt;/body&gt;
&lt;/html&gt;
#5

[eluser]freezy[/eluser]
Quote:Some general comment:
Why do you want to encrypt the client <-> server communication using AES and javascript? Why don’t you just use SSL?

Quote:That’s what i’d do personally
yeah you right guys, absolutely aggree. we can use SSL to implement secure connection. can't tell you guys why i can't use SSL, i'm sorry , thats G** D*** too embarrassin to tell here.

Quote:The forum software did strip out some javascript, so the source code is not complete:
Quote:[removed]

the removed script doesn't have special meaning, just a simple script (e.g import the js).
this is the non CI class code
Code:
&lt;?php

     //php AES implementation
?&gt;
&lt;!-- test harness... --&gt;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;title&gt;AES in PHP test harness&lt;/title&gt;
&lt;/head&gt;
&lt;script src="aes.js"&gt;&lt;/script&gt;
&lt;script&gt;
   function encrypt_on_client(plaintext, password, nBits){
      var aa = AESEncryptCtr(plaintext, password, nBits);
      document.frm.cipher.value = aa;
   }
   function decrypt_on_server(){
      document.frm.submit();
   }
&lt;/script&gt;

&lt;body&gt;
&lt;? $pw = isset($_POST['pw']) ? stripslashes($_POST['pw']) : "L0ck it up saf3";
   $pt = isset($_POST['pt']) ? stripslashes($_POST['pt']) : "pssst ... đon't tell anyøne!";
   $encr = isset($_POST['encr']) ? $_POST['cipher'] : '';
   $decr = isset($_POST['decr']) ? AESDecryptCtr($_POST['cipher'], $pw, 256) : '';
?&gt;  

&lt;form name="frm" id="frm" method="post" action="./aes.php"&gt; &lt;!-- same-document reference --&gt;
  <table>  
    <tr>
      <td>Password:</td>
      <td>&lt;input type="text" name="pw" size="16" value="&lt;?= $pw ?&gt;"&gt;&lt;/td>
    </tr>
    <tr>
      <td>Plaintext:</td>
      <td>&lt;input type="text" name="pt" size="40" value="&lt;?= $pt ?&gt;"&gt;&lt;/td>
    </tr>
    <tr>
      <td></td>
      <td>&lt;input type="text" name="cipher" size="80" value="&lt;?=$encr?&gt;"&gt;&lt;/td>
    </tr>
    <tr>
      <td></td>
      <td>&lt;input type="text" size="40" value="&lt;?= $decr ?&gt;"&gt;&lt;/td>
    </tr>
  </table>
  &lt;input type="button" name="encr" value="Encrypt it:" onclick="encrypt_on_client(document.frm.pt.value, document.frm.pw.value, 256)"&lt;
  &lt;input type="submit" name="decr" value="Decrypt it:"&gt;
  &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
this is the CI View
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;title&gt;AES in PHP test harness&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;script src="&lt;?=site_url('AES.js')?&gt;"&gt;&lt;/script&gt;
&lt;form name="frm" id="frm" method="post" action=""&gt;
  <table>  
    <tr>
      <td>Password:</td>
      <td>&lt;input type="text" name="pw" size="16" value="&lt;?= $pw ?&gt;"&gt;&lt;/td>
    </tr>
    <tr>
      <td>Plaintext:</td>
      <td>&lt;input type="text" name="pt" size="300" value="&lt;?= $pt ?&gt;"&gt;&lt;/td>
    </tr>
    <tr>
      <td>&lt;input type="submit" name="encr" value="Encrypt it:"&gt;&lt;/td>
      <td>&lt;input type="text" name="cipher" size="300" value="&lt;?= $encr ?&gt;"&gt;&lt;/td>
    </tr>
    <tr>
      <td>&lt;input type="submit" name="decr" value="Decrypt it:"&gt;&lt;/td>
      <td>&lt;input type="text" name="plain" size="300" value="&lt;?= $decr ?&gt;"&gt;&lt;/td>
    </tr>
  </table>
&lt;/form&gt;
&lt;input type="button" value="enc client side" onclick="[removed] encry()"/&lt;

&lt;script src="aes.js"&gt;&lt;/script&gt;
   &lt;script&gt;
   function encrypt_on_client(plaintext, password, nBits){
   var aa = AESEncryptCtr(plaintext, password, nBits);
   document.frm.cipher.value = aa;
   }
   function decrypt_on_server(){
   document.frm.submit();
   }
&lt;/script&gt;
&lt;!--- use for copy and paste encrypted text, replace the doc with document ----&gt;
&lt;script&gt;doc.write('this is client side (copy to above): ' + AESEncryptCtr(document.getElementsByName('pt')[0].value, document.getElementsByName('pw')[0].value, 256) + '&lt;br&gt;');&lt;/script&gt;
&lt;!--- use for copy and paste encrypted text ----&gt;
&lt;?= 'this is server side (copy to above) : ' . $server_side . '<br>' ?&gt;
</p>

&lt;/body&gt;
&lt;/html&gt;

Quote:
Quote:The page load well, but looks like the encrypt and the decrypt process doesn’t.
Do you get any error messages? What did you expect, what was the result?
no error message at all, but i can't decrypt the encrypted text. i copy paste the encrypted text to the non CI (this work on the NON CI it self, and i know i can't expect better result), and do decrypt, looks like not work well neither.




Theme © iAndrew 2016 - Forum software by © MyBB