User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Nov 2006
Posts: 23
Reputation: Haze is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Haze Haze is offline Offline
Newbie Poster

Re: Very basic de/encrypt program

  #11  
Dec 4th, 2006
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:.

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.
Reply With Quote  
Join Date: Nov 2006
Posts: 23
Reputation: Haze is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Haze Haze is offline Offline
Newbie Poster

Re: Very basic de/encrypt program

  #12  
Dec 4th, 2006
the edit button seems to disapper a while after you reply but anyway i also realised the 0,length is overcomplicated as it starts at 0 anyway so it should just be length
Reply With Quote  
Join Date: Nov 2006
Posts: 23
Reputation: Haze is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Haze Haze is offline Offline
Newbie Poster

Re: Very basic de/encrypt program

  #13  
Dec 4th, 2006
got it working heres the code.

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_f

and the decrypt is the same except with - 1. now to make it more complicated :cheesy:
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: Very basic de/encrypt program

  #14  
Dec 5th, 2006
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
Reply With Quote  
Join Date: Oct 2006
Posts: 123
Reputation: LaMouche is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 15
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Very basic de/encrypt program

  #15  
Dec 5th, 2006
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:

>>> 
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.
Reply With Quote  
Join Date: Apr 2006
Posts: 137
Reputation: ghostdog74 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 26
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Very basic de/encrypt program

  #16  
Dec 5th, 2006
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
Reply With Quote  
Join Date: Apr 2005
Location: Old Hampshire, Old England (LOL)
Posts: 11,937
Reputation: jbennet is a jewel in the rough jbennet is a jewel in the rough jbennet is a jewel in the rough jbennet is a jewel in the rough 
Rep Power: 30
Solved Threads: 264
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Microsoft Fanboy

Re: Very basic de/encrypt program

  #17  
Dec 5th, 2006
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
TRY MY SUGGESTIONS AT YOUR OWN RISK!
james.bennet1@ntlworld.com
Reply With Quote  
Join Date: Nov 2006
Posts: 23
Reputation: Haze is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Haze Haze is offline Offline
Newbie Poster

Re: Very basic de/encrypt program

  #18  
Dec 5th, 2006
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 .
Reply With Quote  
Join Date: Oct 2006
Posts: 123
Reputation: LaMouche is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 15
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Very basic de/encrypt program

  #19  
Dec 5th, 2006
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.
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: Very basic de/encrypt program

  #20  
Dec 9th, 2006
Originally Posted by Haze View Post

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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Python Marketplace
Thread Tools Display Modes

Other Threads in the Python Forum

All times are GMT -4. The time now is 5:34 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC