Simple encryption in python


 
Thread Tools Search this Thread
Top Forums Programming Simple encryption in python
# 8  
Old 03-21-2012
Quote:
Originally Posted by pandeesh
...
Code:
...
def Encrypt(input):
    res=''
    for i in input:
        k = len(input) + 1
        j=1
        while j < k:
          print("j is ",j)
          val = ord(i) + j
          res = res + ' ' + str(val)
          j = j + 1
          break
    return res
 
input=str(input("Enter the input string:\n"))
result=Encrypt(input)
print("The encrypted value is\n",result)

While running:
Code:
Enter the input string:
well
j is  1
j is  1
j is  1
j is  1
The encrypted value is
  120 102 109 109

But the output i expect is :
Code:
j is  1
j is  2
j is  3
j is  4
The encrypted value is
  120 103 111 112

...
Looks like your variable "j" is being reset in every loop iteration.
If you want it incremented in every iteration, then move it outside the "for" loop - either at the beginning of the function or immediately below the assignment of "res".

tyler_durden
# 9  
Old 03-22-2012
I have tried that:
Code:
def Encrypt(input):
    res=''
    j=1
    for i in input:
        k = len(input) + 1
        
        while j < k:
          print("j is ",j)
          val = ord(i) + j
          res = res + ' ' + str(val)
          j = j + 1
                  
    return res


input=str(input("Enter the input string:\n"))
result=Encrypt(input)
print("The encrypted value is\n",result)

The result is:
Code:
Enter the input string:
well
j is  1
j is  2
j is  3
j is  4
The encrypted value is
  120 121 122 123

It's not coming out from the while loop for each iteration.
Even if i use break also, i am getting the same result.,
But my expected result is :
Code:
120 103 111 112

Thanks
# 10  
Old 03-22-2012
Quote:
Originally Posted by pandeesh
I have tried that:
Code:
 
def Encrypt(input):
    res=''
    j=1
    for i in input:
        k = len(input) + 1
 
        while j < k:
          print("j is ",j)
          val = ord(i) + j
          res = res + ' ' + str(val)
          j = j + 1
 
    return res
 
 
input=str(input("Enter the input string:\n"))
result=Encrypt(input)
print("The encrypted value is\n",result)

...
No, you have not. You did move the assignment statement outside the loop, but you also removed the "break" statement.
I did not mention anything about the "break" statement.

Quote:
...
Even if i use break also, i am getting the same result.,
...
Examples please.

tyler_durden
# 11  
Old 03-22-2012
Please find the example with break:
Code:
def Encrypt(input):
    res=''
    j=1
    for i in input:
        k = len(input) + 1
        
        while j < k:
          print("j is ",j)
          val = ord(i) + j
          res = res + ' ' + str(val)
          j = j + 1
        break
        
    return res


input=str(input("Enter the input string:\n"))
result=Encrypt(input)
print("The encrypted value is\n",result)

The result is:
Code:
Enter the input string:
well
j is  1
j is  2
j is  3
j is  4
The encrypted value is
  120 121 122 123

Thanks
Which assignment you want to move outside of for loop?
Code:
j=1

is already moved before for loop.
# 12  
Old 03-22-2012
Quote:
Originally Posted by pandeesh
Please find the example with break:
Code:
def Encrypt(input):
    res=''
    j=1
    for i in input:
        k = len(input) + 1
        
        while j < k:
          print("j is ",j)
          val = ord(i) + j
          res = res + ' ' + str(val)
          j = j + 1
        break
        
    return res


input=str(input("Enter the input string:\n"))
result=Encrypt(input)
print("The encrypted value is\n",result)

Nope, that's not your original program. Go through the program you posted in your first post carefully and ensure that it is identical to the one above, except for the assignment statement i.e. j = 1.
(Hint: check the indentation of each line.)

Quote:
...
Which assignment you want to move outside of for loop?
Code:
j=1

is already moved before for loop.
Yes I know. That was mentioned in my first post itself.

tyler_durden
# 13  
Old 03-23-2012
thanks,,it worked.
# 14  
Old 03-23-2012
Quote:
Originally Posted by pandeesh
thanks,,it worked.
You are welcome.

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Simple Python Code Question

I have the following code: #!/usr/bin/env python mylist = def printWithoutNewlines(): for objects in mylist: #print(objects) objects = objects.replace('hello', "hi") print objects When executed, it gives the following output: ## ./loop.py hi... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. OS X (Apple)

Python script to do simple audio capture...

This site is the first to get this snippet. It will capture an audio recording of any time length within the limits of OSX's QuickTime Player's capablility... A shell script derivative of this will be used as a further capture for CygWin's AudioScope.sh. Thoroughly read ALL the comments in... (0 Replies)
Discussion started by: wisecracker
0 Replies

3. Cybersecurity

File encryption tools with MAC address as an encryption key

Hi all, I'm looking for secure file encryption tools that use MAC address as encryption key. FYI, I'm using Red Hat Enterprise Linux OS. For example: when A wants to send file to B A will encrypt the file with B's computer MAC/IP address as an encryption key This file can only be decrypted... (2 Replies)
Discussion started by: sergionicosta
2 Replies

4. Homework & Coursework Questions

Help with Simple Perl/Python Script

I have the following problem, which I need done in Perl/ or Python using Unix/linux filters... 1. You have a very large file, named 'ColCheckMe', tab-delmited, that you are asked to process. You are told that each line in 'ColCheckMe' has 7 columns, and that the values... (1 Reply)
Discussion started by: Swapnilsagarwal
1 Replies

5. Shell Programming and Scripting

Help with Simple Perl/Python Script

I have the following problem, which I need done in Perl/ or Python using Unix/linux filters... 1. You have a very large file, named 'ColCheckMe', tab-delmited, that you are asked to process. You are told that each line in 'ColCheckMe' has 7 columns, and that the values in the... (1 Reply)
Discussion started by: Swapnilsagarwal
1 Replies

6. Red Hat

Writing simple python setup commands

Building software in most languages is a pain. Remember ant build.xml, maven2 pom files, and multi-level makefiles? Python has a simple solution for building modules, applications, and extensions called distutils. Disutils comes as part of the Python distribution so there are no other packages... (0 Replies)
Discussion started by: Linux Bot
0 Replies
Login or Register to Ask a Question