![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
[Python] Grid encryptor
I failed to complete this in C when I was first starting out, but I think this python program does it well enough.
basically, a word is entered, like hello. The word is padded with *'s to make its length a perfect square. Therefore, input of hello is padded to form hello***. The input is then arranged into a square: h e l l o * * * * and read in reverse: hl*eo*l** """grid-encrypt - Encrypt and Decrypt text using a grid"""
import math
class GridEncrypt:
"""Encrypt text
Encrypt text using the grid method. Whereby text is converted into a square,
and returned in alternative order."""
squares = [x ** 2 for x in range(1, 101)]
def encrypt(self, word):
"""Encrypt text
Encrypt text by using a grid and return the result"""
if type(word) != type('abc'):
raise TypeError("Input string not found.")
paddedWord = self.__padWord(word)
#split up into list of lists
self.grid = []
seperator = math.sqrt(self.squareSize)
count = 1
temp = []
for x in paddedWord:
if count <= seperator:
temp.append(x)
count += 1
if count > seperator:
self.grid.append(temp)
temp = []
count = 1
self.final = []
pos = 0
for n in range(int(math.sqrt(self.squareSize))):
for row in self.grid:
self.final.append(row[n])
return "".join(self.final)
def __padWord(self, wrd):
word = [x for x in wrd]
if len(word) in GridEncrypt.squares:
self.squareSize = len(wrd)
return word
else:
#pad word with filler to make length a perfect square...
for x in GridEncrypt.squares:
if len(word) < x:
value = x - len(word)
self.squareSize = x
break
word.extend('*' * value) #word is now padded with appropriate values
return word
def decrypt(self, word):
"""Decrypt text
Decrypt text previously encrypted by the encrypt() function"""
if not (len(word) in GridEncrypt.squares):
raise ValueError("Invalid input given.")
#construct list of lists
grid, temp = ([], [])
count = 1
seperator = int(math.sqrt(len(word)))
for x in word:
if count <= seperator:
temp.append(x)
count += 1
if count > seperator:
grid.append(temp)
count = 1
temp = []
final = []
for a in range(seperator):
for row in grid:
final.append(row[a])
return "".join(final)
def main():
ge = GridEncrypt()
encrypted = ge.encrypt(raw_input("Enter message to encrypt: "))
print "\n\nText Encrypted: %s" % encrypted
print "\n\nText decrypted: %s" % ge.decrypt(encrypted)
if __name__ == '__main__':
main()
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#2 |
|
Expert Programmer
|
Text Encrypted: Nr qel.ioWuli*fgoilk*trrt.e*yake * ms Ii*p. w t*
Text decrypted: Nifty program. Works quite well. I like it.****** Edit: It would be nice in the program if the user had the option of entering encrypted text and having it decrypted. Perhaps read from a file even if the message is large enough. |
|
|
|
|
|
#3 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
T etg shIe c a' mth:nlaaha)klbkon*s oisg*.sunee*
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#4 |
|
Professional Programmer
|
Reminds me of Sandorf's cipher
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Looks nice. Sweet stuff. Got to learn that Python sometime...
![]()
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#6 | |
|
Programming Guru
![]() |
Quote:
He actually set it up so it would work just as it does for sha1, base64, md5 or any other encryption in Python. from base64 import encodestring
print base64.encodestring("code")from GridEncrypt import GridEncrypt
print GridEncrypt.encrypt("code") |
|
|
|
|
|
|
#7 | |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
Quote:
Thanks for the compliments everybody. Currently, the program works like this eg: g = GridEncrypt()
print g.encrypt("hello, world")but I'd like to change it the way Sane specified. Give me about 10 minutes :p EDIT: How does one create static methods in python like: class GridEncrypt:
def encrypt(word):
#do stuffSo it could be called like this: GridEncrypt.encrypt("hello") without creating an object?
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! Last edited by Jessehk; Feb 18th, 2006 at 3:53 PM. |
|
|
|
|
|
|
#8 | |
|
Programming Guru
![]() |
Re: [Python] Grid encryptor
>>>
Massive Bump Alert! >>> I read this thread a year ago when Jessehk made this post, but I never saw his edit. It has taken me almost two full years to find and reply to his edit. Sorry for the delay!! XD Even if he doesn't need it anymore, I bet someone can make a use of my answer. And Jessehk may find the response entertaining, who knows. This is one way to do what you asked: GridEncrypt.py class main:
def __call__(self, word):
# Do Some Stuff
return 'Encrypted ' + word
class GridEncrypt:
encrypt = main()my_script.py from GridEncrypt import GridEncrypt
print GridEncrypt.encrypt("code")Quote:
But here's the funny part. There's no real point in doing this, because it's not how the base64 module behaves. I messed up in the snippet I posted before. from base64 import encodestring
print encodestring("code")That's how it actually is. So you don't need any "static class" to mimic that behaviour. But there you go anyways. Hahaha. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|