{"action":"created","issue":{"url":"https://api.github.com/repos/wader/aes-arraybuffer/issues/2","labels_url":"https://api.github.com/repos/wader/aes-arraybuffer/issues/2/labels{/name}","comments_url":"https://api.github.com/repos/wader/aes-arraybuffer/issues/2/comments","events_url":"https://api.github.com/repos/wader/aes-arraybuffer/issues/2/events","html_url":"https://github.com/wader/aes-arraybuffer/issues/2","id":81256888,"number":2,"title":"Encryption padding breaks Cross platform decryptions","user":{"login":"codeuniquely","id":1037719,"avatar_url":"https://avatars.githubusercontent.com/u/1037719?v=3","gravatar_id":"","url":"https://api.github.com/users/codeuniquely","html_url":"https://github.com/codeuniquely","followers_url":"https://api.github.com/users/codeuniquely/followers","following_url":"https://api.github.com/users/codeuniquely/following{/other_user}","gists_url":"https://api.github.com/users/codeuniquely/gists{/gist_id}","starred_url":"https://api.github.com/users/codeuniquely/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/codeuniquely/subscriptions","organizations_url":"https://api.github.com/users/codeuniquely/orgs","repos_url":"https://api.github.com/users/codeuniquely/repos","events_url":"https://api.github.com/users/codeuniquely/events{/privacy}","received_events_url":"https://api.github.com/users/codeuniquely/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-05-27T03:45:10Z","updated_at":"2015-05-27T11:39:14Z","closed_at":null,"body":"Hi, \r\n\r\nThere is a subtle bug when you encode and pad the last block and that bock falls exactly on a 16 byte boundary). inside crypto.js library.\r\n\r\nI've been passing files to various other platforms and things like opensll and they have an issue decoding any file when it exactly a multiple of 16 bytes.\r\n\r\nthis can be resolved by padding the file with 16 bytes (of 0x10) and then the decryption works on all platforms.\r\n\r\nthe code change required is\r\n\r\n    function pkcs_pad_buffer_to_append(length) {\r\n\r\n      var pad_byte_count = pkcs_max_pad_byte_count - (length % pkcs_max_pad_byte_count);\r\n      if (pad_byte_count===0) {\r\n        pad_byte_count = 16;\r\n      }\r\n\r\n      var pad_u8 = new Uint8Array(pad_byte_count);\r\n      for (var i = 0; i < pad_byte_count; i++) {\r\n        pad_u8[i] = pad_byte_count;\r\n      }\r\n\r\n      return pad_u8.buffer;\r\n    }\r\n"},"comment":{"url":"https://api.github.com/repos/wader/aes-arraybuffer/issues/comments/105879699","html_url":"https://github.com/wader/aes-arraybuffer/issues/2#issuecomment-105879699","issue_url":"https://api.github.com/repos/wader/aes-arraybuffer/issues/2","id":105879699,"user":{"login":"wader","id":185566,"avatar_url":"https://avatars.githubusercontent.com/u/185566?v=3","gravatar_id":"","url":"https://api.github.com/users/wader","html_url":"https://github.com/wader","followers_url":"https://api.github.com/users/wader/followers","following_url":"https://api.github.com/users/wader/following{/other_user}","gists_url":"https://api.github.com/users/wader/gists{/gist_id}","starred_url":"https://api.github.com/users/wader/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wader/subscriptions","organizations_url":"https://api.github.com/users/wader/orgs","repos_url":"https://api.github.com/users/wader/repos","events_url":"https://api.github.com/users/wader/events{/privacy}","received_events_url":"https://api.github.com/users/wader/received_events","type":"User","site_admin":false},"created_at":"2015-05-27T11:39:14Z","updated_at":"2015-05-27T11:39:14Z","body":"Tried to reproduce like this:\r\n\r\n```sh\r\n$ echo -n 0123456789012345 > test\r\n$ ls -l test\r\n-rw-r--r--  1 mattias  staff  16 27 Maj 13:23 test\r\n$ openssl enc -aes-128-cbc -K 00000000000000000000000000000000 -iv 00000000000000000000000000000000 -in test -out test.bin\r\n$ xxd -i test.bin\r\nunsigned char test_bin[] = {\r\n  0x82, 0x21, 0xc5, 0xca, 0xb4, 0x04, 0x79, 0xa2, 0x81, 0xa4, 0x78, 0x8b,\r\n  0x56, 0x0e, 0x67, 0xca, 0x79, 0x13, 0x32, 0xdb, 0x0b, 0xb8, 0x97, 0x6c,\r\n  0x18, 0xf8, 0x3d, 0xa5, 0xd5, 0xed, 0x36, 0xf7\r\n};\r\nunsigned int test_bin_len = 32;\r\n$ xxd -i test\r\nunsigned char test[] = {\r\n  0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31,\r\n  0x32, 0x33, 0x34, 0x35\r\n};\r\nunsigned int test_len = 16;\r\n```\r\n\r\nThen in browser console:\r\n\r\n```javascript\r\n> input = new Uint8Array([\r\n  0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31,\r\n  0x32, 0x33, 0x34, 0x35\r\n]);\r\n[48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53]\r\n> key = new Uint8Array(16);\r\n[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\r\n> iv = new Uint8Array(16);\r\n[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\r\n> encrypted = Crypto.encrypt_aes_cbc(Crypto.pkcs_pad(input.buffer), key.buffer, iv.buffer);\r\nArrayBuffer {}\r\n> new Uint8Array(encrypted)\r\n[130, 33, 197, 202, 180, 4, 121, 162, 129, 164, 120, 139, 86, 14, 103, 202, 121, 19, 50, 219, 11, 184, 151, 108, 24, 248, 61, 165, 213, 237, 54, 247]\r\n// pasting test.bin bytes to compare\r\n> [\r\n  0x82, 0x21, 0xc5, 0xca, 0xb4, 0x04, 0x79, 0xa2, 0x81, 0xa4, 0x78, 0x8b,\r\n  0x56, 0x0e, 0x67, 0xca, 0x79, 0x13, 0x32, 0xdb, 0x0b, 0xb8, 0x97, 0x6c,\r\n  0x18, 0xf8, 0x3d, 0xa5, 0xd5, 0xed, 0x36, 0xf7\r\n]\r\n[130, 33, 197, 202, 180, 4, 121, 162, 129, 164, 120, 139, 86, 14, 103, 202, 121, 19, 50, 219, 11, 184, 151, 108, 24, 248, 61, 165, 213, 237, 54, 247]\r\n```\r\n\r\nSeems to produce the same binary as OpenSSL.\r\n\r\nBut really strange that it seems to work in non-aligned cases for you. Can you paste the encryption part of your code and maybe a test encrypted file that don't work?  \r\n\r\n"}}