{"action":"created","issue":{"url":"https://api.github.com/repos/pyca/pynacl/issues/431","repository_url":"https://api.github.com/repos/pyca/pynacl","labels_url":"https://api.github.com/repos/pyca/pynacl/issues/431/labels{/name}","comments_url":"https://api.github.com/repos/pyca/pynacl/issues/431/comments","events_url":"https://api.github.com/repos/pyca/pynacl/issues/431/events","html_url":"https://github.com/pyca/pynacl/issues/431","id":318724343,"number":431,"title":"Decryption failed. Ciphertext failed verification","user":{"login":"leobel96","id":19806799,"avatar_url":"https://avatars2.githubusercontent.com/u/19806799?v=4","gravatar_id":"","url":"https://api.github.com/users/leobel96","html_url":"https://github.com/leobel96","followers_url":"https://api.github.com/users/leobel96/followers","following_url":"https://api.github.com/users/leobel96/following{/other_user}","gists_url":"https://api.github.com/users/leobel96/gists{/gist_id}","starred_url":"https://api.github.com/users/leobel96/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/leobel96/subscriptions","organizations_url":"https://api.github.com/users/leobel96/orgs","repos_url":"https://api.github.com/users/leobel96/repos","events_url":"https://api.github.com/users/leobel96/events{/privacy}","received_events_url":"https://api.github.com/users/leobel96/received_events","type":"User","site_admin":false},"labels":[{"id":703031408,"url":"https://api.github.com/repos/pyca/pynacl/labels/documentation","name":"documentation","color":"fbca04","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2018-04-29T15:07:50Z","updated_at":"2018-04-30T22:13:33Z","closed_at":"2018-04-30T22:08:10Z","author_association":"NONE","body":"I'm pretty new with python and I'm trying to write a code to encrypt a file and one to decrypt it. I have installed the last version of pynacl and I'm using Windows 64 bit. I use a KDF function (argon2i) to derivate the key for the SecretBox from a custom password, then I encrypt the file using the key and I save all the parameters (except the password, obviously) at the beginning of the encrypted file. The encryption is very simple: I take all the parameters from the beginning of the file, I insert password to generate the Key and, then I decrypt it. The problem is that I keep obtaining this error:\r\n```\r\nTraceback (most recent call last):\r\n  File \"C:\\Users\\leobe\\Desktop\\pynacl.py\", line 36, in <module>\r\n    outfile.write(Box.decrypt(chunk,nonce))\r\n  File \"C:\\Program Files (x86)\\Python36-32\\lib\\site-packages\\nacl\\secret.py\", line 125, in decrypt\r\n    nonce, self._key)\r\n  File \"C:\\Program Files (x86)\\Python36-32\\lib\\site-packages\\nacl\\bindings\\crypto_secretbox.py\", line 76, in crypto_secretbox_open\r\n    raising=exc.CryptoError)\r\n  File \"C:\\Program Files (x86)\\Python36-32\\lib\\site-packages\\nacl\\exceptions.py\", line 68, in ensure\r\n    raise raising(*args)\r\nnacl.exceptions.CryptoError: Decryption failed. Ciphertext failed verification \r\n```\r\nIt's very strange because I've verificated that alla parameters are the same in encryption and decryption code and also derivated key and SecretBox are identical. This is the encryption code:\r\n```\r\nfrom nacl import pwhash, secret, utils\r\n\r\npassword = b'ciao'\r\ninFile = \"file.avi\"\r\noutFile = \"out.crypt\"\r\nchunksize = 64 * 1024\r\n\r\nkdf = pwhash.argon2i.kdf\r\nsalt = utils.random(pwhash.argon2i.SALTBYTES)\r\nops = pwhash.argon2i.OPSLIMIT_SENSITIVE\r\nmem = pwhash.argon2i.MEMLIMIT_SENSITIVE\r\nnonce = utils.random(secret.SecretBox.NONCE_SIZE)\r\nencodedOps = str(ops).encode(encoding='UTF-8')\r\nencodedMem = str(mem).encode(encoding='UTF-8')\r\n\r\nderivatedKey = kdf(secret.SecretBox.KEY_SIZE, password, salt, opslimit=ops, memlimit=mem)\r\nBox = secret.SecretBox(derivatedKey)\r\nprint('Box = {}'.format(Box))\r\n\r\n\r\nwith open(inFile, \"rb\") as infile:\r\n  with open(outFile, \"wb\") as outfile:\r\n    outfile.write(salt)\r\n    print('salt = {}'.format(salt))\r\n    outfile.write(encodedOps)\r\n    outfile.write(b'\\n')\r\n    print('ops = {}'.format(ops))\r\n    print ('encodedOps = {}'.format(encodedOps))\r\n    outfile.write(encodedMem)\r\n    outfile.write(b'\\n')\r\n    print('mem = {}'.format(mem))\r\n    print ('encodedMem = {}'.format(encodedMem))\r\n    outfile.write(nonce)\r\n    outfile.write(b'\\n')\r\n    print('nonce = {}'.format(nonce))\r\n    while True:\r\n      chunk = infile.read(chunksize)\r\n      if len(chunk) == 0:\r\n        break\r\n      elif len(chunk) % 16 !=0:\r\n        chunk += bytes(' ' *  (16 - (len(chunk) % 16)),'utf-8')\r\n      outfile.write(Box.encrypt(chunk, nonce))\r\n```\r\nand this is the decryption one:\r\n\r\n```\r\nfrom nacl import pwhash, secret, utils\r\n\r\npassword = b'ciao'\r\noutFile = \"file1.avi\"\r\ninFile = \"out.crypt\"\r\nchunksize = 64 * 1024\r\n\r\nkdf = pwhash.argon2i.kdf\r\n\r\nwith open(inFile, \"rb\") as infile:\r\n    with open(outFile, \"wb\") as outfile:\r\n        salt = infile.read(pwhash.argon2i.SALTBYTES)\r\n        encodedOps = infile.readline()\r\n        encodedMem = infile.readline()\r\n        nonce = infile.read(secret.SecretBox.NONCE_SIZE)\r\n        print('salt = {}'.format(salt))\r\n        print('encodedOps = {}'.format(encodedOps))\r\n        ops = int(encodedOps.decode(encoding='UTF-8'))\r\n        print('ops = {}'.format(ops))\r\n        print('encodedMem = {}'.format(encodedMem))\r\n        mem = int(encodedMem.decode(encoding='UTF-8'))\r\n        print('mem = {}'.format(mem))\r\n        print('nonce = {}'.format(nonce))\r\n        derivatedKey = kdf(secret.SecretBox.KEY_SIZE, password, salt, opslimit=ops, memlimit=mem)\r\n        print(derivatedKey)\r\n        Box = secret.SecretBox(derivatedKey)\r\n        print ('Box = {}'.format(Box))\r\n        while True:\r\n            chunk = infile.read(chunksize)\r\n            if len(chunk) == 0:\r\n                break\r\n            elif len(chunk) % 16 !=0:\r\n                chunk += bytes(' ' *  (16 - (len(chunk) % 16)),'utf-8')\r\n            outfile.write(Box.decrypt(chunk,nonce))\r\n\r\n```\r\nI know the code is not well optimized but now it only needs to work. Thank you\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/pyca/pynacl/issues/comments/385544685","html_url":"https://github.com/pyca/pynacl/issues/431#issuecomment-385544685","issue_url":"https://api.github.com/repos/pyca/pynacl/issues/431","id":385544685,"user":{"login":"leobel96","id":19806799,"avatar_url":"https://avatars2.githubusercontent.com/u/19806799?v=4","gravatar_id":"","url":"https://api.github.com/users/leobel96","html_url":"https://github.com/leobel96","followers_url":"https://api.github.com/users/leobel96/followers","following_url":"https://api.github.com/users/leobel96/following{/other_user}","gists_url":"https://api.github.com/users/leobel96/gists{/gist_id}","starred_url":"https://api.github.com/users/leobel96/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/leobel96/subscriptions","organizations_url":"https://api.github.com/users/leobel96/orgs","repos_url":"https://api.github.com/users/leobel96/repos","events_url":"https://api.github.com/users/leobel96/events{/privacy}","received_events_url":"https://api.github.com/users/leobel96/received_events","type":"User","site_admin":false},"created_at":"2018-04-30T22:13:33Z","updated_at":"2018-04-30T22:13:33Z","author_association":"NONE","body":"Sorry but I'm getting `ImportError: cannot import name 'sodium_increment'`"}}
{"actor":{"display_login":"leobel96"}}