<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	
	>
<channel>
	<title>
	Comments on: Encrypt data using AES and 256-bit keys	</title>
	<atom:link href="https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/feed/" rel="self" type="application/rss+xml" />
	<link>https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/</link>
	<description>Creative Thinking, Clear Engineering</description>
	<lastBuildDate>Mon, 22 May 2023 17:06:15 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.2.9</generator>
	<item>
		<title>
		By: Richard Warrender		</title>
		<link>https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78808</link>

		<dc:creator><![CDATA[Richard Warrender]]></dc:creator>
		<pubDate>Mon, 11 Dec 2017 22:37:54 +0000</pubDate>
		<guid isPermaLink="false">https://richardwarrender.com/?p=936#comment-78808</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78560&quot;&gt;kumar&lt;/a&gt;.

The message is saying it tried to decode the hex into binary data and the length it was expecting didn&#039;t match so check you&#039;ve entered your hex string correctly. You might have missed a character off or there are hidden white spaces lurking somewhere.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78560">kumar</a>.</p>
<p>The message is saying it tried to decode the hex into binary data and the length it was expecting didn&#8217;t match so check you&#8217;ve entered your hex string correctly. You might have missed a character off or there are hidden white spaces lurking somewhere.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Richard Warrender		</title>
		<link>https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78806</link>

		<dc:creator><![CDATA[Richard Warrender]]></dc:creator>
		<pubDate>Mon, 11 Dec 2017 22:17:53 +0000</pubDate>
		<guid isPermaLink="false">https://richardwarrender.com/?p=936#comment-78806</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78785&quot;&gt;simon&lt;/a&gt;.

Hi Simon, be careful here. Don&#039;t forget that block size and key size are &lt;em&gt;different&lt;/em&gt;. Your assumption that the block size of AES256 is 32 is wrong. The AES algorithm uses a 16-byte block size (which is 128-bit assuming 8 bits in a byte). This block size is the same for all key sizes. In laymen terms, the block size is how thinly the data gets chopped up to be worked with; the key size is how often it rotates or gets &quot;shuffled&quot;. The IV is used to modify the first data &lt;em&gt;block&lt;/em&gt; so that is why it must match.

&lt;blockquote&gt;Typically for AES the IV is the size of a block so the IV is 128-bits long.&lt;/blockquote&gt;

My solution does actually follow the `CCrypt` snippet you posted - if you look at the IVs you&#039;ll see they are 32 character hexadecimal strings, 16 pairs - each hex letter pair represents 8-bits each so (8 x 16 = 128-bit).

Hope that helps.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78785">simon</a>.</p>
<p>Hi Simon, be careful here. Don&#8217;t forget that block size and key size are <em>different</em>. Your assumption that the block size of AES256 is 32 is wrong. The AES algorithm uses a 16-byte block size (which is 128-bit assuming 8 bits in a byte). This block size is the same for all key sizes. In laymen terms, the block size is how thinly the data gets chopped up to be worked with; the key size is how often it rotates or gets &#8220;shuffled&#8221;. The IV is used to modify the first data <em>block</em> so that is why it must match.</p>
<blockquote><p>Typically for AES the IV is the size of a block so the IV is 128-bits long.</p></blockquote>
<p>My solution does actually follow the `CCrypt` snippet you posted &#8211; if you look at the IVs you&#8217;ll see they are 32 character hexadecimal strings, 16 pairs &#8211; each hex letter pair represents 8-bits each so (8 x 16 = 128-bit).</p>
<p>Hope that helps.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Richard Warrender		</title>
		<link>https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78805</link>

		<dc:creator><![CDATA[Richard Warrender]]></dc:creator>
		<pubDate>Mon, 11 Dec 2017 21:56:44 +0000</pubDate>
		<guid isPermaLink="false">https://richardwarrender.com/?p=936#comment-78805</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78744&quot;&gt;Nate&lt;/a&gt;.

