Mac Python: Encrypt based on string


macrumors 6502
Original poster
Is there a way to encrypt/decrypt a string based on string 'x'? I would do:

theString = rawInput('Enter your message: ')
theKey = rawInput('Enter your password: ')
theEncryptedString = encrypt(theString, theKey)
print "Your encrypted string is %s" % (theEncryptedString)
Something like that. Any ideas?


macrumors member
Sep 9, 2008
It all depends on how secure you want it to be. You could simply do something like

Convert String1 to Integer1
Convert String2 to Integer2
Multiply Integer1*Integer2 = Integer3
Convert Integer3 to String3

String3 is your encrypted string and you reverse the process to get your original string back.


macrumors 68040
Feb 2, 2008
If this is just for fun, try to encrypt it based on xor with the advantage that the key can be used to get back to the original message. Google for a python version, here is one in C.

char *encrypt(char *theString, char *theKey) {

    size_t stringLength = strlen(theString);
    size_t keyLength = strlen(theKey);

    for(size_t i = 0; i < stringLength; i++) {
        theString[i] = theString[i] ^ theKey[ i % keyLength ];

    return theString;
If the key is the same length as the message, you have a one time pad btw. But still, for anything serious, use an appropriate library.
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.