JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 27th, 2000, 03:05 AM
amodi amodi is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2000
Location: Gorakhpur,U.P.,India
Posts: 24 amodi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Friends,

I would like to encrypt (with a private key) a string on the client side using JavaScript and decrypt the same on the server side using PHP (the private key can be generated by the server). Can any one help me in doing this?

Thanks to u all in advance..



------------------
Anish Modi
Infotech World
India

Reply With Quote
  #2  
Old February 25th, 2008, 02:57 PM
m7x3r m7x3r is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 4 m7x3r User rank is Lance Corporal (50 - 100 Reputation Level)m7x3r User rank is Lance Corporal (50 - 100 Reputation Level)m7x3r User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 40 m 38 sec
Reputation Power: 0
Same Issue

I need to encrypt a variable in javascript then send the returned encrypted msg to the server then decrypt the sent enc_msg using PHP and should obtain the original variable.

I am thinking of creating a random key in PHP setting it to a hidden text field value. Then read that hidden text field value which contains the PHP's random key then have Javascript read that from the hidden field use that to encrypt the message then when it's submitted, PHP should decrypt the message using the same randomly gen. key sent to the client originally.

Any help is greatly appreciated,

Reply With Quote
  #3  
Old February 25th, 2008, 03:29 PM
bigSeth's Avatar
bigSeth bigSeth is offline
simpleton
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: denver.colorado.usa
Posts: 392 bigSeth User rank is Captain (20000 - 30000 Reputation Level)bigSeth User rank is Captain (20000 - 30000 Reputation Level)bigSeth User rank is Captain (20000 - 30000 Reputation Level)bigSeth User rank is Captain (20000 - 30000 Reputation Level)bigSeth User rank is Captain (20000 - 30000 Reputation Level)bigSeth User rank is Captain (20000 - 30000 Reputation Level)bigSeth User rank is Captain (20000 - 30000 Reputation Level)bigSeth User rank is Captain (20000 - 30000 Reputation Level)bigSeth User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 13 h 22 m 25 sec
Reputation Power: 226
Send a message via AIM to bigSeth
I would not advise using hidden inputs for you key, as they can and will be easily viewed/manipulated.
__________________
10% gifted 90% puzzled

Reply With Quote
  #4  
Old February 25th, 2008, 04:59 PM
execute's Avatar
execute execute is offline
CSKA Sofia
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2003
Location: Germany / Bulgaria
Posts: 565 execute User rank is First Lieutenant (10000 - 20000 Reputation Level)execute User rank is First Lieutenant (10000 - 20000 Reputation Level)execute User rank is First Lieutenant (10000 - 20000 Reputation Level)execute User rank is First Lieutenant (10000 - 20000 Reputation Level)execute User rank is First Lieutenant (10000 - 20000 Reputation Level)execute User rank is First Lieutenant (10000 - 20000 Reputation Level)execute User rank is First Lieutenant (10000 - 20000 Reputation Level)execute User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 7 h 36 m 16 sec
Reputation Power: 118
Send a message via ICQ to execute
RSA in Javascript
AES in Javascript
DES in Javascript
Comments on this post
lnxgeek agrees: ..for the links
__________________
Nikola Ivanov
http://weboholic.de

Reply With Quote
  #5  
Old February 25th, 2008, 06:50 PM
lnxgeek lnxgeek is offline
I AM A GOLDEN GOD
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Apr 2003
Location: Ventura, California
Posts: 5,885 lnxgeek User rank is General 1st Grade (Above 100000 Reputation Level)lnxgeek User rank is General 1st Grade (Above 100000 Reputation Level)lnxgeek User rank is General 1st Grade (Above 100000 Reputation Level)lnxgeek User rank is General 1st Grade (Above 100000 Reputation Level)lnxgeek User rank is General 1st Grade (Above 100000 Reputation Level)lnxgeek User rank is General 1st Grade (Above 100000 Reputation Level)lnxgeek User rank is General 1st Grade (Above 100000 Reputation Level)lnxgeek User rank is General 1st Grade (Above 100000 Reputation Level)lnxgeek User rank is General 1st Grade (Above 100000 Reputation Level)lnxgeek User rank is General 1st Grade (Above 100000 Reputation Level)lnxgeek User rank is General 1st Grade (Above 100000 Reputation Level)lnxgeek User rank is General 1st Grade (Above 100000 Reputation Level)lnxgeek User rank is General 1st Grade (Above 100000 Reputation Level)lnxgeek User rank is General 1st Grade (Above 100000 Reputation Level)lnxgeek User rank is General 1st Grade (Above 100000 Reputation Level)lnxgeek User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 Days 21 h 44 m 44 sec
Reputation Power: 1033
Holy resurrected posts, Batman!

@m7x3r>
If you want to encrypt in JS and decrypt in PHP, you'll have to start by finding a matching algorithm between the two. Take a look at PHP's mcrypt functions and find a compatible JS algorithm. I personally looked at using the AES script execute posted at one time; it uses the CTR (counter) method, and as luck would have it, PHP's mcrypt extension can utilize CTR and rijndael-N (AES) encryption. I never did complete the project though.

Of the three posted, DES might be the easiest to make work, although not the most secure. Security is relative when you're talking about anything on the web anyway.
__________________
"Seriously, we're not a search engine, we're actual people." ~ ManiacDan

BookMooch.com : Give books away. Get books you want.

Reply With Quote
  #6  
Old February 25th, 2008, 10:21 PM
m7x3r m7x3r is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 4 m7x3r User rank is Lance Corporal (50 - 100 Reputation Level)m7x3r User rank is Lance Corporal (50 - 100 Reputation Level)m7x3r User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 40 m 38 sec
Reputation Power: 0
still searching...

Quote:
Originally Posted by lnxgeek
Holy resurrected posts, Batman!