Yes, this is a bug. Good find, I knew someone would find it soon! ;-)

`bzero` replaces everything at the given memory address with zeros. My intention was to &quot;zero out&quot; the memory before copying the data from the NSData object resulting in at least one byte at the end being \0 (\0 is used by C to indicate a string is complete).

However just doing what Boris suggests won&#039;t work on its own because we need the length of data + 1 for the \0 character. We&#039;re actually initialising the pointer with one less byte of data than we should do. Hence the fix that will solve both warnings in Xcode is:

&lt;pre&gt;
    NSData *data = [NSData dataFromHexString:hexString];
    NSAssert((data.length + 1) == length, @&quot;The hex provided didn&#039;t decode to match length&quot;);
    
    unsigned long total_bytes = (length + 1) * sizeof(char);
    
    *dataPtr = malloc(total_bytes);
    bzero(*dataPtr, total_bytes);
    memcpy(*dataPtr, data.bytes, data.length);
&lt;/pre&gt;

Hope that helps.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78744">Nate</a>.</p>
<p>Yes, this is a bug. Good find, I knew someone would find it soon! ;-)</p>
<p>`bzero` replaces everything at the given memory address with zeros. My intention was to &#8220;zero out&#8221; the memory before copying the data from the NSData object resulting in at least one byte at the end being \0 (\0 is used by C to indicate a string is complete).</p>
<p>However just doing what Boris suggests won&#8217;t work on its own because we need the length of data + 1 for the \0 character. We&#8217;re actually initialising the pointer with one less byte of data than we should do. Hence the fix that will solve both warnings in Xcode is:</p>
<pre>
    NSData *data = [NSData dataFromHexString:hexString];
    NSAssert((data.length + 1) == length, @"The hex provided didn't decode to match length");
    
    unsigned long total_bytes = (length + 1) * sizeof(char);
    
    *dataPtr = malloc(total_bytes);
    bzero(*dataPtr, total_bytes);
    memcpy(*dataPtr, data.bytes, data.length);
