Input strings must be a multiple of 16 in length ( i have done the padding )
import os,sys,ctypes,struct,random
from getpass import *
from Crypto.Cipher import AES
from Crypto import Random
user = getuser()
extensions = ('.png')
paths = ['C:\Users\' + user + '\Desktop\Test']
Key = Random.new().read(AES.block_size)
def encryption ( Key,in_filename , out_filename=None , chuncksize =64*1024):
if not out_filename:
out_filename = in_filename + '.wbi'
IV = Random.new().read(AES.block_size)
encryptor = AES.new(Key,AES.MODE_CBC, IV)
filesize = os.path.getsize(in_filename)
with open (in_filename,'r', errors='ignore') as infile:
with open (out_filename,'wb') as outfile:
outfile.write(struct.pack('<Q', filesize))
outfile.write(IV)
while True:
chunck = infile.read(chuncksize)
if (len(chunck)==0):
break
elif (len(chunck)%16!=0):
chunck+= ' ' * (16-len(chunck)%16)
#chunck+= ' '*(16-len(chunck)%16)
outfile.write(encryptor.encrypt(chunck))
def file_enc ():
for path in paths:
for root,dirs,files in os.walk(path):
for file in files:
if(file.endswith(extensions)):
encryption(Key, os.path.join(root,file))
os.remove(os.path.join(root,file))
file_enc ()
- OUTPUT : return self._cipher.encrypt(plaintext)
ValueError: Input
strings must be a multiple of 16 in length
i have done the padding as u can see here :
elif (len(chunck)%16!=0):
chunck+= ' ' * (16-len(chunck)%16)
Traceback:
Traceback (most recent call last):
File "C:UsersIm Root BicthesDesktopaes encryption done right.py", line 41, in <module>
file_enc ()
File "C:UsersIm Root BicthesDesktopaes encryption done right.py", line 37, in file_enc
encryption(Key, os.path.join(root,file))
File "C:UsersIm Root BicthesDesktopaes encryption done right.py", line 30, in encryption
outfile.write(encryptor.encrypt(chunck))
File "C:Python32libsite-packagesCryptoCipherblockalgo.py", line 244, in encrypt
return self._cipher.encrypt(plaintext)
ValueError: Input strings must be a multiple of 16 in length
but still i dont know how to fix it any help will be appreciated
using python 3.2.5
python aes
add a comment |
import os,sys,ctypes,struct,random
from getpass import *
from Crypto.Cipher import AES
from Crypto import Random
user = getuser()
extensions = ('.png')
paths = ['C:\Users\' + user + '\Desktop\Test']
Key = Random.new().read(AES.block_size)
def encryption ( Key,in_filename , out_filename=None , chuncksize =64*1024):
if not out_filename:
out_filename = in_filename + '.wbi'
IV = Random.new().read(AES.block_size)
encryptor = AES.new(Key,AES.MODE_CBC, IV)
filesize = os.path.getsize(in_filename)
with open (in_filename,'r', errors='ignore') as infile:
with open (out_filename,'wb') as outfile:
outfile.write(struct.pack('<Q', filesize))
outfile.write(IV)
while True:
chunck = infile.read(chuncksize)
if (len(chunck)==0):
break
elif (len(chunck)%16!=0):
chunck+= ' ' * (16-len(chunck)%16)
#chunck+= ' '*(16-len(chunck)%16)
outfile.write(encryptor.encrypt(chunck))
def file_enc ():
for path in paths:
for root,dirs,files in os.walk(path):
for file in files:
if(file.endswith(extensions)):
encryption(Key, os.path.join(root,file))
os.remove(os.path.join(root,file))
file_enc ()
- OUTPUT : return self._cipher.encrypt(plaintext)
ValueError: Input
strings must be a multiple of 16 in length
i have done the padding as u can see here :
elif (len(chunck)%16!=0):
chunck+= ' ' * (16-len(chunck)%16)
Traceback:
Traceback (most recent call last):
File "C:UsersIm Root BicthesDesktopaes encryption done right.py", line 41, in <module>
file_enc ()
File "C:UsersIm Root BicthesDesktopaes encryption done right.py", line 37, in file_enc
encryption(Key, os.path.join(root,file))
File "C:UsersIm Root BicthesDesktopaes encryption done right.py", line 30, in encryption
outfile.write(encryptor.encrypt(chunck))
File "C:Python32libsite-packagesCryptoCipherblockalgo.py", line 244, in encrypt
return self._cipher.encrypt(plaintext)
ValueError: Input strings must be a multiple of 16 in length
but still i dont know how to fix it any help will be appreciated
using python 3.2.5
python aes
please reformat your code and errors properly and give a bit more of context.
– LoneWanderer
Nov 25 '18 at 2:00
Provide the full traceback, not just the error message alone; the traceback is needed to show where the problem is in your code.
– ShadowRanger
Nov 25 '18 at 2:07
Side-note: If this is Python 3, you shouldn't be reading your file in text mode. The encryptor should either explode (expectingbytes
) or it might seamlessly encode the text, but if it encodes it in a variable width encoding like UTF-8, you might end up with a multiple of 16 growing to a non-multiple. Work with rawbytes
at all times, notstr
.
– ShadowRanger
Nov 25 '18 at 2:12
On checking the source code where it's going wrong, I'm more sure that passingstr
to the API is wrong. You need to passbytes
, so open your file in'rb'
mode, pad withb' '
, not' '
, etc.
– ShadowRanger
Nov 25 '18 at 2:23
Another side note: Using AES in CBC mode with no MAC is not "encryption done right". Please see: en.wikipedia.org/wiki/Authenticated_encryption
– duskwuff
Nov 25 '18 at 3:11
add a comment |
import os,sys,ctypes,struct,random
from getpass import *
from Crypto.Cipher import AES
from Crypto import Random
user = getuser()
extensions = ('.png')
paths = ['C:\Users\' + user + '\Desktop\Test']
Key = Random.new().read(AES.block_size)
def encryption ( Key,in_filename , out_filename=None , chuncksize =64*1024):
if not out_filename:
out_filename = in_filename + '.wbi'
IV = Random.new().read(AES.block_size)
encryptor = AES.new(Key,AES.MODE_CBC, IV)
filesize = os.path.getsize(in_filename)
with open (in_filename,'r', errors='ignore') as infile:
with open (out_filename,'wb') as outfile:
outfile.write(struct.pack('<Q', filesize))
outfile.write(IV)
while True:
chunck = infile.read(chuncksize)
if (len(chunck)==0):
break
elif (len(chunck)%16!=0):
chunck+= ' ' * (16-len(chunck)%16)
#chunck+= ' '*(16-len(chunck)%16)
outfile.write(encryptor.encrypt(chunck))
def file_enc ():
for path in paths:
for root,dirs,files in os.walk(path):
for file in files:
if(file.endswith(extensions)):
encryption(Key, os.path.join(root,file))
os.remove(os.path.join(root,file))
file_enc ()
- OUTPUT : return self._cipher.encrypt(plaintext)
ValueError: Input
strings must be a multiple of 16 in length
i have done the padding as u can see here :
elif (len(chunck)%16!=0):
chunck+= ' ' * (16-len(chunck)%16)
Traceback:
Traceback (most recent call last):
File "C:UsersIm Root BicthesDesktopaes encryption done right.py", line 41, in <module>
file_enc ()
File "C:UsersIm Root BicthesDesktopaes encryption done right.py", line 37, in file_enc
encryption(Key, os.path.join(root,file))
File "C:UsersIm Root BicthesDesktopaes encryption done right.py", line 30, in encryption
outfile.write(encryptor.encrypt(chunck))
File "C:Python32libsite-packagesCryptoCipherblockalgo.py", line 244, in encrypt
return self._cipher.encrypt(plaintext)
ValueError: Input strings must be a multiple of 16 in length
but still i dont know how to fix it any help will be appreciated
using python 3.2.5
python aes
import os,sys,ctypes,struct,random
from getpass import *
from Crypto.Cipher import AES
from Crypto import Random
user = getuser()
extensions = ('.png')
paths = ['C:\Users\' + user + '\Desktop\Test']
Key = Random.new().read(AES.block_size)
def encryption ( Key,in_filename , out_filename=None , chuncksize =64*1024):
if not out_filename:
out_filename = in_filename + '.wbi'
IV = Random.new().read(AES.block_size)
encryptor = AES.new(Key,AES.MODE_CBC, IV)
filesize = os.path.getsize(in_filename)
with open (in_filename,'r', errors='ignore') as infile:
with open (out_filename,'wb') as outfile:
outfile.write(struct.pack('<Q', filesize))
outfile.write(IV)
while True:
chunck = infile.read(chuncksize)
if (len(chunck)==0):
break
elif (len(chunck)%16!=0):
chunck+= ' ' * (16-len(chunck)%16)
#chunck+= ' '*(16-len(chunck)%16)
outfile.write(encryptor.encrypt(chunck))
def file_enc ():
for path in paths:
for root,dirs,files in os.walk(path):
for file in files:
if(file.endswith(extensions)):
encryption(Key, os.path.join(root,file))
os.remove(os.path.join(root,file))
file_enc ()
- OUTPUT : return self._cipher.encrypt(plaintext)
ValueError: Input
strings must be a multiple of 16 in length
i have done the padding as u can see here :
elif (len(chunck)%16!=0):
chunck+= ' ' * (16-len(chunck)%16)
Traceback:
Traceback (most recent call last):
File "C:UsersIm Root BicthesDesktopaes encryption done right.py", line 41, in <module>
file_enc ()
File "C:UsersIm Root BicthesDesktopaes encryption done right.py", line 37, in file_enc
encryption(Key, os.path.join(root,file))
File "C:UsersIm Root BicthesDesktopaes encryption done right.py", line 30, in encryption
outfile.write(encryptor.encrypt(chunck))
File "C:Python32libsite-packagesCryptoCipherblockalgo.py", line 244, in encrypt
return self._cipher.encrypt(plaintext)
ValueError: Input strings must be a multiple of 16 in length
but still i dont know how to fix it any help will be appreciated
using python 3.2.5
python aes
python aes
edited Nov 25 '18 at 2:17
ShadowRanger
61.1k55796
61.1k55796
asked Nov 25 '18 at 1:53
Skon HouasSkon Houas
13
13
please reformat your code and errors properly and give a bit more of context.
– LoneWanderer
Nov 25 '18 at 2:00
Provide the full traceback, not just the error message alone; the traceback is needed to show where the problem is in your code.
– ShadowRanger
Nov 25 '18 at 2:07
Side-note: If this is Python 3, you shouldn't be reading your file in text mode. The encryptor should either explode (expectingbytes
) or it might seamlessly encode the text, but if it encodes it in a variable width encoding like UTF-8, you might end up with a multiple of 16 growing to a non-multiple. Work with rawbytes
at all times, notstr
.
– ShadowRanger
Nov 25 '18 at 2:12
On checking the source code where it's going wrong, I'm more sure that passingstr
to the API is wrong. You need to passbytes
, so open your file in'rb'
mode, pad withb' '
, not' '
, etc.
– ShadowRanger
Nov 25 '18 at 2:23
Another side note: Using AES in CBC mode with no MAC is not "encryption done right". Please see: en.wikipedia.org/wiki/Authenticated_encryption
– duskwuff
Nov 25 '18 at 3:11
add a comment |
please reformat your code and errors properly and give a bit more of context.
– LoneWanderer
Nov 25 '18 at 2:00
Provide the full traceback, not just the error message alone; the traceback is needed to show where the problem is in your code.
– ShadowRanger
Nov 25 '18 at 2:07
Side-note: If this is Python 3, you shouldn't be reading your file in text mode. The encryptor should either explode (expectingbytes
) or it might seamlessly encode the text, but if it encodes it in a variable width encoding like UTF-8, you might end up with a multiple of 16 growing to a non-multiple. Work with rawbytes
at all times, notstr
.
– ShadowRanger
Nov 25 '18 at 2:12
On checking the source code where it's going wrong, I'm more sure that passingstr
to the API is wrong. You need to passbytes
, so open your file in'rb'
mode, pad withb' '
, not' '
, etc.
– ShadowRanger
Nov 25 '18 at 2:23
Another side note: Using AES in CBC mode with no MAC is not "encryption done right". Please see: en.wikipedia.org/wiki/Authenticated_encryption
– duskwuff
Nov 25 '18 at 3:11
please reformat your code and errors properly and give a bit more of context.
– LoneWanderer
Nov 25 '18 at 2:00
please reformat your code and errors properly and give a bit more of context.
– LoneWanderer
Nov 25 '18 at 2:00
Provide the full traceback, not just the error message alone; the traceback is needed to show where the problem is in your code.
– ShadowRanger
Nov 25 '18 at 2:07
Provide the full traceback, not just the error message alone; the traceback is needed to show where the problem is in your code.
– ShadowRanger
Nov 25 '18 at 2:07
Side-note: If this is Python 3, you shouldn't be reading your file in text mode. The encryptor should either explode (expecting
bytes
) or it might seamlessly encode the text, but if it encodes it in a variable width encoding like UTF-8, you might end up with a multiple of 16 growing to a non-multiple. Work with raw bytes
at all times, not str
.– ShadowRanger
Nov 25 '18 at 2:12
Side-note: If this is Python 3, you shouldn't be reading your file in text mode. The encryptor should either explode (expecting
bytes
) or it might seamlessly encode the text, but if it encodes it in a variable width encoding like UTF-8, you might end up with a multiple of 16 growing to a non-multiple. Work with raw bytes
at all times, not str
.– ShadowRanger
Nov 25 '18 at 2:12
On checking the source code where it's going wrong, I'm more sure that passing
str
to the API is wrong. You need to pass bytes
, so open your file in 'rb'
mode, pad with b' '
, not ' '
, etc.– ShadowRanger
Nov 25 '18 at 2:23
On checking the source code where it's going wrong, I'm more sure that passing
str
to the API is wrong. You need to pass bytes
, so open your file in 'rb'
mode, pad with b' '
, not ' '
, etc.– ShadowRanger
Nov 25 '18 at 2:23
Another side note: Using AES in CBC mode with no MAC is not "encryption done right". Please see: en.wikipedia.org/wiki/Authenticated_encryption
– duskwuff
Nov 25 '18 at 3:11
Another side note: Using AES in CBC mode with no MAC is not "encryption done right". Please see: en.wikipedia.org/wiki/Authenticated_encryption
– duskwuff
Nov 25 '18 at 3:11
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464010%2finput-strings-must-be-a-multiple-of-16-in-length-i-have-done-the-padding%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464010%2finput-strings-must-be-a-multiple-of-16-in-length-i-have-done-the-padding%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
please reformat your code and errors properly and give a bit more of context.
– LoneWanderer
Nov 25 '18 at 2:00
Provide the full traceback, not just the error message alone; the traceback is needed to show where the problem is in your code.
– ShadowRanger
Nov 25 '18 at 2:07
Side-note: If this is Python 3, you shouldn't be reading your file in text mode. The encryptor should either explode (expecting
bytes
) or it might seamlessly encode the text, but if it encodes it in a variable width encoding like UTF-8, you might end up with a multiple of 16 growing to a non-multiple. Work with rawbytes
at all times, notstr
.– ShadowRanger
Nov 25 '18 at 2:12
On checking the source code where it's going wrong, I'm more sure that passing
str
to the API is wrong. You need to passbytes
, so open your file in'rb'
mode, pad withb' '
, not' '
, etc.– ShadowRanger
Nov 25 '18 at 2:23
Another side note: Using AES in CBC mode with no MAC is not "encryption done right". Please see: en.wikipedia.org/wiki/Authenticated_encryption
– duskwuff
Nov 25 '18 at 3:11