Enable nonce and block counts in PyCrypto AES MODE_CTR

Some background information, you can skip this part for the current issue.

This is my third question about this topic here on stackoverflow. To be complete, these are other AES issues with crypt-js and PyCrypto and Map AES de / encryption in python and javascript . Unfortunately, my last attempt received two questions for the original question. The problem is that even I did not know what my real question is. I just rummaged through to find the real question I was looking for. With reviews in the comments and reading some additional information, I updated my question. I think I’ll dig up the right question. But after my updates, my problem did not get more views. Therefore, I really hope that this question is now more clear and understandable - even I know that my problem is now: D
Thanks to everyone for doing stackoverflow in this cool community - I often found solutions to my problems here. Please continue to provide feedback on bad issues so that they can be improved and updated, which increases this vast base of knowledge and solutions. And feel free to correct my English grammar and spelling.

Problem

AES in Javascript

I have an encrypted string that I can decrypt with this this Javascript implementation of AES 256 CTR mode

password = "myPassphrase"
ciphertext = "bQJdJ1F2Y0+uILADqEv+/SCDV1jAb7jwUBWk"
origtext = Aes.Ctr.decrypt(ciphertext, password, 256);
alert(origtext)

This decrypts my string and a warning window appears with This is a test Text.

AES with PyCrypto

Now I want to decrypt this string using python and PyCrypto

password = 'myPassphrase'
ciphertext = "bQJdJ1F2Y0+uILADqEv+/SCDV1jAb7jwUBWk"
ctr = Counter.new(nbits=128)
encryptor = AES.new(key, AES.MODE_CTR, counter=ctr)
origtext = encryptor.decrypt(base64.b64decode(ciphertext))
print origtext

. ValueError: AES key must be either 16, 24, or 32 bytes long. , PyCrypto, , , , .

, , :

  • AES 256 (?). AES - 128 . 32 ?
  • . PyCrypto AES.MODE_CTR. counter(). , PyCrypto. Javascript? , .
  • base64. .
  • . , .

:

for (var i=0; i<nBytes; i++) {
    pwBytes[i] = isNaN(password.charCodeAt(i)) ? 0 : password.charCodeAt(i);
}

python

l = 32
key = key + (chr(0)*(l-len(key)%l))

. ? A???B??d9= ,?h????'

l = 32
key = 'myPassphrase'
key = key + (chr(0)*(l-len(key)%l))
ciphertext = "bQJdJ1F2Y0+uILADqEv+/SCDV1jAb7jwUBWk"
ctr = Counter.new(nbits=128)
encryptor = AES.new(key, AES.MODE_CTR, counter=ctr)
origtext = encryptor.decrypt(base64.b64decode(ciphertext))
print origtext

Javascript,

[...] nonce 8 , 8 . [...]

, . , , Javascript:

origtext = ""
var ciphertext =Aes.Ctr.encrypt(origtext, password, 256);
alert(ciphertext)

/gEKb+N3Y08= (12 ). 12? 8 + 8 = 16Bytes? , bruteforce python, for i in xrange(0,20): ciphertext[i:] base64.b64decode(ciphertext)[i:]. , , . .

.

Javascript, . , Javascript . , - "".

, PyCrypto , Javascript, Javascript Python? python, . , .

, nonce block ? ?

+5
1

.

nonce ?

. Javascript ( ) 8- nonce . Python :

import base64
from_js_bin = base64.decode(from_js)
nonce = from_js_bin[:8]
ciphertext = from_js_bin[8:]

from_js - , .

, JS . ( ) 0.

nonce counter Python?

-, , nonce , . , JS NIST 800-38A, - nonce, - . , (LSB - ). : AES CTR mode.

, CTR PyCrypto (a ). , counter , 16 ( AES) . Crypto.Util.Counter , .

. :

from Crypto.Cipher import AES
import struct

class MyCounter:

  def __init__(self, nonce):
    """Initialize the counter object.

    @nonce      An 8 byte binary string.
    """
    assert(len(nonce)==8)
    self.nonce = nonce
    self.cnt = 0

  def __call__(self):
    """Return the next 16 byte counter, as binary string."""
    righthalf = struct.pack('>Q',self.cnt)
    self.cnt += 1
    return self.nonce + righthalf

cipher_ctr = AES.new(key, mode=AES.MODE_CTR, counter=MyCounter(nonce))
plaintext = cipher_ctr.decrypt(ciphertext)

AES?

AES-128 16 . AES-192 24 . AES-256 32 . , . 16- . AES-128 (nBits=128).

?

, , AES . JS- UTF-8 . . 16 , AES-192 -256 . , UTF-8 .

, :

  • JS- ( , ;-)).
  • ( ).
  • Python .
  • , .

Python, .

+6

All Articles