For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > March 2006 > Converting Python code to PHP









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Converting Python code to PHP
ray-solomon

2006-03-15, 1:22 am

Below is just a snippet of my python code that I need for it to be translated to PHP. Anyone care to take a crack at it?



import string, random, zlib, base64

def Gen(key, text, reverse=False):
rand = random.Random(key).randrange
if not reverse:
text = zlib.compress(text)
text = ''.join([chr(ord(elem)^rand(256)) for elem in text])
if reverse:
text = zlib.decompress(text)
return text

if __name__ == "__main__":

def strToHex(aString):
hexlist = ["%02X " % ord(x) for x in aString]
return ''.join(hexlist)

keyString = "Some special key"
idString = "Hello"
print "String:", idString
idString = Gen(keyString, idString)
hexString = strToHex(idString)
b64String = base64.encodestring(hexString)
print "Encrypted string:", b64String
idString = Gen(keyString, idString, reverse=True)
print "Decrypted string:", idString
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com