4

I have a string that I would like to encrypt in Python, store it as a cookie, then in a PHP file I'd like to retrieve that cookie, and decrypt it in PHP. How would I go about doing this?


I appreciate the fast responses.

All cookie talk aside, lets just say I want to encrypt a string in Python and then decrypt a string in PHP.

Are there any examples you can point me to?

2
  • are you sure you want to "encrypt" a script? may be you're talking about the hashing? Commented Jan 21, 2010 at 19:41
  • @SilentGhost sounds like he wants to encrypt a cookie :-) Commented Jan 21, 2010 at 19:42

5 Answers 5

7

Use a standard encryption scheme. The implementation is going to be equivalent in either language.

RSA is available (via third party libraries) in both languages, if you need asymmetric key crypto. So is AES, if you need symmetric keys.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 AES seems to be supported by both Python and PHP although it does does require external libraries in both.
For historical and legal reasons alot of crypto stuff isn't in the standard language libraries. Regardless, its readily available.
5

There is a good example here:
http://www.codekoala.com/blog/2009/aes-encryption-python-using-pycrypto/

Other links that may help:
http://www.phpclasses.org/browse/package/4238.html
http://www.chilkatsoft.com/p/php_aes.asp

Comments

1

If you're not talking about encryption but encoding to make sure the contents make it through safely regardless of quoting issues, special characters, and line breaks, I think base64 encoding is your best bet. PHP has base64_encode / decode() out of the box, and I'm sure Python has, too.

Note that base64 encoding obviously does nothing to encrypt your data (i.e. to make it unreadable to outsiders), and base64 encoded data grows by 33%.

Comments

0

Well, my first thought would be to use a web server that uses SSL and set the cookie's secure property to true, meaning that it will only be served over SSL connections.

However, I'm aware that this probably isn't what you're looking for.

Comments

0

Although a bit late. Find sample code below using the Fernet library

#Python Code - fernet 1.0 library
from cryptography.fernet import Fernet
key =  b"Gm3wFh9OiQHcVc8rcAMm8IOqKOJtk7CbrGRKVhrvXhg="
f = Fernet(key)
token = f.encrypt(b'the quick brown fox jumps over the lazy hare')
print(token)

##gAAAAABiMWVPsStLo42ExcmIqcGvRvCCmnhB5B6dc2JsOm4w-VsE9oJOuk_qYuZvHv5quQR4t_6ZjNJzAdCiDPOtESNzCreJZLwc2X-_apbqKKnBwc3KhmqL-K5X7t1uR1WXuyUEYUtW



<?php
    //PHP - kelvinmo/fernet-php v1.0.1 A
    require 'vendor/autoload.php';
    use Fernet\Fernet;
    $key = "Gm3wFh9OiQHcVc8rcAMm8IOqKOJtk7CbrGRKVhrvXhg=" ;
    $fernet = new Fernet($key);
    $token = "gAAAAABiMWVPsStLo42ExcmIqcGvRvCCmnhB5B6dc2JsOm4w-VsE9oJOuk_qYuZvHv5quQR4t_6ZjNJzAdCiDPOtESNzCreJZLwc2X-_apbqKKnBwc3KhmqL-K5X7t1uR1WXuyUEYUtW";
    echo $fernet->decode($token);

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.