1 Replies - 22530 Views - Last Post: 24 November 2006 - 08:59 AM Rate Topic: ***-- 2 Votes

#1 maximusthegladiator2006  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-October 06

Caesar Cipher python code!

Posted 24 October 2006 - 11:14 AM

encipher( S, n ) --- >
encipher('xyza', 1)
'yzab'
and
decipher( S )
decipher('Bzdrzq bhogdq? H oqdedq Bzdrzq rzkzc.')
'Caesar cipher? I prefer Caesar salad.'

I have to write a program on the above given examples.. and it can be anything.. if the text is entered in upprecase it shold give output in uppercase and same for lowercase.. it shouldn't do anything if number or special characters are used..

I used built in functions ORD and CHR.. I don't know I am unable to establish the connection as I need a helper function..

I would be appreciative if some one can help me.. Thank you :)

Is This A Good Question/Topic? 0
  • +

Replies To: Caesar Cipher python code!

#2 v0id  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 4
  • Joined: 22-November 06

Re: Caesar Cipher python code!

Posted 24 November 2006 - 08:59 AM

View Postmaximusthegladiator2006, on 24 Oct, 2006 - 11:14 AM, said:

encipher( S, n ) --- >
encipher('xyza', 1)
'yzab'
and
decipher( S )
decipher('Bzdrzq bhogdq? H oqdedq Bzdrzq rzkzc.')
'Caesar cipher? I prefer Caesar salad.'

I have to write a program on the above given examples.. and it can be anything.. if the text is entered in upprecase it shold give output in uppercase and same for lowercase.. it shouldn't do anything if number or special characters are used..

I used built in functions ORD and CHR.. I don't know I am unable to establish the connection as I need a helper function..

I would be appreciative if some one can help me.. Thank you :)


I don't know exactly what you what, is it just some function to convert to caesarciphers and back again?
If it's, I've wrote two simple functions:
encipher(S [, n (= 3)])
decipher(S [, n (= 3)])
They've the default n as three, 'cus that's the original number, Caesar used.

small = "abcdefghijklmnopqrstuvwxyz"
big   = small.upper()
size  = len(big)-1

def encipher(S, n = 3):
	finale_str = ''
	for c in S:
		if c.islower():
			if (small.find(c)+n)>size:
				c = small[(small.find(c)+n)-(size+1)]
			else:
				c = small[small.find(c)+n]
		elif c.isupper():
			if (big.find(c)+n)>size:
				c = big[(big.find(c)+n)-(size+1)]
			else:
				c = big[big.find(c)+n]
		finale_str += c
	return finale_str

def decipher(S, n = 3):
	finale_str = ''
	for c in S:
		if c.islower():
			if (small.find(c)-n)<0:
				c = small[size-small.find(c)-(n-1)]
			else:
				c = small[small.find(c)-n]
		elif c.isupper():
			if (big.find(c)-n)<0:
				c = big[size-big.find(c)-(n-1)]
			else:
				c = big[big.find(c)-n]
		finale_str += c
	return finale_str

# Encipher examples...
print encipher("abcdefghijklmnopqrstuvwxyz", 1)
print encipher("abcdefghijklmnopqrstuvwxyz", 2)
print encipher("abcdefghijklmnopqrstuvwxyz")

print encipher("v0id is me!", 1)
print encipher("v0id is me!", 2)
print encipher("v0id is me!")

print "\n"

# Decipher examples...
print decipher("x0kf ku og!", 1)
print decipher("x0kf ku og!", 2)
print decipher("x0kf ku og!")

print "\n"

# Both...
string = "Dream.In.Code!"
print string
print encipher(string)
print decipher(string)
print decipher(encipher(string))
print encipher(decipher(string))


Was This Post Helpful? 1

Page 1 of 1