Print-View

Nz17 Productions (Thursday, 19 October 2017 10:27:27 +0000)
http://www.nz17.com/forum/

Width too high for your page to be printed? You be able to limit the maximum width to 500 pixes, which is enough for older browsers. You can enter also enter another value in the URL or delete the limitation.


 http://www.nz17.com/forum/board_topic.php?tid=682
 nz17 (Monday, 26 April 2010 14:56:16 +0000) to topic Message Cipher Source Code
I hope that some of you find this code which I wrote for my message encoder/decoder for our "Secret Cypher Announcement" useful. The forum ate all the whitespace characters for this, so if you want to run the program please download the file attached to this forum post. (Or use a program smart enough to auto-indent the source code.)

#!/usr/bin/env python
####
# Copyright 2010 Nz17 Productions, Sean Robinson
#
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
####

# Encode a message by reversing the alphabet (a=>z, b=>y, etc.)
def reverseAlphabet(message):
cyphered = ""

for v in message:
if ord(v) >= ord('a') and ord(v) <= ord('z'):
cyphered += chr(ord(v) + 25 - (ord(v) - ord('a')) * 2)
elif ord(v) >= ord('A') and ord(v) <= ord('Z'):
cyphered += chr(ord(v) + 25 - (ord(v) - ord('A')) * 2)
else:
cyphered += v

return cyphered

# Encode a message by stepping each letter back by one (a=>z, b=>a, etc.)
def oneStepBack(message):
cyphered = ""

for v in message:
if ord(v) > ord('a') and ord(v) <= ord('z'):
cyphered += chr(ord(v) - 1)
elif ord(v) == ord('a'):
cyphered += 'z'
elif ord(v) > ord('A') and ord(v) <= ord('Z'):
cyphered += chr(ord(v) - 1)
elif ord(v) == ord('A'):
cyphered += 'Z'
else:
cyphered += v

return cyphered

# Encode a message by stepping each letter forward by one (a=>b, b=>c, etc.)
def oneStepForward(message):
cyphered = ""

for v in message:
if ord(v) >= ord('a') and ord(v) < ord('z'):
cyphered += chr(ord(v) + 1)
elif ord(v) == ord('z'):
cyphered += 'a'
elif ord(v) >= ord('A') and ord(v) < ord('Z'):
cyphered += chr(ord(v) + 1)
elif ord(v) == ord('Z'):
cyphered += 'A'
else:
cyphered += v

return cyphered

# Main section
runAgain = 'y';

while runAgain == 'y':
message = raw_input("Please input message to encypher/decypher: ")
print

print "[Available cyphers]"
print "1) Reverse Alphabet"
print "2) One Step Back"
print "3) One Step Forward"
print
cypherMethod = raw_input("Please select cypher to use: ")

print
print message
if cypherMethod == '1':
print reverseAlphabet(message)
elif cypherMethod == '2':
print oneStepBack(message)
elif cypherMethod == '3':
print oneStepForward(message)
print

runAgain = raw_input("Convert another message (y/n)? ")
print