</pre>
<p>Hope that helps.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: simon		</title>
		<link>https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78785</link>

		<dc:creator><![CDATA[simon]]></dc:creator>
		<pubDate>Mon, 20 Nov 2017 09:53:48 +0000</pubDate>
		<guid isPermaLink="false">https://richardwarrender.com/?p=936#comment-78785</guid>

					<description><![CDATA[Documentation of CCrypt says about IV &quot;If present, must be the same length as the selected algorithm&#039;s block size.&quot; Block size of AES256 is 32. This solution uses kCCKeySizeAES128 + 1. Is this correct?]]></description>
			<content:encoded><![CDATA[<p>Documentation of CCrypt says about IV &#8220;If present, must be the same length as the selected algorithm&#8217;s block size.&#8221; Block size of AES256 is 32. This solution uses kCCKeySizeAES128 + 1. Is this correct?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Nate		</title>
		<link>https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78744</link>

		<dc:creator><![CDATA[Nate]]></dc:creator>
		<pubDate>Wed, 25 Oct 2017 05:49:59 +0000</pubDate>
		<guid isPermaLink="false">https://richardwarrender.com/?p=936#comment-78744</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78719&quot;&gt;Boris&lt;/a&gt;.

Boris,

bzero(*dataPtr, sizeof(*dataPtr));

XCode 9 hates this line. I keep getting memory errors if I use the category multiple times. 
&#039;bzero&#039; call operates on objects of type &#039;char&#039; while the size is based on a different type &#039;char *&#039;

Symbiosis(8772,0x10d2d1340) malloc: *** error for object 0x608000457af0: Invalid pointer dequeued from free list
*** set a breakpoint in malloc_error_break to debug

The strange thing is my encryption and decryption is working before the memory crash.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78719">Boris</a>.</p>
<p>Boris,</p>
<p>bzero(*dataPtr, sizeof(*dataPtr));</p>
<p>XCode 9 hates this line. I keep getting memory errors if I use the category multiple times.<br />
&#8216;bzero&#8217; call operates on objects of type &#8216;char&#8217; while the size is based on a different type &#8216;char *&#8217;</p>
<p>Symbiosis(8772,0x10d2d1340) malloc: *** error for object 0x608000457af0: Invalid pointer dequeued from free list<br />
*** set a breakpoint in malloc_error_break to debug</p>
<p>The strange thing is my encryption and decryption is working before the memory crash.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Boris		</title>
		<link>https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78719</link>

		<dc:creator><![CDATA[Boris]]></dc:creator>
		<pubDate>Tue, 26 Sep 2017 09:31:02 +0000</pubDate>
		<guid isPermaLink="false">https://richardwarrender.com/?p=936#comment-78719</guid>

					<description><![CDATA[Shouldn&#039;t the bzero part (line 37 in NSData+AES256Encryption)
be `bzero(*dataPtr, length);`? I mean, you want to zero out the entire array and not just the `sizeof(*dataPtr)`.]]></description>
			<content:encoded><![CDATA[<p>Shouldn&#8217;t the bzero part (line 37 in NSData+AES256Encryption)<br />
be `bzero(*dataPtr, length);`? I mean, you want to zero out the entire array and not just the `sizeof(*dataPtr)`.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Erik		</title>
		<link>https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78691</link>

		<dc:creator><![CDATA[Erik]]></dc:creator>
		<pubDate>Mon, 21 Aug 2017 02:36:11 +0000</pubDate>
		<guid isPermaLink="false">https://richardwarrender.com/?p=936#comment-78691</guid>

					<description><![CDATA[Thank You, I have no real experience with Objective-C and not much encryption knowledge but found it easy to follow and implement.]]></description>
			<content:encoded><![CDATA[<p>Thank You, I have no real experience with Objective-C and not much encryption knowledge but found it easy to follow and implement.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Anonymous		</title>
		<link>https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78685</link>

		<dc:creator><![CDATA[Anonymous]]></dc:creator>
		<pubDate>Wed, 02 Aug 2017 05:03:13 +0000</pubDate>
		<guid isPermaLink="false">https://richardwarrender.com/?p=936#comment-78685</guid>

					<description><![CDATA[Hi Richard,

I have implemented the same but the IV is not prefixing to encrypted data. Could you please help me]]></description>
			<content:encoded><![CDATA[<p>Hi Richard,</p>
<p>I have implemented the same but the IV is not prefixing to encrypted data. Could you please help me</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Lorenzo		</title>
		<link>https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78674</link>

		<dc:creator><![CDATA[Lorenzo]]></dc:creator>
		<pubDate>Tue, 20 Jun 2017 22:47:02 +0000</pubDate>
		<guid isPermaLink="false">https://richardwarrender.com/?p=936#comment-78674</guid>

					<description><![CDATA[on iOS 10 there is no trace of the Common Crypto libraries. How could we achieve the same result? Thank you.]]></description>
			<content:encoded><![CDATA[<p>on iOS 10 there is no trace of the Common Crypto libraries. How could we achieve the same result? Thank you.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: sumit magdum		</title>
		<link>https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78563</link>

		<dc:creator><![CDATA[sumit magdum]]></dc:creator>
		<pubDate>Wed, 01 Feb 2017 10:34:07 +0000</pubDate>
		<guid isPermaLink="false">https://richardwarrender.com/?p=936#comment-78563</guid>

					<description><![CDATA[why getting garbage data after decryption 
 if( cryptStatus == kCCSuccess )
    {
        // The returned NSData takes ownership of the buffer and will free it on deallocation
        return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
    }]]></description>
			<content:encoded><![CDATA[<p>why getting garbage data after decryption<br />
 if( cryptStatus == kCCSuccess )<br />
    {<br />
        // The returned NSData takes ownership of the buffer and will free it on deallocation<br />
        return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];<br />
    }</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: kumar		</title>
		<link>https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78561</link>

		<dc:creator><![CDATA[kumar]]></dc:creator>
		<pubDate>Mon, 30 Jan 2017 07:40:21 +0000</pubDate>
		<guid isPermaLink="false">https://richardwarrender.com/?p=936#comment-78561</guid>

					<description><![CDATA[my key and iv are of 16 bytes]]></description>
			<content:encoded><![CDATA[<p>my key and iv are of 16 bytes</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: kumar		</title>
		<link>https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78560</link>

		<dc:creator><![CDATA[kumar]]></dc:creator>
		<pubDate>Mon, 30 Jan 2017 07:06:59 +0000</pubDate>
		<guid isPermaLink="false">https://richardwarrender.com/?p=936#comment-78560</guid>

					<description><![CDATA[Hi i am getting error  &quot;The hex provided didn&#039;t decode to match length&quot; i have key and iv can you please help]]></description>
			<content:encoded><![CDATA[<p>Hi i am getting error  &#8220;The hex provided didn&#8217;t decode to match length&#8221; i have key and iv can you please help</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Richard Warrender		</title>
		<link>https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78542</link>

		<dc:creator><![CDATA[Richard Warrender]]></dc:creator>
		<pubDate>Thu, 05 Jan 2017 16:33:25 +0000</pubDate>
		<guid isPermaLink="false">https://richardwarrender.com/?p=936#comment-78542</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78540&quot;&gt;Anonymous&lt;/a&gt;.

In the &lt;a href=&quot;https://developer.apple.com/reference/foundation/nsdata/1547229-datawithbytesnocopy?language=objc&quot; rel=&quot;nofollow&quot;&gt;NSData docs&lt;/a&gt; under &lt;em&gt;Discussion&lt;/em&gt; it says &lt;strong&gt;[NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted]&lt;/strong&gt; takes ownership of the bytes pointer and frees it on deallocation. Hence it only needs to be freed if the cryptStatus fails.

In other words once we wrap the bytes up in NSData we can let the system free it. Hope that helps!]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78540">Anonymous</a>.</p>
<p>In the <a href="https://developer.apple.com/reference/foundation/nsdata/1547229-datawithbytesnocopy?language=objc" rel="nofollow">NSData docs</a> under <em>Discussion</em> it says <strong>[NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted]</strong> takes ownership of the bytes pointer and frees it on deallocation. Hence it only needs to be freed if the cryptStatus fails.</p>
<p>In other words once we wrap the bytes up in NSData we can let the system free it. Hope that helps!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Anonymous		</title>
		<link>https://richardwarrender.com/2016/04/encrypt-data-using-aes-and-256-bit-keys/#comment-78540</link>

		<dc:creator><![CDATA[Anonymous]]></dc:creator>
		<pubDate>Thu, 05 Jan 2017 06:11:55 +0000</pubDate>
		<guid isPermaLink="false">https://richardwarrender.com/?p=936#comment-78540</guid>

					<description><![CDATA[In this situation when we get success in either encrypting or decrypting data
 if( cryptStatus == kCCSuccess )
    {
        // The returned NSData takes ownership of the buffer and will free it on deallocation
        return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
    }
We did not free the buffer. Why?]]></description>
			<content:encoded><![CDATA[<p>In this situation when we get success in either encrypting or decrypting data<br />
 if( cryptStatus == kCCSuccess )<br />
    {<br />
        // The returned NSData takes ownership of the buffer and will free it on deallocation<br />
        return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];<br />
    }<br />
We did not free the buffer. Why?</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Minified using Disk
Database Caching 8/36 queries in 0.011 seconds using Memcached

Served from: richardwarrender.com @ 2026-05-03 12:28:20 by W3 Total Cache
-->