{"action":"created","issue":{"url":"https://api.github.com/repos/krzyzanowskim/CryptoSwift/issues/312","repository_url":"https://api.github.com/repos/krzyzanowskim/CryptoSwift","labels_url":"https://api.github.com/repos/krzyzanowskim/CryptoSwift/issues/312/labels{/name}","comments_url":"https://api.github.com/repos/krzyzanowskim/CryptoSwift/issues/312/comments","events_url":"https://api.github.com/repos/krzyzanowskim/CryptoSwift/issues/312/events","html_url":"https://github.com/krzyzanowskim/CryptoSwift/issues/312","id":173193117,"number":312,"title":"extra bytes after decrypting AES 256 CBC","user":{"login":"ashiqsulaimanbaryons","id":20036490,"avatar_url":"https://avatars.githubusercontent.com/u/20036490?v=3","gravatar_id":"","url":"https://api.github.com/users/ashiqsulaimanbaryons","html_url":"https://github.com/ashiqsulaimanbaryons","followers_url":"https://api.github.com/users/ashiqsulaimanbaryons/followers","following_url":"https://api.github.com/users/ashiqsulaimanbaryons/following{/other_user}","gists_url":"https://api.github.com/users/ashiqsulaimanbaryons/gists{/gist_id}","starred_url":"https://api.github.com/users/ashiqsulaimanbaryons/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ashiqsulaimanbaryons/subscriptions","organizations_url":"https://api.github.com/users/ashiqsulaimanbaryons/orgs","repos_url":"https://api.github.com/users/ashiqsulaimanbaryons/repos","events_url":"https://api.github.com/users/ashiqsulaimanbaryons/events{/privacy}","received_events_url":"https://api.github.com/users/ashiqsulaimanbaryons/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2016-08-25T12:44:24Z","updated_at":"2016-08-30T23:05:16Z","closed_at":"2016-08-30T23:05:16Z","body":"When i am using this code. The Uint8 array generated after the decryption, constantly has 7 0's in the end.   [56, 56, 53, 98, 53, 101, 98, 99, 53, 98, 57, 49, 54, 57, 48, 98, 51, 55, 56, 49, 98, 101, 51, 51, 101, 102, 55, 55, 55, 98, 49, 48, 79, 114, 70, 101, 99, 107, 114, 87, 111, 0, 0, 0, 0, 0, 0, 0]\r\nJust want to confirm if it is safe to remove the trailing 0's.\r\n\r\n```swift\r\n do {\r\n            let aes = try AES(key:  \"0123456789abcdef\", iv: \"fedcba9876543210\", blockMode: .CBC, padding: NoPadding())\r\n            //print(\"the value of hexaval is  \\(dataFromHexadecimalString(encryptedString))\")\r\n            let count = dataFromHexadecimalString(encryptedString)!.length / sizeof(UInt8)\r\n            \r\n            // create an array of Uint8\r\n            var array = [UInt8](count: count, repeatedValue: 0)\r\n            \r\n            // copy bytes into array\r\n            dataFromHexadecimalString(encryptedString)!.getBytes(&array, length:count * sizeof(UInt8))\r\n            \r\n            //print(\"the array is \\(array)\")\r\n            var ciphertext = try aes.decrypt(Array<UInt8>(array))\r\n            /// loop to remove the trailing zeros       \r\n            print(\"the cipher is \\(ciphertext)\")\r\n            //Convert [UInt8] to NSData\r\n            let data = NSData(bytes: ciphertext, length: ciphertext.count)\r\n            \r\n            //Convert NSData to NSString\r\n            let newNSString = NSString(data: data, encoding: NSUTF8StringEncoding)! as String\r\n            \r\n            //print(\"decrypted vlaue is \\(newNSString)\")\r\n            \r\n            apiKey = newNSString\r\n            \r\n            return newNSString\r\n            \r\n        } catch {\r\n            print(\"Decryption error\")\r\n        }\r\n       \r\n        return \"Error\"\r\n    }\r\n```"},"comment":{"url":"https://api.github.com/repos/krzyzanowskim/CryptoSwift/issues/comments/243609381","html_url":"https://github.com/krzyzanowskim/CryptoSwift/issues/312#issuecomment-243609381","issue_url":"https://api.github.com/repos/krzyzanowskim/CryptoSwift/issues/312","id":243609381,"user":{"login":"krzyzanowskim","id":758033,"avatar_url":"https://avatars.githubusercontent.com/u/758033?v=3","gravatar_id":"","url":"https://api.github.com/users/krzyzanowskim","html_url":"https://github.com/krzyzanowskim","followers_url":"https://api.github.com/users/krzyzanowskim/followers","following_url":"https://api.github.com/users/krzyzanowskim/following{/other_user}","gists_url":"https://api.github.com/users/krzyzanowskim/gists{/gist_id}","starred_url":"https://api.github.com/users/krzyzanowskim/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/krzyzanowskim/subscriptions","organizations_url":"https://api.github.com/users/krzyzanowskim/orgs","repos_url":"https://api.github.com/users/krzyzanowskim/repos","events_url":"https://api.github.com/users/krzyzanowskim/events{/privacy}","received_events_url":"https://api.github.com/users/krzyzanowskim/received_events","type":"User","site_admin":false},"created_at":"2016-08-30T23:05:16Z","updated_at":"2016-08-30T23:05:16Z","body":"There are no extra bytes. Output and input number of bytes is exactly the same.\r\n\r\nTo answer your question, it doesn't look like you shouldn't remove trailing zeros. It's just an accident you get 0 there. It's enough to modify single byte and you won't get trailing zeros.\r\n\r\nIt's just by accident trailing 0 is string termination and when you build String out of it it's ignored.\r\n\r\n```\r\ndo {\r\n    let aes = try AES(key:  \"0123456789abcdef\", iv: \"fedcba9876543210\", blockMode: .CBC, padding: NoPadding())\r\n    let bytes = Array<UInt8>(hex: \"5cbb915bd54458fd77aa86e9606533b563166eefebd3529866fb46609e7ad8f9e1ee2c09906ec08d00e024f9d47dd711\")\r\n    bytes.count\r\n    let ciphertext = try aes.decrypt(bytes)\r\n    ciphertext.count\r\n    print(\"the cipher is \\(ciphertext)\")\r\n} catch {\r\n    print(\"Decryption error\")\r\n}\r\n```"}}
{"actor":{"display_login":"krzyzanowskim"}}