1

I'm learning Python 3 and i'm trying to make a Caesar Cypher Algorithm. I'm taking a string with commandline input and then I want to change the hexa bytes values (I mean add or substract an integer on it) and finally return the modified string.

stringToDecode = "L|k€y+*^*zo‚*€kvsno|*k€om*vo*zk}}*cyvksr"
for i in range(256):
  print('%i: %s' % (i, repr(''.join([chr((ord(c)+i)%256) for c in stringToDecode]))))

Here is what i managed to do atm but it doesn't works really well. Thx a lot for reading! :)

5
  • 1
    Good lord that is terrible formatting. Please don't make Python look like Perl!! Commented Jan 21, 2014 at 0:28
  • Well, sry, Python is kinda new for me, I've always used C/C++/C# before. So feel free to give me some advices if my formatting suck so much ^^. Commented Jan 21, 2014 at 0:31
  • I'm a bit confused on your question. You want to increment the ord() value of each character by the same amount in order to implement a Caesar Cypher, right? Commented Jan 21, 2014 at 0:43
  • That's it! But with my code it doesn't works fine. Commented Jan 21, 2014 at 0:56
  • With my code the result of line 246 is: 246: 'Bra¢o! Tu peu\x10 ¢alider a¢ec le pass Yolaihu\n' . But if I replace the first line by : stringToDecode ='\x4c\x7c\x6b\x80\x79\x2b\x2a\x5e\x7f\x2a\x7a\x6f\x7f\x82\x2a\x80\x6b\x76\x73\x6e\x6f\x7c\x2a\x6b\x80\x6f\x6d\x2a\x76\x6f\x2a\x7a\x6b\x7d\x7d\x2a\x63\x79\x76\x6b\x73\x72\x7f\x14\x0a' (Whitch is exactly the same but in hexa) Then the result is : 246: 'Bravo! Tu peux valider avec le pass Yolaihu\n\x00' Whitch is exactly what i want. Commented Jan 21, 2014 at 1:15

1 Answer 1

3

This is a bit easier to read. Remember, you usually spend at least twice as long reading your code than writing it, so why not make it easier to read :).

stringToDecode = "L|k€y+*^*zo‚*€kvsno|*k€om*vo*zk}}*cyvksr"
for i in range(256):
    list_comp = [chr((ord(c)+i)%256) for c in stringToDecode]
    joined_list = ''.join(list_comp)
    new_val = repr(joined_list) # you shouldn't need this
    print('%i: %s' % (i, new_val))
Sign up to request clarification or add additional context in comments.

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.