•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 391,810 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,500 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Views: 3915 | Replies: 19 | Solved
![]() |
•
•
Join Date: Nov 2006
Posts: 23
Reputation:
Rep Power: 2
Solved Threads: 0
This is what i have come up with for the encryption i plan to add things to it but that will be after it works :rolleyes:.
i added in the print temp to see if it was working to change the b into a c up until i get the error. I t does print out the blah and the c after that so it works up until this error.
Traceback (most recent call last):
File "C:/Python25/Decrypt_Encrypt2.py", line 15, in <module>
print encrypt('blah')
File "C:/Python25/Decrypt_Encrypt2.py", line 12, in encrypt
text_f[position - 1: position] = temp
TypeError: 'str' object doesn't support slice assignment
with the main line being 'str' object doesn't support slice assignment.
i tried to see if it was talking about temp so i forced it to be a integer replacing temp with int(temp) but that gave me this error.
text_f[position - 1: position] = int(temp)
ValueError: invalid literal for int() with base 10: 'c'
the rest of the error is the same as the one above. any help would be great.
edit: and i just realised that position position - 1 thing is overcomplciated becuase i can just do text_f[position] etc i thought you could only do that with lists :/
def encrypt(text_f):
length = len(text_f)
for position in range (1,length):
temp = ord(text_f[position - 1: position])
temp = temp + 1
temp = chr(temp)
print temp
text_f[position - 1: position] = temp
print 'blah'
print encrypt('blah')i added in the print temp to see if it was working to change the b into a c up until i get the error. I t does print out the blah and the c after that so it works up until this error.
Traceback (most recent call last):
File "C:/Python25/Decrypt_Encrypt2.py", line 15, in <module>
print encrypt('blah')
File "C:/Python25/Decrypt_Encrypt2.py", line 12, in encrypt
text_f[position - 1: position] = temp
TypeError: 'str' object doesn't support slice assignment
with the main line being 'str' object doesn't support slice assignment.
i tried to see if it was talking about temp so i forced it to be a integer replacing temp with int(temp) but that gave me this error.
text_f[position - 1: position] = int(temp)
ValueError: invalid literal for int() with base 10: 'c'
the rest of the error is the same as the one above. any help would be great.
edit: and i just realised that position position - 1 thing is overcomplciated becuase i can just do text_f[position] etc i thought you could only do that with lists :/
Last edited by Haze : Dec 4th, 2006 at 1:05 pm.
•
•
Join Date: Nov 2006
Posts: 23
Reputation:
Rep Power: 2
Solved Threads: 0
got it working heres the code.
and the decrypt is the same except with - 1. now to make it more complicated :cheesy:
def encrypt(text_f):
length = len(text_f)
for position in range (length):
temp = ord(text_f[position])
temp = temp + 1
temp = chr(temp)
text_f = text_f[:position] + temp + text_f[position + 1:]
return text_fand the decrypt is the same except with - 1. now to make it more complicated :cheesy:
•
•
Join Date: Jul 2006
Posts: 562
Reputation:
Rep Power: 4
Solved Threads: 72
Good job!
I like especially that you turned it into a function. You could now extend that a bit to accomplish encrypting and decrypting at once: have 'encrypt' accept an argument 'displace' (or some better name) that indicates how many letters up or down to translate the character. Thus, encryption would be
encrypt(text, 1)
and decryption would be
encrypt(text,-1)
Also: your z's aren't going to work yet.
Jeff
I like especially that you turned it into a function. You could now extend that a bit to accomplish encrypting and decrypting at once: have 'encrypt' accept an argument 'displace' (or some better name) that indicates how many letters up or down to translate the character. Thus, encryption would be
encrypt(text, 1)
and decryption would be
encrypt(text,-1)
Also: your z's aren't going to work yet.
Jeff
I tried it out myself and came up with the following code. It adds to a new string created in the functions to create the encoded or decoded text.
[php]
def encode(text):
encoded = ""
for letter in text:
if letter in "abcdefghijklmnopqrstuvwxy":
encoded += chr(ord(letter)+1)
elif letter == "z":
encoded += "a"
else:
encoded += letter
return encoded
def decode(text):
decoded = ""
for letter in text:
if letter in "bcdefghijklmnopqrstuvwxyz":
decoded += chr(ord(letter)-1)
elif letter == "a":
decoded += "z"
else:
decoded += letter
return decoded
s = raw_input("Enter a string to encode: ")
s_encoded = encode(s)
print '"%s" encrypts to:\n%s' % (s, s_encoded)
s_decoded = decode(s_encoded)
print '"%s" decrypts to:\n%s' % (s_encoded, s_decoded)
[/php]
Here was my successful output:
I know you seem to have mostly figured this out, but I thought I'd just give it a try.
Note: My algorithm would need to be changed in order to adapt the functions to accept varying numbers of rotation for the encryption.
[php]
def encode(text):
encoded = ""
for letter in text:
if letter in "abcdefghijklmnopqrstuvwxy":
encoded += chr(ord(letter)+1)
elif letter == "z":
encoded += "a"
else:
encoded += letter
return encoded
def decode(text):
decoded = ""
for letter in text:
if letter in "bcdefghijklmnopqrstuvwxyz":
decoded += chr(ord(letter)-1)
elif letter == "a":
decoded += "z"
else:
decoded += letter
return decoded
s = raw_input("Enter a string to encode: ")
s_encoded = encode(s)
print '"%s" encrypts to:\n%s' % (s, s_encoded)
s_decoded = decode(s_encoded)
print '"%s" decrypts to:\n%s' % (s_encoded, s_decoded)
[/php]
Here was my successful output:
>>> Enter a string to encode: I love daniweb.com! wootz! "I love daniweb.com! wootz!" encrypts to: I mpwf ebojxfc.dpn! xppua! "I mpwf ebojxfc.dpn! xppua!" decrypts to: I love daniweb.com! wootz!
I know you seem to have mostly figured this out, but I thought I'd just give it a try.
Note: My algorithm would need to be changed in order to adapt the functions to accept varying numbers of rotation for the encryption.
Last edited by LaMouche : Dec 5th, 2006 at 1:29 am.
•
•
Join Date: Apr 2006
Posts: 137
Reputation:
Rep Power: 3
Solved Threads: 26
def en_de_code(text,flag=0):
if flag == 0:
return ''.join([chr(ord(char) + 1) for char in text ]) #encode
else:
return ''.join([chr(ord(char) - 1) for char in text ]) #decode•
•
Join Date: Apr 2005
Location: Old Hampshire, Old England (LOL)
Posts: 11,937
Reputation:
Rep Power: 30
Solved Threads: 264
ive made a good vb text editor with encryption program using XOR (you enter a passphrase to open the document)
if you want the code you can have it. might be able to translate it into c or something
if you want the code you can have it. might be able to translate it into c or something
TRY MY SUGGESTIONS AT YOUR OWN RISK!
james.bennet1@ntlworld.com
james.bennet1@ntlworld.com
•
•
Join Date: Nov 2006
Posts: 23
Reputation:
Rep Power: 2
Solved Threads: 0
Ghostdog:
thats a pretty clever bit of code ill use it when i fully understand it (or else it just feels like cheating)
jrcagle:
i realised that my z's dont need to come out as a its encryption its meant to look weird.
also iam trying to work it so that it can encrypt/decrypt on the same function. allthough its slightly harder now that i made it more complicated.
lamouche:
nice to see other people doing the same thing :cheesy: and from what i understand of your code it seems good.
jbennet:
thanks for the offer but iam useless with vb and c the only things i do know are Truebasic (have to use that in school not by choice) and python and i dont even know much about either of those
.
thats a pretty clever bit of code ill use it when i fully understand it (or else it just feels like cheating)
jrcagle:
i realised that my z's dont need to come out as a its encryption its meant to look weird.
also iam trying to work it so that it can encrypt/decrypt on the same function. allthough its slightly harder now that i made it more complicated.
lamouche:
nice to see other people doing the same thing :cheesy: and from what i understand of your code it seems good.
jbennet:
thanks for the offer but iam useless with vb and c the only things i do know are Truebasic (have to use that in school not by choice) and python and i dont even know much about either of those
. Wow, ghostdog. That's short code. Talk about clean. I can understand that... the list comprehensiong stuff, but I have yet to implement it into something of my own. I don't think about using it when I'm designing something...it's just not in my brain yet.
Last edited by LaMouche : Dec 5th, 2006 at 5:57 pm.
•
•
Join Date: Jul 2006
Posts: 562
Reputation:
Rep Power: 4
Solved Threads: 72
•
•
•
•
jrcagle:
i realised that my z's dont need to come out as a its encryption its meant to look weird.
also iam trying to work it so that it can encrypt/decrypt on the same function. allthough its slightly harder now that i made it more complicated.
Actually, you'll have a hard time decrypting if your z's don't have a unique target value. Of course, that value could be any character -- such as '!'. Perhaps that's what you have in mind.
Jeff
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
- Previous Thread: Python Indentation
- Next Thread: Tkinter Window Fixed Size



Linear Mode