top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

pycrypto: output is different each time for same text input?

+3 votes
369 views

I seem to have misunderstood something about the way Crypto.Cipher is supposed to work, because I'm getting unexpected results, here is my code..

import hashlib
from Crypto.Cipher import AES
from Crypto import Random

h = hashlib.new('sha256')
h.update('my key')
key = h.digest()

iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CFB, iv)
txt = 'hello world'

This is the part where I'm confused, because it seems like encrypt will output a different result every time, so how can I decrypt it?

msg = cipher.encrypt(txt)    
>>> '|sx08xf2x12xdex8cDxe7u*'

msg = cipher.encrypt(txt)
>>> 'xa1xed7xb8h
posted Oct 24, 2013 by Majula Joshi

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
 
Best answer

Code that will work:

#!/usr/bin/python3
import hashlib
from Crypto.Cipher import AES
from Crypto import Random

h = hashlib.new('sha256')
h.update(b'my key')
key = h.digest()

iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CFB, iv)
txt = 'hello world'

msg = cipher.encrypt(txt)
print(msg)

cipher = AES.new(key, AES.MODE_CFB, iv) # Use *same* IV!
origtxt = cipher.decrypt(msg)
print(origtxt)

Also note that manually deriving a symmetric secret using SHA256 is an INCREDIBLY bad idea. Have a look at PBKDF2.

answer Oct 24, 2013 by Anderson
+1 vote

You're not reinitializing the internal state of the crypto engine. When you recreate "cipher" with the same IV every time, it will work.

answer Oct 24, 2013 by Deepak Dasgupta
Similar Questions
0 votes

I wrote and compiled c code. I am using command "time a.out" . Every time output comes different.

$time ./a.out

real 0m0.002s
user 0m0.001s
sys 0m0.001s
-bash-3.2$ time ./a.out

real 0m0.001s
user 0m0.001s
sys 0m0.001s
-bash-3.2$ time ./a.out

real 0m0.001s
user 0m0.000s
sys 0m0.001s

Can someone please explain why these different different output come ?

+3 votes

Implement a code that counts the occurrences of each word in an input file and write the word along with corresponding count in an output file sorted by the words alphabetically.

Sample Input

Gaurav is  a Good Boy
Sita is a Good Girl
Tommy is  a Good Dog
Ram is a Good person

Sample Output

D:\>Java FileWordCount inputFile.txt outputFile.txt
    Boy : 1
    Dog : 1
    Gaurav : 1
    Girl : 1
    Good : 4
    Ram : 1
    Sita : 1
    Tommy : 1
    a : 4
    is : 4
    person : 1
0 votes

After a user selects a file from the form, that selection of his can be found form reading the variable 'filename'

If the filename already exists in to the database i want to update its counter and that is what i'm trying to accomplish by:

if form.getvalue('filename'):
 cur.execute('''UPDATE files SET hits = hits + 1, host = %s, lastvisit = %s WHERE url = %s''', (host, lastvisit, filename) )

For some reason this never return any data, because for troubleshooting i have tried:

data = cur.fetchone()

if data:
 print("something been returned out of this"_

Since for sure the filename the user selected is represented by a record inside 'files' table why its corresponding counter never seems to get updated?

...