class Window(QtGui.QMainWindow): def __init__(self): ''' Main window class. It's frameless and transparent. ''' super(Window, self).__init__(None, QtCore.Qt.FramelessWindowHint) QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('CleanLooks')) QtGui.QApplication.setPalette(QtGui.QApplication.style().standardPalette()) icon_path = os.path.split(os.path.abspath(__file__))[0] + '/config/icon.ico' self.setWindowIcon(QtGui.QIcon(icon_path)) self.resize(C['W_WIDTH'], C['W_HEIGHT']) self.setMaximumHeight(C['W_MAX_HEIGHT']) self.setStyleSheet(D['STYLE_MAIN']) self.setWindowTitle('Scrambled Egg %s :: Live Crypt' % __version__) self.setAttribute(QtCore.Qt.WA_TranslucentBackground) self.setWindowOpacity(0.9) self.setAcceptDrops(True) self.SE = ScrambledEgg() self.centralWidget = ContainerWidget(self) # Central Widget. self.setCentralWidget(self.centralWidget) self.container = QtGui.QWidget(self.centralWidget) # Container Widget. self.container.setObjectName('Container') self.container.setStyleSheet(D['STYLE_CONTAINER']) self.textBar = QtGui.QLabel(self) # Top text bar. self.layout = QtGui.QGridLayout(self.centralWidget) # Main Layout. self.centralWidget.setLayout(self.layout) self.leftText = QtGui.QTextEdit('', self.centralWidget) # To write clean text. self.rightText = QtGui.QPlainTextEdit('' , self.centralWidget) # To view encrypted text. self.buttonCryptMode = QtGui.QPushButton(self.centralWidget) self.buttonDecryptMode = QtGui.QPushButton(self.centralWidget) self.buttonBrowseRSAL = QtGui.QPushButton('Browse', self.centralWidget) self.buttonBrowseRSAR = QtGui.QPushButton('Browse', self.centralWidget) self.preProcess = QtGui.QComboBox(self.centralWidget) # Left side. self.comboCrypt = QtGui.QComboBox(self.centralWidget) # Left side. self.postProcess = QtGui.QComboBox(self.centralWidget) # Left side. self.linePasswordL = QtGui.QLineEdit(self.centralWidget) # Left password line. self.lineRSAPathL = QtGui.QLineEdit(self.centralWidget) # Left RSA Path line. self.checkPwdL = QtGui.QCheckBox('<- Pwd', self.centralWidget) # Left side. self.nrLettersL = QtGui.QLabel('', self.centralWidget) # Left side. self.setFormatting = QtGui.QCheckBox('Formatted text', self.centralWidget) # Left side. self.setTags = QtGui.QCheckBox('No tags', self.centralWidget) # Left side. self.showHTML = QtGui.QCheckBox('Show HTML', self.centralWidget) # Left side. self.preDecrypt = QtGui.QComboBox(self.centralWidget) # Right side. self.comboDecrypt = QtGui.QComboBox(self.centralWidget) # Right side. self.postDecrypt = QtGui.QComboBox(self.centralWidget) # Right side. self.linePasswordR = QtGui.QLineEdit(self.centralWidget) # Right password line. self.lineRSAPathR = QtGui.QLineEdit(self.centralWidget) # Right RSA Path line. self.checkPwdR = QtGui.QCheckBox('<- Pwd', self.centralWidget) # Right side. self.nrLettersR = QtGui.QLabel('', self.centralWidget) # Right side. self.loadFile = QtGui.QPushButton('Import', self.centralWidget) # Right side. self.saveFile = QtGui.QPushButton('Export', self.centralWidget) # Right side. self.helpButton = QtGui.QPushButton('Help !', self.centralWidget) # Right side. self.minButton = QtGui.QPushButton(D['MIN_BTN_TXT'], self.centralWidget) self.closeButton = QtGui.QPushButton(D['CLOSE_BTN_TXT'], self.centralWidget) self.micLayout = QtGui.QHBoxLayout() self.micLayout.addWidget(self.minButton) self.micLayout.addWidget(self.closeButton) # Row, Col, rowSpan, colSpan self.layout.addWidget(self.container, 0, 1, 43, 12) self.layout.addWidget(self.textBar, 1, 1, 3, 8) self.layout.addLayout(self.micLayout, 1, 10+C['MIC_BTNS_POS'], 1, C['MIC_BTNS_SPAN']) self.layout.addItem(QtGui.QSpacerItem(1, 8), 3, 1, 1, 1) self.layout.addWidget(self.buttonCryptMode, 4, 2, 1, 5) self.layout.addWidget(self.buttonDecryptMode, 4, 7, 1, 5) self.layout.addWidget(self.preProcess, 5, 2, 1, 1) self.layout.addWidget(self.comboCrypt, 5, 3, 1, 1) self.layout.addWidget(self.postProcess, 5, 4, 1, 1) self.layout.addWidget(self.preDecrypt, 5, 7, 1, 1) self.layout.addWidget(self.comboDecrypt, 5, 8, 1, 1) self.layout.addWidget(self.postDecrypt, 5, 9, 1, 1) self.layout.addWidget(self.linePasswordL, 6, 2, 1, 4) self.layout.addWidget(self.linePasswordR, 6, 7, 1, 4) self.layout.addWidget(self.checkPwdL, 6, 6, 1, 1) self.layout.addWidget(self.checkPwdR, 6, 11, 1, 1) self.layout.addWidget(self.lineRSAPathL, 7, 2, 1, 4) self.layout.addWidget(self.lineRSAPathR, 7, 7, 1, 4) self.layout.addWidget(self.buttonBrowseRSAL, 7, 6, 1, 1) self.layout.addWidget(self.buttonBrowseRSAR, 7, 11, 1, 1) self.layout.addWidget(self.leftText, 8, 2, 32, 5) self.layout.addWidget(self.rightText, 8, 7, 32, 5) self.layout.addWidget(self.setFormatting, 40, 2, 1, 1) self.layout.addWidget(self.setTags, 40, 3, 1, 1) self.layout.addWidget(self.showHTML, 40, 4, 1, 1) self.layout.addWidget(self.loadFile, 40, 7, 1, 1) self.layout.addWidget(self.saveFile, 40, 8, 1, 1) self.layout.addWidget(self.helpButton, 40, 9, 1, 1) self.layout.addWidget(self.nrLettersL, 40, 6, 1, 1) self.layout.addWidget(self.nrLettersR, 40, 11, 1, 1) self.__setup() # Prepair all components! self.__connect() # Connect all components! def __setup(self): ''' Setup all components. ''' # # Toogle buttons. self.buttonCryptMode.setCheckable(True) self.buttonCryptMode.setChecked(True) self.buttonCryptMode.setToolTip('Switch to Encryption mode') self.buttonCryptMode.setStyleSheet(D['STYLE_BUTTON']) self.buttonDecryptMode.setCheckable(True) self.buttonDecryptMode.setToolTip('Switch to Decryption mode') self.buttonDecryptMode.setStyleSheet(D['STYLE_BUTTON']) self.helpButton.setStyleSheet(D['STYLE_HELP_BUTTON']) self.minButton.setMaximumWidth(25) self.minButton.setMaximumHeight(25) self.minButton.setStyleSheet(D['STYLE_MIN_BUTTON']) self.closeButton.setMaximumWidth(25) self.closeButton.setMaximumHeight(25) self.closeButton.setStyleSheet(D['STYLE_CLOSE_BUTTON']) # Some styles. self.loadFile.setStyleSheet(D['STYLE_BUTTON']) self.saveFile.setStyleSheet(D['STYLE_BUTTON']) self.leftText.setStyleSheet(D['STYLE_L_TEXTEDIT']) self.rightText.setStyleSheet(D['STYLE_R_TEXTEDIT']) # Password fields. self.linePasswordL.setEchoMode(QtGui.QLineEdit.Password) self.linePasswordL.setToolTip('Password used for encrypting the text') self.linePasswordL.setMaxLength(99) self.linePasswordL.setStyleSheet(D['STYLE_LINEEDIT']) self.checkPwdL.setTristate(False) self.checkPwdL.setStyleSheet(D['STYLE_CHECKBOX']) self.linePasswordR.setEchoMode(QtGui.QLineEdit.Password) self.linePasswordR.setToolTip('Password used for decrypting the text') self.linePasswordR.setMaxLength(99) self.linePasswordR.setDisabled(True) self.linePasswordR.setStyleSheet(D['STYLE_LINEEDIT']) self.checkPwdR.setTristate(False) self.checkPwdR.setStyleSheet(D['STYLE_CHECKBOX']) # RSA Path. self.lineRSAPathL.setStyleSheet(D['STYLE_LINEEDIT']) self.lineRSAPathL.setToolTip('RSA Encryption requires both a password and the path to a public/ private RSA key') self.lineRSAPathL.hide() self.lineRSAPathR.setStyleSheet(D['STYLE_LINEEDIT']) self.lineRSAPathR.setToolTip('RSA Decryption requires both a password and the path to a public/ private RSA key') self.lineRSAPathR.hide() self.lineRSAPathR.setDisabled(True) self.buttonBrowseRSAL.setStyleSheet(D['STYLE_BUTTON']) self.buttonBrowseRSAL.hide() self.buttonBrowseRSAL.setMaximumWidth(60) self.buttonBrowseRSAR.setStyleSheet(D['STYLE_BUTTON']) self.buttonBrowseRSAR.hide() self.buttonBrowseRSAR.setMaximumWidth(60) self.buttonBrowseRSAR.setDisabled(True) # Formatted text. self.setFormatting.setTristate(False) self.setFormatting.setChecked(True) self.setFormatting.setToolTip('Encrypt this text as HTML') self.setFormatting.setStyleSheet(D['STYLE_CHECKBOX']) self.setTags.setTristate(False) self.setTags.setToolTip('Strip pre/enc/post tags') self.setTags.setStyleSheet(D['STYLE_CHECKBOX']) self.showHTML.setTristate(False) self.showHTML.setToolTip('Toogle view HTML source behind the formatted text') self.showHTML.setStyleSheet(D['STYLE_CHECKBOX']) # All combo boxes. MIN = 120 self.preProcess.setMinimumWidth(MIN) self.preProcess.setStyleSheet(D['STYLE_COMBOBOX']) self.comboCrypt.setMinimumWidth(MIN) self.comboCrypt.setStyleSheet(D['STYLE_COMBOBOX']) self.postProcess.setMinimumWidth(MIN) self.postProcess.setStyleSheet(D['STYLE_COMBOBOX']) self.preDecrypt.setMinimumWidth(MIN) self.preDecrypt.setStyleSheet(D['STYLE_COMBOBOX']) self.comboDecrypt.setMinimumWidth(MIN) self.comboDecrypt.setStyleSheet(D['STYLE_COMBOBOX']) self.postDecrypt.setMinimumWidth(MIN) self.postDecrypt.setStyleSheet(D['STYLE_COMBOBOX']) # Pre combo-boxes. self.preProcess.setToolTip('Select pre-process') self.postDecrypt.setToolTip('Select post-decrypt') for scramble in SCRAMBLE: self.preProcess.addItem(scramble, scramble) self.postDecrypt.addItem(scramble, scramble) # Encryption/ decryption combo-boxes. self.comboCrypt.setToolTip('Select encryption algorithm; it will use the provided password') self.comboDecrypt.setToolTip('Select encryption algorithm; it will use the provided password') for enc in ENC.keys(): self.comboCrypt.addItem(enc, enc) self.comboDecrypt.addItem(enc, enc) # Post combo-boxes. self.postProcess.setToolTip('Select post-process') self.preDecrypt.setToolTip('Select pre-decrypt') for encode in ENCODE: self.postProcess.addItem(encode, encode) self.preDecrypt.addItem(encode, encode) # def __connect(self): ''' Connect all components. ''' # self.linePasswordL.textChanged.connect(self.onLeftTextChanged) self.leftText.textChanged.connect(self.onLeftTextChanged) self.checkPwdL.stateChanged.connect(lambda x: \ self.linePasswordL.setEchoMode(QtGui.QLineEdit.Normal) if self.checkPwdL.isChecked() \ else self.linePasswordL.setEchoMode(QtGui.QLineEdit.Password)) self.buttonCryptMode.clicked.connect(self.onCryptMode) # self.linePasswordR.textChanged.connect(self.onRightTextChanged) self.rightText.textChanged.connect(self.onRightTextChanged) self.checkPwdR.stateChanged.connect(lambda x: \ self.linePasswordR.setEchoMode(QtGui.QLineEdit.Normal) if self.checkPwdR.isChecked() \ else self.linePasswordR.setEchoMode(QtGui.QLineEdit.Password)) self.buttonDecryptMode.clicked.connect(self.onDecryptMode) # self.buttonBrowseRSAL.clicked.connect(self.browseRSAkey) self.buttonBrowseRSAR.clicked.connect(self.browseRSAkey) # self.preProcess.currentIndexChanged.connect(self.onLeftTextChanged) self.comboCrypt.currentIndexChanged.connect(self.onLeftTextChanged) self.postProcess.currentIndexChanged.connect(self.onLeftTextChanged) # self.preDecrypt.currentIndexChanged.connect(self.onRightTextChanged) self.comboDecrypt.currentIndexChanged.connect(self.onRightTextChanged) self.postDecrypt.currentIndexChanged.connect(self.onRightTextChanged) # self.saveFile.clicked.connect(self.onSave) self.loadFile.clicked.connect(self.onLoad) self.helpButton.clicked.connect(self.onHelp) self.setFormatting.toggled.connect(self.onLeftTextChanged) self.setTags.toggled.connect(self.onLeftTextChanged) self.showHTML.toggled.connect(self.toggleHtml) # self.minButton.clicked.connect(lambda: self.setWindowState(QtCore.Qt.WindowMinimized)) self.closeButton.clicked.connect(lambda: self.close()) # # ACTION ! self.onCryptMode() # def dragEnterEvent(self, e): # mime_data = e.mimeData() # Accept plain text, HTML text and file paths. if mime_data.hasHtml() or mime_data.hasText() or mime_data.hasFormat('text/uri-list'): e.accept() else: e.ignore() # def dropEvent(self, e): # mime_data = e.mimeData() if mime_data.hasFormat('text/html'): dataf = mime_data.html() self.leftText.setHtml(dataf) elif mime_data.hasFormat('text/plain'): dataf = mime_data.text() self.leftText.setPlainText(dataf) elif mime_data.hasFormat('text/uri-list'): uri = mime_data.data('text/uri-list') uris = str(uri).split('\r\n')[:-1] # List of dragged files. for url in uris: # f_name = urllib.unquote(url) # Ignore windows shortcuts. They are ugly :) if os.path.splitext(f_name)[1].lower() == '.lnk': continue o = urllib.urlopen(url) sz = os.fstat(o.fileno())[6] t = tarfile.TarInfo(os.path.split(f_name)[1]) t.size = sz #self.attach.addFile(t, o) #? # print('I should encrypt on DROP!') # def browseRSAkey(self): # f = QtGui.QFileDialog() path = f.getOpenFileName(self, 'Path to RSA public or private key', os.getcwd(), 'All files (*.*)') if not path: return # self.SE.rsa_path = path self.lineRSAPathL.setText(path) self.lineRSAPathR.setText(path) # def onCryptMode(self): # self.buttonCryptMode.setChecked(True) self.buttonCryptMode.setText('Encrypt Mode is Enabled') self.buttonDecryptMode.setChecked(False) self.buttonDecryptMode.setText('Decrypt Mode') # self.linePasswordL.setDisabled(False) self.lineRSAPathL.setDisabled(False) self.buttonBrowseRSAL.setDisabled(False) self.leftText.setDisabled(False) # self.linePasswordR.setDisabled(True) self.lineRSAPathR.setDisabled(True) self.buttonBrowseRSAR.setDisabled(True) self.rightText.setDisabled(True) # self.checkPwdL.setDisabled(False) self.checkPwdR.setDisabled(True) # self.preProcess.setCurrentIndex(self.postDecrypt.currentIndex()) self.comboCrypt.setCurrentIndex(self.comboDecrypt.currentIndex()) self.postProcess.setCurrentIndex(self.preDecrypt.currentIndex()) # def onDecryptMode(self): # self.buttonCryptMode.setChecked(False) self.buttonCryptMode.setText('Encrypt Mode') self.buttonDecryptMode.setChecked(True) self.buttonDecryptMode.setText('Decrypt Mode is Enabled') # self.linePasswordL.setDisabled(True) self.lineRSAPathL.setDisabled(True) self.buttonBrowseRSAL.setDisabled(True) self.leftText.setDisabled(True) # self.linePasswordR.setDisabled(False) self.lineRSAPathR.setDisabled(False) self.buttonBrowseRSAR.setDisabled(False) self.rightText.setDisabled(False) # self.checkPwdL.setDisabled(True) self.checkPwdR.setDisabled(False) # self.postDecrypt.setCurrentIndex(self.preProcess.currentIndex()) self.comboDecrypt.setCurrentIndex(self.comboCrypt.currentIndex()) self.preDecrypt.setCurrentIndex(self.postProcess.currentIndex()) # def cleanupHtml(self, txt): # def spacerepl(matchobj): return matchobj.group(0).replace(b' ', b' ') # Replacing all spaces with txt = re.sub(b'>([^<>]+)<(?!/style>)', spacerepl, txt) # Write the new file open('doc.htm', 'w').write(txt) # Process the file with Tidy if platform.uname()[0].lower() == 'windows': p = subprocess.Popen(['tidy.exe', '-config', 'tidy.txt', 'doc.htm']).wait() elif platform.uname()[0].lower() == 'linux': env = os.environ env.update({'LD_LIBRARY_PATH': os.getcwd()}) p = subprocess.Popen(['./tidy', '-config', 'tidy.txt', 'doc.htm'], env=env).wait() else: print('Platform `%s` is not supported yet!\n' % platform.uname()[0]) txt = open('doc.htm', 'r').read() # Delete the wrong/ obsolete tags txt = txt.replace(b'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"\n"http://www.w3.org/TR/html4/loose.dtd">\n', '') txt = txt.replace(b'<meta name="generator" content="HTML Tidy for Windows (vers 25 March 2009), see www.w3.org">\n', '') txt = txt.replace(b'<meta name="generator" content="HTML Tidy for Linux/x86 (vers 25 March 2009), see www.w3.org">\n', '') txt = txt.replace(b'<meta name="generator" content="HTML Tidy for Linux (vers 25 March 2009), see www.w3.org">\n', '') txt = txt.replace(b'<meta name="qrichtext" content="1">\n', '') txt = txt.replace(b'<title></title>\n', '') txt = txt.replace(b'</style>\n\n<style type="text/css">\n', '') txt = txt.replace(b'<br>\n', '\n') # The clean file, for debug... open('doc.htm', 'w').write(txt) return txt # def toggleHtml(self): # if self.showHTML.isChecked(): vDlg = QtGui.QDialog(self.centralWidget) vDlg.setMinimumSize(C['W_WIDTH'], C['W_HEIGHT']-20) vDlg.text = QtGui.QPlainTextEdit(self.cleanupHtml( self.leftText.toHtml().encode('latin1') ), vDlg) vDlg.btnSave = QtGui.QPushButton('Save', vDlg) vDlg.btnSave.clicked.connect( lambda: vDlg.accept() ) vDlg.btnCancel = QtGui.QPushButton('Cancel', vDlg) vDlg.btnCancel.clicked.connect( lambda: vDlg.reject() ) layout = QtGui.QGridLayout(vDlg) layout.addWidget(vDlg.text, 1, 1, 2, 2) layout.addWidget(vDlg.btnSave, 3, 1) layout.addWidget(vDlg.btnCancel, 3, 2) vDlg.setLayout(layout) vRes = vDlg.exec_() if vRes: self.leftText.setHtml( vDlg.text.toPlainText().encode('latin1') ) self.showHTML.setChecked(False) # def onLeftTextChanged(self): # if not self.buttonCryptMode.isChecked(): return # # Save all pre/enc/post operations. pre = self.preProcess.currentText() enc = self.comboCrypt.currentText() post = self.postProcess.currentText() # # If encryption mode is RSA, reveal key path. if enc=='RSA': self.lineRSAPathL.show() self.lineRSAPathR.show() self.buttonBrowseRSAL.show() self.buttonBrowseRSAR.show() else: self.lineRSAPathL.hide() self.lineRSAPathR.hide() self.buttonBrowseRSAL.hide() self.buttonBrowseRSAR.hide() # if not self.leftText.toPlainText(): self.rightText.clear() return # pwd = self.linePasswordL.text().encode() tags = not self.setTags.isChecked() # if self.setFormatting.isChecked(): # HTML string. txt = self.leftText.toHtml().encode('utf-8') # Cleanup HTML string. txt = self.cleanupHtml(txt) else: txt = self.leftText.toPlainText().encode('utf-8') # self.postDecrypt.setCurrentIndex(self.preProcess.currentIndex()) self.comboDecrypt.setCurrentIndex(self.comboCrypt.currentIndex()) self.preDecrypt.setCurrentIndex(self.postProcess.currentIndex()) # # Encrypt the text. final = self.SE.encrypt(txt, pre, enc, post, pwd, tags) # if final: self.rightText.setPlainText(final) if self.setFormatting.isChecked(): self.nrLettersL.setText('Html: %i' % len(txt)) else: self.nrLettersL.setText('Text: %i' % len(txt)) self.nrLettersR.setText('Enc: %i' % len(final)) self.textBar.setStyleSheet(D['TXT_BAR_OK']) self.textBar.setText(' Encryption mode step 1: %s , step 2: %s , step 3: %s' % (pre, enc, post)) else: self.rightText.clear() self.textBar.setStyleSheet(D['TXT_BAR_BAD']) self.textBar.setText(self.SE.error) # def onRightTextChanged(self): # if not self.buttonDecryptMode.isChecked(): return # txt = self.rightText.toPlainText().encode() # try: re_groups = re.search(NO_TAGS, txt).groups() tags = findg(re_groups) # If Json. if tags.startswith('"pre"'): pre = 'Json' enc = re.search('"enc":"([0-9a-zA-Z ]{1,3})"', tags).group(1) post = re.search('"pre":"([0-9a-zA-Z ]{1,3})"', tags).group(1) # If XML. elif tags.startswith('<pre>'): pre = 'XML' enc = re.search('<enc>([0-9a-zA-Z ]{1,3})</enc>', tags).group(1) post = re.search('<pre>([0-9a-zA-Z ]{1,3})</pre>', tags).group(1) else: pre=None ; enc=None ; post=None # Identify the rest. if not pre: pre = tags.split(':')[2] if not enc: enc = tags.split(':')[1] if not post: post = tags.split(':')[0] pre = pre.decode() enc = enc.decode() post = post.decode() self.postDecrypt.setCurrentIndex( self.postDecrypt.findText(post, QtCore.Qt.MatchFlag(QtCore.Qt.MatchContains)) ) self.comboDecrypt.setCurrentIndex( self.comboDecrypt.findText(enc, QtCore.Qt.MatchFlag(QtCore.Qt.MatchContains)) ) self.preDecrypt.setCurrentIndex( self.preDecrypt.findText(pre, QtCore.Qt.MatchFlag(QtCore.Qt.MatchContains)) ) except: pass # # This must be right here. pre = self.preDecrypt.currentText() enc = self.comboDecrypt.currentText() post = self.postDecrypt.currentText() pwd = self.linePasswordR.text().encode() # # If encryption mode is RSA, reveal key path. if enc=='RSA': self.lineRSAPathL.show() self.lineRSAPathR.show() self.buttonBrowseRSAL.show() self.buttonBrowseRSAR.show() else: self.lineRSAPathL.hide() self.lineRSAPathR.hide() self.buttonBrowseRSAL.hide() self.buttonBrowseRSAR.hide() # if not txt: self.leftText.clear() return # self.preProcess.setCurrentIndex(self.postDecrypt.currentIndex()) self.comboCrypt.setCurrentIndex(self.comboDecrypt.currentIndex()) self.postProcess.setCurrentIndex(self.preDecrypt.currentIndex()) # # Decrypt the text. final = self.SE.decrypt(txt, pre, enc, post, pwd) # if final: # Setup string as HTML. self.leftText.setHtml(final.decode()) self.nrLettersL.setText('Dec: %i' % len(final)) self.nrLettersR.setText('Enc: %i' % len(txt)) self.textBar.setStyleSheet(D['TXT_BAR_OK']) self.textBar.setText(' Decryption mode step 1: %s , step 2: %s , step 3: %s' % (pre, enc, post)) else: self.leftText.clear() self.textBar.setStyleSheet(D['TXT_BAR_BAD']) self.textBar.setText(self.SE.error) # def onSave(self): # # Save all pre/enc/post operations. pre = self.preProcess.currentText() enc = self.comboCrypt.currentText() post = self.postProcess.currentText() # f = QtGui.QFileDialog() if post in ['Base64 Codec', 'Base32 Codec', 'HEX Codec']: path = f.getSaveFileName(self, 'Save crypted text', os.getcwd(), 'PNG Images (*.png)') ext = '.png' elif post=='XML': path = f.getSaveFileName(self, 'Save crypted text', os.getcwd(), 'XML Files (*.xml)') ext = '.xml' elif post=='Json': path = f.getSaveFileName(self, 'Save crypted text', os.getcwd(), 'Json Files (*.json)') ext = '.json' else: path = f.getSaveFileName(self, 'Save crypted text', os.getcwd(), 'All files (*.*)') ext = '' if not path: return # # Save password. pwd = self.linePasswordL.text() # Text from rigth side. txt = self.rightText.toPlainText() # File extension. if not os.path.splitext(path)[1]: path += ext # # For PNG files. if ext=='.png': self.SE.toImage(txt, pre, enc, post, pwd, path, encrypt=False) else: open(path, 'w').write(txt) # def onLoad(self): # f = QtGui.QFileDialog() path = f.getOpenFileName(self, 'Load crypted text', os.getcwd(), 'All files (*.*)') if not path: return # Import the text from file, without decryption. val = self.SE._import(pre=None, enc=None, post=None, pwd=None, fpath=path, decrypt=False) if val: # For step 1. self.preProcess.setCurrentIndex( self.preProcess.findText(self.SE.post, QtCore.Qt.MatchFlag(QtCore.Qt.MatchFixedString)) ) self.postDecrypt.setCurrentIndex( self.postDecrypt.findText(self.SE.post, QtCore.Qt.MatchFlag(QtCore.Qt.MatchFixedString)) ) # For step 2. self.comboCrypt.setCurrentIndex( self.comboCrypt.findText(self.SE.enc, QtCore.Qt.MatchFlag(QtCore.Qt.MatchFixedString)) ) self.comboDecrypt.setCurrentIndex( self.comboDecrypt.findText(self.SE.enc, QtCore.Qt.MatchFlag(QtCore.Qt.MatchFixedString)) ) # For step 3. self.postProcess.setCurrentIndex( self.postProcess.findText(self.SE.pre, QtCore.Qt.MatchFlag(QtCore.Qt.MatchFixedString)) ) self.preDecrypt.setCurrentIndex( self.preDecrypt.findText(self.SE.pre, QtCore.Qt.MatchFlag(QtCore.Qt.MatchFixedString)) ) self.rightText.setPlainText(val) self.onDecryptMode() self.onRightTextChanged() self.rightText.setFocus() # def onHelp(self): # QtGui.QMessageBox.about(self.centralWidget, 'Scrambled Egg %s Help' % __version__, '<br><b>Copyright (C) 2010-2012</b> : Cristi Constantin. All rights reserved.<br>' '<b>Website</b> : http://scrambled-egg.googlecode.com/<br><br>' 'Scrambled-Egg is a software designed for encrypting your sensitive data.<br>' 'This is done in <font color="blue"><b>3 steps</b></font> : <i>pre encryption</i>, <i>encryption</i>, and <i>post encryption</i>.<br>' 'The input data can be : plain text, or formatted text (Microsoft Office or HTML).<br><br>' '<font color="blue"><b>Step 1</b></font> can compress your data using <b>ZLIB</b>, or <b>BZ2</b>. This step is optional.<br>' '<font color="blue"><b>Step 2</b></font> is the real encryption, for example with <b>AES</b>, or <b>Blowfish</b>. ' 'The password is used only in this step. ' 'For <b>RSA</b> encryption, along with the password, you have to type the path to the public or private RSA key.<br>' '<font color="blue"><b>Step 3</b></font> will encode your data. This step is required, the rest are optional. ' 'There are a lot of encodings available, for example <b>Base64</b>, or <b>HEX</b>.<br><br>' 'This FREE program is distributed in the hope that it will be useful, but without any warranty.<br><br>' 'Enjoy!')
class Window(wx.Frame): def __init__(self): wx.Frame.__init__(self, parent=None, title='Scrambled Egg GUI', size=(800, 600)) self.SetMinSize((800, 300)) panel = wx.Panel(self) hbox = wx.BoxSizer(wx.HORIZONTAL) self.statusBar = self.CreateStatusBar() self.statusBar.SetStatusText('Ready') self.SE = ScrambledEgg() # Left vertical layout leftbag = wx.BoxSizer(wx.VERTICAL) # Combo bag left cbagl = wx.BoxSizer(wx.HORIZONTAL) self.preProcess = wx.ComboBox(panel, -1, SCRAMBLE[0], size=(130, -1), choices=SCRAMBLE, style=wx.CB_READONLY) # Left side self.comboCrypt = wx.ComboBox(panel, -1, ENC.keys()[0], size=(130, -1), choices=ENC.keys(), style=wx.CB_READONLY) # Left side self.postProcess = wx.ComboBox(panel, -1, ENCODE[0], size=(130, -1), choices=ENCODE, style=wx.CB_READONLY) # Left side # cbagl.Add(self.preProcess) cbagl.Add(self.comboCrypt) cbagl.Add(self.postProcess) self.buttonCryptMode = wx.ToggleButton(panel, label='Encryption Mode is Enabled') self.buttonCryptMode.SetValue(True) # Password bag left pbagl = wx.BoxSizer(wx.HORIZONTAL) self.linePasswordL = wx.TextCtrl(panel, -1, style=wx.TE_PASSWORD) # Left password line self.checkPwdL = wx.CheckBox(panel, -1, size=(85, -1), label='<- Pwd') # Left side # pbagl.Add(self.linePasswordL, proportion=5, flag=wx.EXPAND) pbagl.Add(self.checkPwdL, proportion=1, flag=wx.ALIGN_CENTER_VERTICAL) # Left check boxes bbagl = wx.BoxSizer(wx.HORIZONTAL) self.setTags = wx.CheckBox(panel, -1, size=(105, 25), label='Tags') # Left side self.setTags.SetValue(True) self.nrLettersL = wx.StaticText(panel, -1, '') # bbagl.Add(self.setTags, proportion=5, flag=wx.EXPAND) bbagl.Add(self.nrLettersL, proportion=1, flag=wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_CENTER_HORIZONTAL) self.leftText = wx.TextCtrl(panel, style=wx.TE_MULTILINE) # Plain text # Append in left vertical layout leftbag.Add(self.buttonCryptMode, border=3, flag=wx.EXPAND|wx.RIGHT|wx.BOTTOM) leftbag.Add(cbagl, border=3, flag=wx.EXPAND|wx.RIGHT|wx.BOTTOM) leftbag.Add(pbagl, border=3, flag=wx.EXPAND|wx.RIGHT|wx.BOTTOM) leftbag.Add(self.leftText, border=3, proportion=10, flag=wx.EXPAND|wx.RIGHT|wx.BOTTOM) leftbag.Add(bbagl, border=3, flag=wx.EXPAND|wx.RIGHT|wx.BOTTOM) # Right vertical layout rightbag = wx.BoxSizer(wx.VERTICAL) # Combo bag right cbagr = wx.BoxSizer(wx.HORIZONTAL) self.preDecrypt = wx.ComboBox(panel, -1, SCRAMBLE[0], size=(130, -1), choices=SCRAMBLE, style=wx.CB_READONLY) # Right side self.comboDecrypt = wx.ComboBox(panel, -1, ENC.keys()[0], size=(130, -1), choices=ENC.keys(), style=wx.CB_READONLY) # Right side self.postDecrypt = wx.ComboBox(panel, -1, ENCODE[0], size=(130, -1), choices=ENCODE, style=wx.CB_READONLY) # Right side cbagr.Add(self.preDecrypt) cbagr.Add(self.comboDecrypt) cbagr.Add(self.postDecrypt) self.buttonDecryptMode = wx.ToggleButton(panel, label='Decryption Mode') self.buttonDecryptMode.SetValue(False) # Password bag right pbagr = wx.BoxSizer(wx.HORIZONTAL) self.linePasswordR = wx.TextCtrl(panel, -1, style=wx.TE_PASSWORD) # Right password line self.checkPwdR = wx.CheckBox(panel, -1, size=(85, -1), label='<- Pwd') # Right side # pbagr.Add(self.linePasswordR, proportion=5, flag=wx.EXPAND) pbagr.Add(self.checkPwdR, proportion=1, flag=wx.ALIGN_CENTER_VERTICAL) # Right buttons bbagr = wx.BoxSizer(wx.HORIZONTAL) self.loadFile = wx.Button(panel, size=(105, 25), label='Import') # Right side self.saveFile = wx.Button(panel, size=(105, 25), label='Export') # Right side self.helpButton = wx.Button(panel, size=(105, 25), label='Help !') # Right side self.nrLettersR = wx.StaticText(panel, -1, '') # bbagr.Add(self.loadFile) bbagr.Add(self.saveFile) bbagr.Add(self.helpButton) bbagr.Add(self.nrLettersR, flag=wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_CENTER_HORIZONTAL) self.rightText = wx.TextCtrl(panel, style=wx.TE_MULTILINE) # Encrypted text # Append in right vertical layout rightbag.Add(self.buttonDecryptMode, border=3, flag=wx.EXPAND|wx.RIGHT|wx.BOTTOM) rightbag.Add(cbagr, border=3, flag=wx.EXPAND|wx.RIGHT|wx.BOTTOM) rightbag.Add(pbagr, border=3, flag=wx.EXPAND|wx.RIGHT|wx.BOTTOM) rightbag.Add(self.rightText, border=3, proportion=10, flag=wx.EXPAND|wx.RIGHT|wx.BOTTOM) rightbag.Add(bbagr, border=3, flag=wx.EXPAND|wx.RIGHT|wx.BOTTOM) hbox.Add(leftbag, proportion=1, flag=wx.EXPAND|wx.ALL, border=5) hbox.Add(rightbag, proportion=1, flag=wx.EXPAND|wx.ALL, border=5) panel.SetSizer(hbox) self._bind_events() self.Center() self.Show(True) self.leftText.SetFocus() def _bind_events(self): self.buttonCryptMode.Bind( wx.EVT_TOGGLEBUTTON, self.onCryptMode) self.buttonDecryptMode.Bind(wx.EVT_TOGGLEBUTTON, self.onDecryptMode) self.linePasswordL.Bind(wx.EVT_TEXT, self.onLeftTextChanged) self.leftText.Bind(wx.EVT_TEXT, self.onLeftTextChanged) self.linePasswordR.Bind(wx.EVT_TEXT, self.onRightTextChanged) self.rightText.Bind(wx.EVT_TEXT, self.onRightTextChanged) self.checkPwdL.Bind(wx.EVT_CHECKBOX, lambda x: \ self.linePasswordL.SetWindowStyle(0) if self.checkPwdL.IsChecked() \ else self.linePasswordL.SetWindowStyle(wx.TE_PASSWORD)) self.checkPwdR.Bind(wx.EVT_CHECKBOX, lambda x: \ self.linePasswordR.SetWindowStyle(0) if self.checkPwdR.IsChecked() \ else self.linePasswordR.SetWindowStyle(wx.TE_PASSWORD)) self.preProcess.Bind(wx.EVT_COMBOBOX, self.onLeftTextChanged) self.comboCrypt.Bind(wx.EVT_COMBOBOX, self.onLeftTextChanged) self.postProcess.Bind(wx.EVT_COMBOBOX, self.onLeftTextChanged) self.preDecrypt.Bind(wx.EVT_COMBOBOX, self.onRightTextChanged) self.comboDecrypt.Bind(wx.EVT_COMBOBOX, self.onRightTextChanged) self.postDecrypt.Bind(wx.EVT_COMBOBOX, self.onRightTextChanged) self.saveFile.Bind(wx.EVT_BUTTON, self.onSave) self.loadFile.Bind(wx.EVT_BUTTON, self.onLoad) self.helpButton.Bind(wx.EVT_BUTTON, self.onHelp) # ACTION ! self.onCryptMode(None) def onCryptMode(self, e): self.buttonCryptMode.SetValue(True) self.buttonCryptMode.SetLabel('Encrypt Mode is Enabled') self.buttonDecryptMode.SetValue(False) self.buttonDecryptMode.SetLabel('Decrypt Mode') # self.linePasswordL.Enable(True) #self.lineRSAPathL.Enable(True) #self.buttonBrowseRSAL.Enable(True) self.leftText.Enable(True) # self.linePasswordR.Enable(False) #self.lineRSAPathR.Enable(False) #self.buttonBrowseRSAR.Enable(False) self.rightText.Enable(False) # self.checkPwdL.Enable(True) self.checkPwdR.Enable(False) # self.preProcess.SetSelection(self.preDecrypt.GetCurrentSelection()) self.comboCrypt.SetSelection(self.comboDecrypt.GetCurrentSelection()) self.postProcess.SetSelection(self.postDecrypt.GetCurrentSelection()) # def onDecryptMode(self, e): self.buttonCryptMode.SetValue(False) self.buttonCryptMode.SetLabel('Encrypt Mode') self.buttonDecryptMode.SetValue(True) self.buttonDecryptMode.SetLabel('Decrypt Mode is Enabled') # self.linePasswordL.Enable(False) #self.lineRSAPathL.Enable(False) #self.buttonBrowseRSAL.Enable(False) self.leftText.Enable(False) # self.linePasswordR.Enable(True) #self.lineRSAPathR.Enable(True) #self.buttonBrowseRSAR.Enable(True) self.rightText.Enable(True) # self.checkPwdL.Enable(False) self.checkPwdR.Enable(True) # self.postDecrypt.SetSelection(self.postProcess.GetCurrentSelection()) self.comboDecrypt.SetSelection(self.comboCrypt.GetCurrentSelection()) self.preDecrypt.SetSelection(self.preProcess.GetCurrentSelection()) # def onLeftTextChanged(self, e): if not self.buttonCryptMode.GetValue(): return # Save all pre/enc/post operations. pre = self.preProcess.GetValue() enc = self.comboCrypt.GetValue() post = self.postProcess.GetValue() if not self.leftText.GetValue(): self.rightText.Clear() return pwd = self.linePasswordL.GetValue().encode() tags = self.setTags.GetValue() txt = self.leftText.GetValue().encode() self.postDecrypt.SetSelection(self.postProcess.GetCurrentSelection()) self.comboDecrypt.SetSelection(self.comboCrypt.GetCurrentSelection()) self.preDecrypt.SetSelection(self.preProcess.GetCurrentSelection()) # Encrypt the text... final = self.SE.encrypt(txt, pre, enc, post, pwd, tags) if final: self.statusBar.SetForegroundColour(wx.BLUE) self.statusBar.SetStatusText(' Encryption mode step 1: %s , step 2: %s , step 3: %s' % (pre, enc, post)) self.rightText.SetValue(final) self.nrLettersL.SetLabel('Plain: %i' % len(txt)) self.nrLettersR.SetLabel(' Enc: %i' % len(final)) else: self.rightText.Clear() self.statusBar.SetForegroundColour(wx.RED) self.statusBar.SetStatusText(self.SE.error) def onRightTextChanged(self, e): if not self.buttonDecryptMode.GetValue(): return txt = self.rightText.GetValue().encode() try: re_groups = re.search(NO_TAGS, txt).groups() tags = findg(re_groups) # If Json. if tags.startswith('"pre"'): pre = 'Json' enc = re.search('"enc":"([0-9a-zA-Z ]{1,3})"', tags).group(1) post = re.search('"pre":"([0-9a-zA-Z ]{1,3})"', tags).group(1) # If XML. elif tags.startswith('<pre>'): pre = 'XML' enc = re.search('<enc>([0-9a-zA-Z ]{1,3})</enc>', tags).group(1) post = re.search('<pre>([0-9a-zA-Z ]{1,3})</pre>', tags).group(1) else: pre=None ; enc=None ; post=None # Identify the rest. if not pre: pre = tags.split(':')[2] if not enc: enc = tags.split(':')[1] if not post: post = tags.split(':')[0] pre = {ENCODE_D[k]:k for k in ENCODE_D}[pre] enc = {ENC[k]:k for k in ENC}[enc] post = {SCRAMBLE_D[k]:k for k in SCRAMBLE_D}[post] self.postDecrypt.SetStringSelection(pre) self.comboDecrypt.SetStringSelection(enc) self.preDecrypt.SetStringSelection(post) except: pass # This must be right here. pre = self.postDecrypt.GetValue() enc = self.comboDecrypt.GetValue() post = self.preDecrypt.GetValue() pwd = self.linePasswordR.GetValue().encode() if not txt: self.leftText.Clear() return self.preProcess.SetSelection(self.preDecrypt.GetCurrentSelection()) self.comboCrypt.SetSelection(self.comboDecrypt.GetCurrentSelection()) self.postProcess.SetSelection(self.postDecrypt.GetCurrentSelection()) # Decrypt the text... final = self.SE.decrypt(txt, pre, enc, post, pwd) if final: self.statusBar.SetForegroundColour(wx.BLUE) self.statusBar.SetStatusText(' Decryption mode step 1: %s , step 2: %s , step 3: %s' % (pre, enc, post)) self.leftText.SetValue(final.encode()) self.nrLettersL.SetLabel('Plain: %i' % len(final)) self.nrLettersR.SetLabel(' Enc: %i' % len(txt)) else: self.leftText.Clear() self.statusBar.SetForegroundColour(wx.RED) self.statusBar.SetStatusText(self.SE.error) def onSave(self, e): # Save all pre/enc/post operations. pre = self.preProcess.GetValue() enc = self.comboCrypt.GetValue() post = self.postProcess.GetValue() if post in ['Base64 Codec', 'Base32 Codec', 'HEX Codec']: wildcard = '*.png' elif post=='XML': wildcard = '*.xml' elif post=='Json': wildcard = '*.json' else: wildcard = '*.*' f = wx.FileDialog(self, message='Save crypted text', defaultDir=os.getcwd(), wildcard=wildcard, style=wx.FD_SAVE) r = f.ShowModal() if r != 5100: return path = f.GetPath() if not path: return # Save password. pwd = self.linePasswordL.GetValue() # Text from rigth side. txt = self.rightText.GetValue() # File extension. if not os.path.splitext(path)[1]: path += wildcard[1:] # For PNG files. if wildcard=='*.png': self.SE.toImage(txt, pre, enc, post, pwd, path, encrypt=False) else: open(path, 'w').write(txt) def onLoad(self, e): f = wx.FileDialog(self, message='Save crypted text', defaultDir=os.getcwd()) r = f.ShowModal() if r != 5100: return path = f.GetPath() if not path: return # Import the text from file, without decryption. val = self.SE._import(pre=None, enc=None, post=None, pwd=None, fpath=path, decrypt=False) if val: # For step 1. self.preProcess.SetValue( self.SE.post ) self.postDecrypt.SetValue( self.SE.pre ) # For step 2. self.comboCrypt.SetValue( self.SE.enc ) self.comboDecrypt.SetValue( self.SE.enc ) # For step 3. self.postProcess.SetValue( self.SE.pre ) self.preDecrypt.SetValue( self.SE.post ) self.rightText.SetValue(val.encode()) self.onDecryptMode(None) self.onRightTextChanged(None) self.rightText.SetFocus() def onHelp(self, e): # dia = wx.MessageDialog(None, 'Copyright (C) 2010-2012: Cristi Constantin, all rights reserved.\n' 'Website: http://scrambled-egg.googlecode.com/\n\n' 'Scrambled-Egg is a software designed for encrypting your sensitive data.\n' 'This is done in 3 steps: pre encryption, encryption, and post encryption.\n' 'The input data can only be plain text.\n\n' 'Step 1 can compress your data using ZLIB, or BZ2. This step is optional.\n' 'Step 2 is the real encryption, for example with AES, or Blowfish.' 'The password is used only in this step.\n' #'For <b>RSA</b> encryption, along with the password, you have to type the path to the public or private RSA key.<br>' 'Step 3 will encode your data. This step is required, the rest are optional. ' 'There are a lot of encodings available, for example Base64, or HEX.\n\n' 'This FREE program is distributed in the hope that it will be useful, but without any warranty.\n\n' 'Enjoy!', 'Scrambled Egg %s Help' % __version__, wx.OK) dia.ShowModal()
class Window(QtGui.QMainWindow): def __init__(self): ''' Main window class. It's frameless and transparent. ''' super(Window, self).__init__(None, QtCore.Qt.FramelessWindowHint) QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('CleanLooks')) QtGui.QApplication.setPalette( QtGui.QApplication.style().standardPalette()) icon_path = os.path.split( os.path.abspath(__file__))[0] + '/config/icon.ico' self.setWindowIcon(QtGui.QIcon(icon_path)) self.resize(C['W_WIDTH'], C['W_HEIGHT']) self.setMaximumHeight(C['W_MAX_HEIGHT']) self.setStyleSheet(D['STYLE_MAIN']) self.setWindowTitle('Scrambled Egg %s :: Live Crypt' % __version__) self.setAttribute(QtCore.Qt.WA_TranslucentBackground) self.setWindowOpacity(0.9) self.setAcceptDrops(True) self.SE = ScrambledEgg() self.centralWidget = ContainerWidget(self) # Central Widget. self.setCentralWidget(self.centralWidget) self.container = QtGui.QWidget(self.centralWidget) # Container Widget. self.container.setObjectName('Container') self.container.setStyleSheet(D['STYLE_CONTAINER']) self.textBar = QtGui.QLabel(self) # Top text bar. self.layout = QtGui.QGridLayout(self.centralWidget) # Main Layout. self.centralWidget.setLayout(self.layout) self.leftText = QtGui.QTextEdit( '', self.centralWidget) # To write clean text. self.rightText = QtGui.QPlainTextEdit( '', self.centralWidget) # To view encrypted text. self.buttonCryptMode = QtGui.QPushButton(self.centralWidget) self.buttonDecryptMode = QtGui.QPushButton(self.centralWidget) self.buttonBrowseRSAL = QtGui.QPushButton('Browse', self.centralWidget) self.buttonBrowseRSAR = QtGui.QPushButton('Browse', self.centralWidget) self.preProcess = QtGui.QComboBox(self.centralWidget) # Left side. self.comboCrypt = QtGui.QComboBox(self.centralWidget) # Left side. self.postProcess = QtGui.QComboBox(self.centralWidget) # Left side. self.linePasswordL = QtGui.QLineEdit( self.centralWidget) # Left password line. self.lineRSAPathL = QtGui.QLineEdit( self.centralWidget) # Left RSA Path line. self.checkPwdL = QtGui.QCheckBox('<- Pwd', self.centralWidget) # Left side. self.nrLettersL = QtGui.QLabel('', self.centralWidget) # Left side. self.setFormatting = QtGui.QCheckBox('Formatted text', self.centralWidget) # Left side. self.setTags = QtGui.QCheckBox('No tags', self.centralWidget) # Left side. self.showHTML = QtGui.QCheckBox('Show HTML', self.centralWidget) # Left side. self.preDecrypt = QtGui.QComboBox(self.centralWidget) # Right side. self.comboDecrypt = QtGui.QComboBox(self.centralWidget) # Right side. self.postDecrypt = QtGui.QComboBox(self.centralWidget) # Right side. self.linePasswordR = QtGui.QLineEdit( self.centralWidget) # Right password line. self.lineRSAPathR = QtGui.QLineEdit( self.centralWidget) # Right RSA Path line. self.checkPwdR = QtGui.QCheckBox('<- Pwd', self.centralWidget) # Right side. self.nrLettersR = QtGui.QLabel('', self.centralWidget) # Right side. self.loadFile = QtGui.QPushButton('Import', self.centralWidget) # Right side. self.saveFile = QtGui.QPushButton('Export', self.centralWidget) # Right side. self.helpButton = QtGui.QPushButton('Help !', self.centralWidget) # Right side. self.minButton = QtGui.QPushButton(D['MIN_BTN_TXT'], self.centralWidget) self.closeButton = QtGui.QPushButton(D['CLOSE_BTN_TXT'], self.centralWidget) self.micLayout = QtGui.QHBoxLayout() self.micLayout.addWidget(self.minButton) self.micLayout.addWidget(self.closeButton) # Row, Col, rowSpan, colSpan self.layout.addWidget(self.container, 0, 1, 43, 12) self.layout.addWidget(self.textBar, 1, 1, 3, 8) self.layout.addLayout(self.micLayout, 1, 10 + C['MIC_BTNS_POS'], 1, C['MIC_BTNS_SPAN']) self.layout.addItem(QtGui.QSpacerItem(1, 8), 3, 1, 1, 1) self.layout.addWidget(self.buttonCryptMode, 4, 2, 1, 5) self.layout.addWidget(self.buttonDecryptMode, 4, 7, 1, 5) self.layout.addWidget(self.preProcess, 5, 2, 1, 1) self.layout.addWidget(self.comboCrypt, 5, 3, 1, 1) self.layout.addWidget(self.postProcess, 5, 4, 1, 1) self.layout.addWidget(self.preDecrypt, 5, 7, 1, 1) self.layout.addWidget(self.comboDecrypt, 5, 8, 1, 1) self.layout.addWidget(self.postDecrypt, 5, 9, 1, 1) self.layout.addWidget(self.linePasswordL, 6, 2, 1, 4) self.layout.addWidget(self.linePasswordR, 6, 7, 1, 4) self.layout.addWidget(self.checkPwdL, 6, 6, 1, 1) self.layout.addWidget(self.checkPwdR, 6, 11, 1, 1) self.layout.addWidget(self.lineRSAPathL, 7, 2, 1, 4) self.layout.addWidget(self.lineRSAPathR, 7, 7, 1, 4) self.layout.addWidget(self.buttonBrowseRSAL, 7, 6, 1, 1) self.layout.addWidget(self.buttonBrowseRSAR, 7, 11, 1, 1) self.layout.addWidget(self.leftText, 8, 2, 32, 5) self.layout.addWidget(self.rightText, 8, 7, 32, 5) self.layout.addWidget(self.setFormatting, 40, 2, 1, 1) self.layout.addWidget(self.setTags, 40, 3, 1, 1) self.layout.addWidget(self.showHTML, 40, 4, 1, 1) self.layout.addWidget(self.loadFile, 40, 7, 1, 1) self.layout.addWidget(self.saveFile, 40, 8, 1, 1) self.layout.addWidget(self.helpButton, 40, 9, 1, 1) self.layout.addWidget(self.nrLettersL, 40, 6, 1, 1) self.layout.addWidget(self.nrLettersR, 40, 11, 1, 1) self.__setup() # Prepair all components! self.__connect() # Connect all components! def __setup(self): ''' Setup all components. ''' # # Toogle buttons. self.buttonCryptMode.setCheckable(True) self.buttonCryptMode.setChecked(True) self.buttonCryptMode.setToolTip('Switch to Encryption mode') self.buttonCryptMode.setStyleSheet(D['STYLE_BUTTON']) self.buttonDecryptMode.setCheckable(True) self.buttonDecryptMode.setToolTip('Switch to Decryption mode') self.buttonDecryptMode.setStyleSheet(D['STYLE_BUTTON']) self.helpButton.setStyleSheet(D['STYLE_HELP_BUTTON']) self.minButton.setMaximumWidth(25) self.minButton.setMaximumHeight(25) self.minButton.setStyleSheet(D['STYLE_MIN_BUTTON']) self.closeButton.setMaximumWidth(25) self.closeButton.setMaximumHeight(25) self.closeButton.setStyleSheet(D['STYLE_CLOSE_BUTTON']) # Some styles. self.loadFile.setStyleSheet(D['STYLE_BUTTON']) self.saveFile.setStyleSheet(D['STYLE_BUTTON']) self.leftText.setStyleSheet(D['STYLE_L_TEXTEDIT']) self.rightText.setStyleSheet(D['STYLE_R_TEXTEDIT']) # Password fields. self.linePasswordL.setEchoMode(QtGui.QLineEdit.Password) self.linePasswordL.setToolTip('Password used for encrypting the text') self.linePasswordL.setMaxLength(99) self.linePasswordL.setStyleSheet(D['STYLE_LINEEDIT']) self.checkPwdL.setTristate(False) self.checkPwdL.setStyleSheet(D['STYLE_CHECKBOX']) self.linePasswordR.setEchoMode(QtGui.QLineEdit.Password) self.linePasswordR.setToolTip('Password used for decrypting the text') self.linePasswordR.setMaxLength(99) self.linePasswordR.setDisabled(True) self.linePasswordR.setStyleSheet(D['STYLE_LINEEDIT']) self.checkPwdR.setTristate(False) self.checkPwdR.setStyleSheet(D['STYLE_CHECKBOX']) # RSA Path. self.lineRSAPathL.setStyleSheet(D['STYLE_LINEEDIT']) self.lineRSAPathL.setToolTip( 'RSA Encryption requires both a password and the path to a public/ private RSA key' ) self.lineRSAPathL.hide() self.lineRSAPathR.setStyleSheet(D['STYLE_LINEEDIT']) self.lineRSAPathR.setToolTip( 'RSA Decryption requires both a password and the path to a public/ private RSA key' ) self.lineRSAPathR.hide() self.lineRSAPathR.setDisabled(True) self.buttonBrowseRSAL.setStyleSheet(D['STYLE_BUTTON']) self.buttonBrowseRSAL.hide() self.buttonBrowseRSAL.setMaximumWidth(60) self.buttonBrowseRSAR.setStyleSheet(D['STYLE_BUTTON']) self.buttonBrowseRSAR.hide() self.buttonBrowseRSAR.setMaximumWidth(60) self.buttonBrowseRSAR.setDisabled(True) # Formatted text. self.setFormatting.setTristate(False) self.setFormatting.setChecked(True) self.setFormatting.setToolTip('Encrypt this text as HTML') self.setFormatting.setStyleSheet(D['STYLE_CHECKBOX']) self.setTags.setTristate(False) self.setTags.setToolTip('Strip pre/enc/post tags') self.setTags.setStyleSheet(D['STYLE_CHECKBOX']) self.showHTML.setTristate(False) self.showHTML.setToolTip( 'Toogle view HTML source behind the formatted text') self.showHTML.setStyleSheet(D['STYLE_CHECKBOX']) # All combo boxes. MIN = 120 self.preProcess.setMinimumWidth(MIN) self.preProcess.setStyleSheet(D['STYLE_COMBOBOX']) self.comboCrypt.setMinimumWidth(MIN) self.comboCrypt.setStyleSheet(D['STYLE_COMBOBOX']) self.postProcess.setMinimumWidth(MIN) self.postProcess.setStyleSheet(D['STYLE_COMBOBOX']) self.preDecrypt.setMinimumWidth(MIN) self.preDecrypt.setStyleSheet(D['STYLE_COMBOBOX']) self.comboDecrypt.setMinimumWidth(MIN) self.comboDecrypt.setStyleSheet(D['STYLE_COMBOBOX']) self.postDecrypt.setMinimumWidth(MIN) self.postDecrypt.setStyleSheet(D['STYLE_COMBOBOX']) # Pre combo-boxes. self.preProcess.setToolTip('Select pre-process') self.postDecrypt.setToolTip('Select post-decrypt') for scramble in SCRAMBLE: self.preProcess.addItem(scramble, scramble) self.postDecrypt.addItem(scramble, scramble) # Encryption/ decryption combo-boxes. self.comboCrypt.setToolTip( 'Select encryption algorithm; it will use the provided password') self.comboDecrypt.setToolTip( 'Select encryption algorithm; it will use the provided password') for enc in ENC.keys(): self.comboCrypt.addItem(enc, enc) self.comboDecrypt.addItem(enc, enc) # Post combo-boxes. self.postProcess.setToolTip('Select post-process') self.preDecrypt.setToolTip('Select pre-decrypt') for encode in ENCODE: self.postProcess.addItem(encode, encode) self.preDecrypt.addItem(encode, encode) # def __connect(self): ''' Connect all components. ''' # self.linePasswordL.textChanged.connect(self.onLeftTextChanged) self.leftText.textChanged.connect(self.onLeftTextChanged) self.checkPwdL.stateChanged.connect(lambda x: \ self.linePasswordL.setEchoMode(QtGui.QLineEdit.Normal) if self.checkPwdL.isChecked() \ else self.linePasswordL.setEchoMode(QtGui.QLineEdit.Password)) self.buttonCryptMode.clicked.connect(self.onCryptMode) # self.linePasswordR.textChanged.connect(self.onRightTextChanged) self.rightText.textChanged.connect(self.onRightTextChanged) self.checkPwdR.stateChanged.connect(lambda x: \ self.linePasswordR.setEchoMode(QtGui.QLineEdit.Normal) if self.checkPwdR.isChecked() \ else self.linePasswordR.setEchoMode(QtGui.QLineEdit.Password)) self.buttonDecryptMode.clicked.connect(self.onDecryptMode) # self.buttonBrowseRSAL.clicked.connect(self.browseRSAkey) self.buttonBrowseRSAR.clicked.connect(self.browseRSAkey) # self.preProcess.currentIndexChanged.connect(self.onLeftTextChanged) self.comboCrypt.currentIndexChanged.connect(self.onLeftTextChanged) self.postProcess.currentIndexChanged.connect(self.onLeftTextChanged) # self.preDecrypt.currentIndexChanged.connect(self.onRightTextChanged) self.comboDecrypt.currentIndexChanged.connect(self.onRightTextChanged) self.postDecrypt.currentIndexChanged.connect(self.onRightTextChanged) # self.saveFile.clicked.connect(self.onSave) self.loadFile.clicked.connect(self.onLoad) self.helpButton.clicked.connect(self.onHelp) self.setFormatting.toggled.connect(self.onLeftTextChanged) self.setTags.toggled.connect(self.onLeftTextChanged) self.showHTML.toggled.connect(self.toggleHtml) # self.minButton.clicked.connect( lambda: self.setWindowState(QtCore.Qt.WindowMinimized)) self.closeButton.clicked.connect(lambda: self.close()) # # ACTION ! self.onCryptMode() # def dragEnterEvent(self, e): # mime_data = e.mimeData() # Accept plain text, HTML text and file paths. if mime_data.hasHtml() or mime_data.hasText() or mime_data.hasFormat( 'text/uri-list'): e.accept() else: e.ignore() # def dropEvent(self, e): # mime_data = e.mimeData() if mime_data.hasFormat('text/html'): dataf = mime_data.html() self.leftText.setHtml(dataf) elif mime_data.hasFormat('text/plain'): dataf = mime_data.text() self.leftText.setPlainText(dataf) elif mime_data.hasFormat('text/uri-list'): uri = mime_data.data('text/uri-list') uris = str(uri).split('\r\n')[:-1] # List of dragged files. for url in uris: # f_name = urllib.unquote(url) # Ignore windows shortcuts. They are ugly :) if os.path.splitext(f_name)[1].lower() == '.lnk': continue o = urllib.urlopen(url) sz = os.fstat(o.fileno())[6] t = tarfile.TarInfo(os.path.split(f_name)[1]) t.size = sz #self.attach.addFile(t, o) #? # print('I should encrypt on DROP!') # def browseRSAkey(self): # f = QtGui.QFileDialog() path = f.getOpenFileName(self, 'Path to RSA public or private key', os.getcwd(), 'All files (*.*)') if not path: return # self.SE.rsa_path = path self.lineRSAPathL.setText(path) self.lineRSAPathR.setText(path) # def onCryptMode(self): # self.buttonCryptMode.setChecked(True) self.buttonCryptMode.setText('Encrypt Mode is Enabled') self.buttonDecryptMode.setChecked(False) self.buttonDecryptMode.setText('Decrypt Mode') # self.linePasswordL.setDisabled(False) self.lineRSAPathL.setDisabled(False) self.buttonBrowseRSAL.setDisabled(False) self.leftText.setDisabled(False) # self.linePasswordR.setDisabled(True) self.lineRSAPathR.setDisabled(True) self.buttonBrowseRSAR.setDisabled(True) self.rightText.setDisabled(True) # self.checkPwdL.setDisabled(False) self.checkPwdR.setDisabled(True) # self.preProcess.setCurrentIndex(self.postDecrypt.currentIndex()) self.comboCrypt.setCurrentIndex(self.comboDecrypt.currentIndex()) self.postProcess.setCurrentIndex(self.preDecrypt.currentIndex()) # def onDecryptMode(self): # self.buttonCryptMode.setChecked(False) self.buttonCryptMode.setText('Encrypt Mode') self.buttonDecryptMode.setChecked(True) self.buttonDecryptMode.setText('Decrypt Mode is Enabled') # self.linePasswordL.setDisabled(True) self.lineRSAPathL.setDisabled(True) self.buttonBrowseRSAL.setDisabled(True) self.leftText.setDisabled(True) # self.linePasswordR.setDisabled(False) self.lineRSAPathR.setDisabled(False) self.buttonBrowseRSAR.setDisabled(False) self.rightText.setDisabled(False) # self.checkPwdL.setDisabled(True) self.checkPwdR.setDisabled(False) # self.postDecrypt.setCurrentIndex(self.preProcess.currentIndex()) self.comboDecrypt.setCurrentIndex(self.comboCrypt.currentIndex()) self.preDecrypt.setCurrentIndex(self.postProcess.currentIndex()) # def cleanupHtml(self, txt): # def spacerepl(matchobj): return matchobj.group(0).replace(b' ', b' ') # Replacing all spaces with txt = re.sub(b'>([^<>]+)<(?!/style>)', spacerepl, txt) # Write the new file open('doc.htm', 'w').write(txt) # Process the file with Tidy if platform.uname()[0].lower() == 'windows': p = subprocess.Popen( ['tidy.exe', '-config', 'tidy.txt', 'doc.htm']).wait() elif platform.uname()[0].lower() == 'linux': env = os.environ env.update({'LD_LIBRARY_PATH': os.getcwd()}) p = subprocess.Popen(['./tidy', '-config', 'tidy.txt', 'doc.htm'], env=env).wait() else: print('Platform `%s` is not supported yet!\n' % platform.uname()[0]) txt = open('doc.htm', 'r').read() # Delete the wrong/ obsolete tags txt = txt.replace( b'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"\n"http://www.w3.org/TR/html4/loose.dtd">\n', '') txt = txt.replace( b'<meta name="generator" content="HTML Tidy for Windows (vers 25 March 2009), see www.w3.org">\n', '') txt = txt.replace( b'<meta name="generator" content="HTML Tidy for Linux/x86 (vers 25 March 2009), see www.w3.org">\n', '') txt = txt.replace( b'<meta name="generator" content="HTML Tidy for Linux (vers 25 March 2009), see www.w3.org">\n', '') txt = txt.replace(b'<meta name="qrichtext" content="1">\n', '') txt = txt.replace(b'<title></title>\n', '') txt = txt.replace(b'</style>\n\n<style type="text/css">\n', '') txt = txt.replace(b'<br>\n', '\n') # The clean file, for debug... open('doc.htm', 'w').write(txt) return txt # def toggleHtml(self): # if self.showHTML.isChecked(): vDlg = QtGui.QDialog(self.centralWidget) vDlg.setMinimumSize(C['W_WIDTH'], C['W_HEIGHT'] - 20) vDlg.text = QtGui.QPlainTextEdit( self.cleanupHtml(self.leftText.toHtml().encode('latin1')), vDlg) vDlg.btnSave = QtGui.QPushButton('Save', vDlg) vDlg.btnSave.clicked.connect(lambda: vDlg.accept()) vDlg.btnCancel = QtGui.QPushButton('Cancel', vDlg) vDlg.btnCancel.clicked.connect(lambda: vDlg.reject()) layout = QtGui.QGridLayout(vDlg) layout.addWidget(vDlg.text, 1, 1, 2, 2) layout.addWidget(vDlg.btnSave, 3, 1) layout.addWidget(vDlg.btnCancel, 3, 2) vDlg.setLayout(layout) vRes = vDlg.exec_() if vRes: self.leftText.setHtml(vDlg.text.toPlainText().encode('latin1')) self.showHTML.setChecked(False) # def onLeftTextChanged(self): # if not self.buttonCryptMode.isChecked(): return # # Save all pre/enc/post operations. pre = self.preProcess.currentText() enc = self.comboCrypt.currentText() post = self.postProcess.currentText() # # If encryption mode is RSA, reveal key path. if enc == 'RSA': self.lineRSAPathL.show() self.lineRSAPathR.show() self.buttonBrowseRSAL.show() self.buttonBrowseRSAR.show() else: self.lineRSAPathL.hide() self.lineRSAPathR.hide() self.buttonBrowseRSAL.hide() self.buttonBrowseRSAR.hide() # if not self.leftText.toPlainText(): self.rightText.clear() return # pwd = self.linePasswordL.text().encode() tags = not self.setTags.isChecked() # if self.setFormatting.isChecked(): # HTML string. txt = self.leftText.toHtml().encode('utf-8') # Cleanup HTML string. txt = self.cleanupHtml(txt) else: txt = self.leftText.toPlainText().encode('utf-8') # self.postDecrypt.setCurrentIndex(self.preProcess.currentIndex()) self.comboDecrypt.setCurrentIndex(self.comboCrypt.currentIndex()) self.preDecrypt.setCurrentIndex(self.postProcess.currentIndex()) # # Encrypt the text. final = self.SE.encrypt(txt, pre, enc, post, pwd, tags) # if final: self.rightText.setPlainText(final) if self.setFormatting.isChecked(): self.nrLettersL.setText('Html: %i' % len(txt)) else: self.nrLettersL.setText('Text: %i' % len(txt)) self.nrLettersR.setText('Enc: %i' % len(final)) self.textBar.setStyleSheet(D['TXT_BAR_OK']) self.textBar.setText( ' Encryption mode step 1: %s , step 2: %s , step 3: %s' % (pre, enc, post)) else: self.rightText.clear() self.textBar.setStyleSheet(D['TXT_BAR_BAD']) self.textBar.setText(self.SE.error) # def onRightTextChanged(self): # if not self.buttonDecryptMode.isChecked(): return # txt = self.rightText.toPlainText().encode() # try: re_groups = re.search(NO_TAGS, txt).groups() tags = findg(re_groups) # If Json. if tags.startswith('"pre"'): pre = 'Json' enc = re.search('"enc":"([0-9a-zA-Z ]{1,3})"', tags).group(1) post = re.search('"pre":"([0-9a-zA-Z ]{1,3})"', tags).group(1) # If XML. elif tags.startswith('<pre>'): pre = 'XML' enc = re.search('<enc>([0-9a-zA-Z ]{1,3})</enc>', tags).group(1) post = re.search('<pre>([0-9a-zA-Z ]{1,3})</pre>', tags).group(1) else: pre = None enc = None post = None # Identify the rest. if not pre: pre = tags.split(':')[2] if not enc: enc = tags.split(':')[1] if not post: post = tags.split(':')[0] pre = pre.decode() enc = enc.decode() post = post.decode() self.postDecrypt.setCurrentIndex( self.postDecrypt.findText( post, QtCore.Qt.MatchFlag(QtCore.Qt.MatchContains))) self.comboDecrypt.setCurrentIndex( self.comboDecrypt.findText( enc, QtCore.Qt.MatchFlag(QtCore.Qt.MatchContains))) self.preDecrypt.setCurrentIndex( self.preDecrypt.findText( pre, QtCore.Qt.MatchFlag(QtCore.Qt.MatchContains))) except: pass # # This must be right here. pre = self.preDecrypt.currentText() enc = self.comboDecrypt.currentText() post = self.postDecrypt.currentText() pwd = self.linePasswordR.text().encode() # # If encryption mode is RSA, reveal key path. if enc == 'RSA': self.lineRSAPathL.show() self.lineRSAPathR.show() self.buttonBrowseRSAL.show() self.buttonBrowseRSAR.show() else: self.lineRSAPathL.hide() self.lineRSAPathR.hide() self.buttonBrowseRSAL.hide() self.buttonBrowseRSAR.hide() # if not txt: self.leftText.clear() return # self.preProcess.setCurrentIndex(self.postDecrypt.currentIndex()) self.comboCrypt.setCurrentIndex(self.comboDecrypt.currentIndex()) self.postProcess.setCurrentIndex(self.preDecrypt.currentIndex()) # # Decrypt the text. final = self.SE.decrypt(txt, pre, enc, post, pwd) # if final: # Setup string as HTML. self.leftText.setHtml(final.decode()) self.nrLettersL.setText('Dec: %i' % len(final)) self.nrLettersR.setText('Enc: %i' % len(txt)) self.textBar.setStyleSheet(D['TXT_BAR_OK']) self.textBar.setText( ' Decryption mode step 1: %s , step 2: %s , step 3: %s' % (pre, enc, post)) else: self.leftText.clear() self.textBar.setStyleSheet(D['TXT_BAR_BAD']) self.textBar.setText(self.SE.error) # def onSave(self): # # Save all pre/enc/post operations. pre = self.preProcess.currentText() enc = self.comboCrypt.currentText() post = self.postProcess.currentText() # f = QtGui.QFileDialog() if post in ['Base64 Codec', 'Base32 Codec', 'HEX Codec']: path = f.getSaveFileName(self, 'Save crypted text', os.getcwd(), 'PNG Images (*.png)') ext = '.png' elif post == 'XML': path = f.getSaveFileName(self, 'Save crypted text', os.getcwd(), 'XML Files (*.xml)') ext = '.xml' elif post == 'Json': path = f.getSaveFileName(self, 'Save crypted text', os.getcwd(), 'Json Files (*.json)') ext = '.json' else: path = f.getSaveFileName(self, 'Save crypted text', os.getcwd(), 'All files (*.*)') ext = '' if not path: return # # Save password. pwd = self.linePasswordL.text() # Text from rigth side. txt = self.rightText.toPlainText() # File extension. if not os.path.splitext(path)[1]: path += ext # # For PNG files. if ext == '.png': self.SE.toImage(txt, pre, enc, post, pwd, path, encrypt=False) else: open(path, 'w').write(txt) # def onLoad(self): # f = QtGui.QFileDialog() path = f.getOpenFileName(self, 'Load crypted text', os.getcwd(), 'All files (*.*)') if not path: return # Import the text from file, without decryption. val = self.SE._import(pre=None, enc=None, post=None, pwd=None, fpath=path, decrypt=False) if val: # For step 1. self.preProcess.setCurrentIndex( self.preProcess.findText( self.SE.post, QtCore.Qt.MatchFlag(QtCore.Qt.MatchFixedString))) self.postDecrypt.setCurrentIndex( self.postDecrypt.findText( self.SE.post, QtCore.Qt.MatchFlag(QtCore.Qt.MatchFixedString))) # For step 2. self.comboCrypt.setCurrentIndex( self.comboCrypt.findText( self.SE.enc, QtCore.Qt.MatchFlag(QtCore.Qt.MatchFixedString))) self.comboDecrypt.setCurrentIndex( self.comboDecrypt.findText( self.SE.enc, QtCore.Qt.MatchFlag(QtCore.Qt.MatchFixedString))) # For step 3. self.postProcess.setCurrentIndex( self.postProcess.findText( self.SE.pre, QtCore.Qt.MatchFlag(QtCore.Qt.MatchFixedString))) self.preDecrypt.setCurrentIndex( self.preDecrypt.findText( self.SE.pre, QtCore.Qt.MatchFlag(QtCore.Qt.MatchFixedString))) self.rightText.setPlainText(val) self.onDecryptMode() self.onRightTextChanged() self.rightText.setFocus() # def onHelp(self): # QtGui.QMessageBox.about( self.centralWidget, 'Scrambled Egg %s Help' % __version__, '<br><b>Copyright (C) 2010-2012</b> : Cristi Constantin. All rights reserved.<br>' '<b>Website</b> : http://scrambled-egg.googlecode.com/<br><br>' 'Scrambled-Egg is a software designed for encrypting your sensitive data.<br>' 'This is done in <font color="blue"><b>3 steps</b></font> : <i>pre encryption</i>, <i>encryption</i>, and <i>post encryption</i>.<br>' 'The input data can be : plain text, or formatted text (Microsoft Office or HTML).<br><br>' '<font color="blue"><b>Step 1</b></font> can compress your data using <b>ZLIB</b>, or <b>BZ2</b>. This step is optional.<br>' '<font color="blue"><b>Step 2</b></font> is the real encryption, for example with <b>AES</b>, or <b>Blowfish</b>. ' 'The password is used only in this step. ' 'For <b>RSA</b> encryption, along with the password, you have to type the path to the public or private RSA key.<br>' '<font color="blue"><b>Step 3</b></font> will encode your data. This step is required, the rest are optional. ' 'There are a lot of encodings available, for example <b>Base64</b>, or <b>HEX</b>.<br><br>' 'This FREE program is distributed in the hope that it will be useful, but without any warranty.<br><br>' 'Enjoy!')