This is one of my earliest python projects.
Made while still learning python, ported this over from an old Delphi project of mine.
For stronger encryption, and to encrypt files as well as strings try one of my newer ciphers like Rubik’s Crypt, however this should be a great choice for a beginner python programmer, as it’s short, and I haven’t used too many advanced features.
The GUI was made in glade, but you won’t need it if you’re okay with using it as a command line tool, or directly from python or IDLE.
SS.py
class SafeSide2:
def characterset(self,start):
cset=[]
for i in range(126-start):
place = abs(round(( (i-1) *3.6 +i ))) % (len(cset) +1)
cset.insert( place , chr( i + start ) )
return cset
def strtocsetind(self,string,cset):
outp=[]
for i in range( len(string) ):
outp.insert(i,cset.index(string[i]))
return outp
def cropstr(self,string,cset):
outp=''
for i in range( len(string )):
if (string[i] in cset) == True:
outp= outp+string[i]
return outp
def csettostrind(self,string,cset):
outp=''
for i in range(len(string)):
outp= outp+ cset[string[i]]
return outp
def Keyset(self,key,cset):
Set=[]
for i1 in range(len(key)):
for i2 in range(len(key)):
Set.insert(i1*len(key)+i2,(key[i1]+key[i2]+((i1*7)%45) -((i1*6)%8) +i1+i2)%len(cset))
return Set
def encryptstr(self,key,string):
cset= self.characterset(32)
Key = self.strtocsetind(self.cropstr(key,cset),cset)
Extendedkey= self.Keyset(Key,cset)
plaintext= self.strtocsetind(self.cropstr(string,cset),cset)
ciphertext=[]
for i in range(len(plaintext)):
ciphertext.insert(i,(plaintext[i]+i+Extendedkey[i%len(Extendedkey)])%len(cset))
return self.csettostrind(ciphertext,cset)
def decryptstr(self,key,string):
cset= self.characterset(32)
Key = self.strtocsetind(self.cropstr(key,cset),cset)
Extendedkey= self.Keyset(Key,cset)
plaintext= self.strtocsetind(self.cropstr(string,cset),cset)
ciphertext=[]
for i in range(len(plaintext)):
ciphertext.insert(i,(plaintext[i]-i-Extendedkey[i%len(Extendedkey)])%len(cset))
return self.csettostrind(ciphertext,cset)
Everything below here is just an optional add-on, the above tool is all you need, but I’ve also made two user friendly interfaces, one for command-line usage the other for graphical usage (gtk via glade).
Command Line Interface (SS_interface.py)
from SS import SafeSide2
mykey= raw_input('Please Insert Your Key: \n --> ')
exiting= False
while exiting == False:
A=''
while (((A =='E') or (A =='e') or (A =='D') or (A =='d') or (A =='Q') or (A =='q')) != True):
A= raw_input('Encrypt Decrypt or Quit? (E/D/Q) :\n --> ')
if ((A == 'E') or (A == 'e')):
PT= raw_input('Please insert PlainText: \n --> ')
out= SafeSide2().encryptstr(mykey,PT)
print out
if ((A == 'D') or (A=='d')):
CT= raw_input('Please insert CypherText: \n --> ')
out = SafeSide2().decryptstr(mykey,CT)
print out
if ((A == 'Q') or (A=='q')):
exiting =True
The graphical user interface (SSgui.py) requires the glade file
from SS import SafeSide2
import sys
try:
import pygtk
pygtk.require("2.0")
except:
print 'error'
pass
try:
import gtk
import gtk.glade
except:
print("GTK Not Availible")
sys.exit(1)
class SSgui:
wTree = None
def __init__( self ):
self.wTree = gtk.glade.XML("SSGui.Glade")
dic = {
"on_Encrypt_button_press_event" : self.encrypt,
"on_Decrypt_button_press_event" : self.decrypt,
"on_windowMain_destroy" : self.quit,
}
self.wTree.signal_autoconnect( dic )
gtk.main()
def encrypt(self, widget,wtf):
try:
GUI_string= self.wTree.get_widget("entry2").get_text()
self.wTree.get_widget("output").set_text(SafeSide2().encryptstr(self.wTree.get_widget("keybox").get_text(),GUI_string))
except ValueError:
self.wTree.get_widget("hboxWarning").show()
return 0
def decrypt(self, widget,wtf):
try:
GUI_string= self.wTree.get_widget("entry1").get_text()
self.wTree.get_widget("output").set_text(SafeSide2().decryptstr(self.wTree.get_widget("keybox").get_text(),GUI_string))
except ValueError:
self.wTree.get_widget("hboxWarning").show()
return 0
def quit(self, widget):
sys.exit(0)
SSgui()
Here’s the glade XML (SSGui.Glade)
<?xml version="1.0" encoding="UTF-8"?>
<glade-interface>
<!-- interface-requires gtk+ 2.16 -->
<!-- interface-naming-policy project-wide -->
<widget class="GtkWindow" id="windowMain">
<property name="visible">True</property>
<property name="default_width">440</property>
<property name="default_height">250</property>
<signal name="destroy_event" handler="on_windowMain_destroy"/>
<child>
<widget class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<child>
<widget class="GtkHBox" id="hbox3">
<property name="visible">True</property>
<child>
<widget class="GtkVBox" id="vbox2">
<property name="visible">True</property>
<child>
<widget class="GtkLabel" id="label2">
<property name="visible">True</property>
<property name="label" translatable="yes">Encryption</property>
</widget>
<packing>
<property name="position">0</property>
</packing>
</child>
<child>
<widget class="GtkEntry" id="entry2">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">•</property>
</widget>
<packing>
<property name="position">1</property>
</packing>
</child>
</widget>
<packing>
<property name="position">0</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="Encrypt">
<property name="label" translatable="yes">Encrypt Plaintext </property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="button_press_event" handler="on_Encrypt_button_press_event"/>
</widget>
<packing>
<property name="position">1</property>
</packing>
</child>
</widget>
<packing>
<property name="position">0</property>
</packing>
</child>
<child>
<widget class="GtkHBox" id="hbox2">
<property name="visible">True</property>
<child>
<widget class="GtkVBox" id="vbox3">
<property name="visible">True</property>
<child>
<widget class="GtkLabel" id="label3">
<property name="visible">True</property>
<property name="label" translatable="yes">Decryption</property>
</widget>
<packing>
<property name="position">0</property>
</packing>
</child>
<child>
<widget class="GtkEntry" id="entry1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">•</property>
</widget>
<packing>
<property name="position">1</property>
</packing>
</child>
</widget>
<packing>
<property name="position">0</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="Decrypt">
<property name="label" translatable="yes">Decrypt Cyphertext</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="button_press_event" handler="on_Decrypt_button_press_event"/>
</widget>
<packing>
<property name="position">1</property>
</packing>
</child>
</widget>
<packing>
<property name="position">1</property>
</packing>
</child>
<child>
<widget class="GtkHBox" id="hbox1">
<property name="visible">True</property>
<child>
<widget class="GtkVBox" id="vbox4">
<property name="visible">True</property>
<child>
<widget class="GtkEntry" id="output">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">•</property>
</widget>
<packing>
<property name="position">0</property>
</packing>
</child>
<child>
<widget class="GtkHBox" id="hbox4">
<property name="visible">True</property>
<child>
<widget class="GtkEntry" id="keybox">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">•</property>
</widget>
<packing>
<property name="position">0</property>
</packing>
</child>
<child>
<widget class="GtkLabel" id="Label4">
<property name="visible">True</property>
<property name="label" translatable="yes"><-- Key</property>
</widget>
<packing>
<property name="position">1</property>
</packing>
</child>
</widget>
<packing>
<property name="position">1</property>
</packing>
</child>
</widget>
<packing>
<property name="position">0</property>
</packing>
</child>
</widget>
<packing>
<property name="position">2</property>
</packing>
</child>
</widget>
</child>
</widget>
</glade-interface>
The full project is available here.
0