@m7x3r>
If you want to encrypt in JS and decrypt in PHP, you'll have to start by finding a matching algorithm between the two. Take a look at PHP's mcrypt functions and find a compatible JS algorithm. I personally looked at using the AES script execute posted at one time; it uses the CTR (counter) method, and as luck would have it, PHP's mcrypt extension can utilize CTR and rijndael-N (AES) encryption. I never did complete the project though.

Of the three posted, DES might be the easiest to make work, although not the most secure. Security is relative when you're talking about anything on the web anyway.




Thanks to all your comments.... As far as avoiding the hidden textfield to pass a $var(in PHP) to a var(in JS).. I searched such topic and it seems to be 2 ways one is to pass the vars in the header upon submission meaning it will be in the url which could look like this http://myforume/mysite/mypage.php?var1=''&var2=''
something along those lines I am trying to avoid that and avoid using _GET[].... aside from this or writing and reading from a hidden html text field... I am unaware of any other way to pass php vars to js vars... if u know off any other... pls do... open to suggestions....

I yet to get my hands dirty with this ... but I have already encrypted using JS using RSA.. but I wanted to have another encrypter - wrapper so to speak to encrypt my original encrypted msg... this time Encrypt in JavaScript and Decrypt in PHP using the same key if not then pub. priv. keys.... I will take a look at mycrypt() php function thank you lnxgeek...

But you think it's safe to assume that if JS provides an implementation of some algorithm that PHP will follow the same login in implementing that encryption algorithm...aside from any native functions that may randomize a key or play with some vars here and there.....

Thanks again to all your support so far....this is the last mile for me...

Reply With Quote
  #7  
Old February 26th, 2008, 11:45 PM
m7x3r m7x3r is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 4 m7x3r User rank is Lance Corporal (50 - 100 Reputation Level)m7x3r User rank is Lance Corporal (50 - 100 Reputation Level)m7x3r User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 40 m 38 sec
Reputation Power: 0
almost done

Well as suggested by execute... thank you for providing the links

i found this
tero.co.uk/des to

be very helpful... all i really did is downloaded the javascript source to encrypt the encrypted msg on the client then passed the output of DES javascript to PHP in HEX ... this is important has to be in HEX on the client used JavaScript's stringToHex()... then PHP's DES implementation was close but I gave up on it so instead I opened php.ini and I uncommented "exension = mcrypt.....something .dll file" just take out the " ; " in the php.ini file enabling mcrypt.

then simply used mcrypt_decrypt() with the same key as JS used, and the converted msg from hex back to string this time based on des/PHP implementation [HexToString()] provided on the tero site... and i got back my RSA encrypted password to be stored in the dB.... however I got one last question if anyone can help me with :

mcrypt_decrypt() The IV parameter must be as long as the blocksize WARNING

not too sure on how to fix this bug or at least supress the warning... any suggestions are greatly valued...

thank you in advance...

Reply With Quote
  #8  
Old February 27th, 2008, 02:50 AM
execute's Avatar
execute execute is offline
CSKA Sofia
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2003
Location: Germany / Bulgaria
Posts: 565 execute User rank is First Lieutenant (10000 - 20000 Reputation Level)execute User rank is First Lieutenant (10000 - 20000 Reputation Level)execute User rank is First Lieutenant (10000 - 20000 Reputation Level)execute User rank is First Lieutenant (10000 - 20000 Reputation Level)execute User rank is First Lieutenant (10000 - 20000 Reputation Level)execute User rank is First Lieutenant (10000 - 20000 Reputation Level)execute User rank is First Lieutenant (10000 - 20000 Reputation Level)execute User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 7 h 36 m 16 sec
Reputation Power: 118
Send a message via ICQ to execute
Well, there're different modes in which an encryption/decryption with symmetric keys could be made, you have to see which mode the JS is using. ECB and CBC aren't commonly used, I would go with CFB.

More on the subject on Wikipedia and Google.

Reply With Quote
  #9  
Old February 28th, 2008, 05:05 PM
m7x3r m7x3r is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 4 m7x3r User rank is Lance Corporal (50 - 100 Reputation Level)m7x3r User rank is Lance Corporal (50 - 100 Reputation Level)m7x3r User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 40 m 38 sec
Reputation Power: 0
Mcrypt IV Warning

Quote:
Originally Posted by execute
Well, there're different modes in which an encryption/decryption with symmetric keys could be made, you have to see which mode the JS is using. ECB and CBC aren't commonly used, I would go with CFB.

More on the subject on Wikipedia and Google.


Firstly, many thanks to everyone that provided an insight or any useful tips that helped me accomplish this JavaScript DES Encrypt and PHP DES Encrypt... I have been able to find the solution to that IV Warning resulting from a PHP's Mcrypt library function, here is the code i used to decrypt using DES PHP's mcrypt library of the string value of a JavaScript encrypted msg in DES:

/* Open module, and create IV */
$key = '10';
$td = mcrypt_module_open('des', '', 'ecb','');
$key = substr($key, 0, mcrypt_enc_get_key_size($td));
$iv_size = mcrypt_enc_get_iv_size($td);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

/* Initialize encryption handle */
if (mcrypt_generic_init($td, $key, $iv) != -1)
{
//Iniitialize buffers for decryption
mcrypt_generic_init($td, $key, $iv);

// $DES_HexToString is the encrypted msg in string format
//converted from the Hex value that's given by the JS DES func
$p_t = mdecrypt_generic($td, $DES_HexToString);

//Clean Up
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
}




echo "<br>";
echo "MCrypt Method to decrypt Hex :";
echo $p_t;

Amazing heh...
Comments on this post
execute agrees: That's the spirit

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Encryption in JavaScript/Decryption in PHP


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT