Caesar Cipher

This program lets you write a short line and encrypt it using Caesar’s Cipher. It shows the two prior and next rotations, as well as the current encryption key.

Usage

  • Normal letters let you type text
  • Backspace lets you delete the last letter
  • Tab sets the encryption key to zero
  • Escape toggles if key and other rotations are shown
  • Return toggles if special characters (characters, that are not contained in the standard alphabet) are displayed
  • Up arrow rotates up (encryption key decreases)
  • Down arrow rotates down (encryption key increases)

Hello World! Kog hvs accb zobrwbu toys?  dbeeubee


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 7th of August, 2015
#         edited 8th of August, 2015

# importing needed modules
import pygame, sys, time, math, os, random
pygame.font.init()

""" CLASSES """
# dummy class for global variables
class dummy():
	pass

""" FUNCTIONS """
# quits the program
def quit():
	sys.exit()

# cycle (or rotate) given text (ceaser method)
def cycle(_text, _times):
	text = ""
	for _ in _text:
		if _ in main.EXTRA:
			text += _
		else:
			if _.isupper():
				text += main.LETTERS[(main.LETTERS.index(_.lower()) + _times)%len(main.LETTERS)].upper()
			else:
				text += main.LETTERS[(main.LETTERS.index(_) + _times)%len(main.LETTERS)]
	return text

# changes text to output
# (so that original text does not get changed)
def out(_text):
	if main.CLEAR:
		text = ""
		for _ in _text:
			if _ not in main.EXTRA:
				text += _.lower()
		return text
	
	return _text

""" TICK; RENDER """
# tick function
def tick():
	# handle events
	for event in pygame.event.get():
		# quit
		if event.type == pygame.QUIT:
			quit()
		
		# keydown
		if event.type == pygame.KEYDOWN:
			
			# tab sets key to zero
			if event.key == pygame.K_TAB:
				main.TEXT = cycle(main.TEXT, -main.KEY)
				main.KEY = 0
			
			# escape toggles preview (showing key and near rotaions)
			elif event.key == pygame.K_ESCAPE:
				main.PREVIEW = not main.PREVIEW
				
			# return toggles clear (clears every non-abc letter and lowers them)
			elif event.key == pygame.K_RETURN:
				main.CLEAR = not main.CLEAR
			
			# cycle down and up
			elif event.key == pygame.K_DOWN:
				main.TEXT = cycle(main.TEXT, 1)
				main.KEY += 1
			elif event.key == pygame.K_UP:
				main.TEXT = cycle(main.TEXT, -1)
				main.KEY -= 1
			
			# backspace clears the last letter
			elif event.key == pygame.K_BACKSPACE:
				if len(main.TEXT) > 0:
					main.TEXT = main.TEXT[0:-1]
			
			# text input
			else:
				l = event.unicode
				if l.lower() in main.LETTERS or l.lower() in main.EXTRA:
					main.TEXT += l

# render function
def render():
	# fill
	main.SURF.fill([0, 0, 0])
	
	# get text dimensions
	w, h = main.FONT.size("")
	
	# render text preview I
	if main.PREVIEW:
		text = main.FONT.render(out(cycle(main.TEXT, -2)), 1, [55, 55, 55])
		rect = text.get_rect()
		pos = [(main.WIDTH-rect.right)/2, (main.HEIGHT-rect.bottom)/2-h-h]
		main.SURF.blit(text, pos)
		
		text = main.FONT.render(out(cycle(main.TEXT, -1)), 1, [155, 155, 155])
		rect = text.get_rect()
		pos = [(main.WIDTH-rect.right)/2, (main.HEIGHT-rect.bottom)/2-h]
		main.SURF.blit(text, pos)
	
	# render actual text
	text = main.FONT.render(out(main.TEXT), 1, [255, 255, 255])
	rect = text.get_rect()
	pos = [(main.WIDTH-rect.right)/2, (main.HEIGHT-rect.bottom)/2]
	main.SURF.blit(text, pos)
	
	# render text preview II
	if main.PREVIEW:
		text = main.FONT.render(out(cycle(main.TEXT, 1)), 1, [155, 155, 155])
		rect = text.get_rect()
		pos = [(main.WIDTH-rect.right)/2, (main.HEIGHT-rect.bottom)/2+h]
		main.SURF.blit(text, pos)
		
		text = main.FONT.render(out(cycle(main.TEXT, 2)), 1, [55, 55, 55])
		rect = text.get_rect()
		pos = [(main.WIDTH-rect.right)/2, (main.HEIGHT-rect.bottom)/2+h+h]
		main.SURF.blit(text, pos)
	
	# render key
	if main.PREVIEW:
		text = main.FONTSMALL.render("key = " + str(main.KEY%len(main.LETTERS)), 1, [255, 0, 0])
		rect = text.get_rect()
		pos = [50, main.HEIGHT - rect.bottom - 50]
		main.SURF.blit(text, pos)
	
	# blit and flip
	main.SCREEN.blit(main.SURF, [0, 0])
	pygame.display.flip()

""" INIT """
# initialize program
def init():
	main.WIDTH, main.HEIGHT = 1080, 720
	main.SIZE = [main.WIDTH, main.HEIGHT]
	main.SCREEN = pygame.display.set_mode(main.SIZE)
	main.SURF = pygame.Surface(main.SIZE)
	
	main.CAPTION = "Caesar Cipher"
	main.TICKS = 0
	main.KEYSDOWN = []
	
	# font
	main.FONT = pygame.font.SysFont(None, 100)
	main.FONTSMALL = pygame.font.SysFont(None, 50)
	
	# text
	main.TEXT = ""
	main.EXTRA = " ,.;-+*#!%&/()[]{}?'" + '"'
	main.LETTERS = []
	for _ in "abcdefghijklmnopqrstuvwxyz":
		main.LETTERS.append(_)
	
	# other
	main.KEY = 0
	main.CLEAR = False
	main.PREVIEW = True
	
	# functions
	pygame.display.set_caption(main.CAPTION)

""" RUN """
# run function (uses tick() and render())
def run():
	ticksPerSecond = 60
	lastTime = time.time() * 1000000000
	nsPerTick =  1000000000.0 / float(ticksPerSecond)
	
	ticks = 0
	frames = 0
	
	lastTimer = time.time() * 1000
	delta = 0.0
	
	while True:
		now = time.time() * 1000000000
		delta += float(now - lastTime) / float(nsPerTick)
		lastTime = now
		shouldRender = False
				
		while delta >= 1:
			ticks += 1
			main.TICKS += 1
			tick()
			delta -= 1
			shouldRender = True
		
		if shouldRender:
			frames += 1
			render()
		
		if time.time() * 1000 - lastTimer >= 1000:
			lastTimer += 1000
			
			# debug
			# print("Frames: " + str(frames) + ", ticks: " + str(ticks))
			
			frames = 0
			ticks = 0

# main variable
main = dummy()
init()

# start program
run()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s