<p>I have an existing application that is written in C++ for Windows. This application uses the Win32 CryptoAPI to generate a TripleDES session key for encrypting/decrypting data. We're using the <a href="http://www.phdcc.com/cryptorc4.htm" rel="nofollow noreferrer">exponent of one trick</a> to export the session key out as a blob, which allows the blob to be stored somewhere in a decrypted format.</p>

<p>The question is how can we use this in our .NET application (C#). The framework encapsulates/wraps much of what the CryptoAPI is doing. Part of the problem is the CryptAPI states that the TripleDES algorithm for the <a href="http://msdn.microsoft.com/en-us/library/aa386986(VS.85).aspx" rel="nofollow noreferrer">Microsoft Enhanced Cryptographic Provider</a> is 168 bits (3 keys of 56 bits). However, the .NET framework states their keys are 192 bits (3 keys of 64 bits). Apparently, the 3 extra bytes in the .NET framework is for parity?</p>

<p>Anyway, we need to read the key portion out of the blob and somehow be able to use that in our .NET application. Currently we are not getting the expected results when attempting to use the key in .NET. The decryption is failing miserably. Any help would be greatly appreciated. </p>

<h2>Update:</h2>

<p>I've been working on ways to resolve this and have come up with a solution that I will post in time. However, still would appreciate any feedback from others.</p>

[{'answer_id': 49232, 'author': 'Nic Strong', 'author_id': 2281, 'author_profile': 'https://Stackoverflow.com/users/2281', 'pm_score': 1, 'selected': False, 'text': '<p>Ok, forget the last answer I can\'t read :) You are working with 3Des keys not RSA keys.</p>\n\n<p>I worked on a bunch of code to share keys between .NET, CryptoAPI and openssl. Found a lot of good example code here for doing the key conversions: </p>\n\n<p><a href="http://www.jensign.com/JavaScience/cryptoutils/index.html" rel="nofollow noreferrer">http://www.jensign.com/JavaScience/cryptoutils/index.html</a></p>\n\n<p>There is some 3des stuff in some of those examples, but it was related to openssl -> .NET iirc.</p>\n\n<p>I also just looked back over the RSA key code and one thing I notice I am doing is using Array.Reverse() on all the key parts of the RSA key (D,DP,DQ,InverseQ,Modulus,P,Q) i guess to convert endian. I remember that being non-obvious when first tackling the problem.</p>\n\n<p>Hope some of that helps. Good luck.</p>\n'}, {'answer_id': 345696, 'author': 'Scott Saad', 'author_id': 4916, 'author_profile': 'https://Stackoverflow.com/users/4916', 'pm_score': 4, 'selected': True, 'text': '<h2>Intro</h2>\n\n<p>I\'m Finally getting around to posting the solution. I hope it provides some help to others out there that might be doing similar type things. There really isn\'t much reference to doing this elsewhere.</p>\n\n<h2>Prerequisites</h2>\n\n<p>In order for a lot of this to make sense it\'s necessary to read the <a href="http://www.phdcc.com/cryptorc4.htm" rel="nofollow noreferrer"><em>exponent of one trick</em></a>, which allows you to export a session key out to a blob (a well known byte structure). One can then do what they wish with this byte stream, but it holds the all important key.</p>\n\n<h2>MSDN Documentation is Confusing</h2>\n\n<p>In this particular example, I\'m using the <a href="http://msdn.microsoft.com/en-us/library/aa386986(VS.85).aspx" rel="nofollow noreferrer">Microsoft Enhanced Cryptographic Provider</a>, with the Triple DES (<a href="http://msdn.microsoft.com/en-us/library/aa382020(VS.85).aspx" rel="nofollow noreferrer">CALG_3DES</a>) algorithm. The first thing that threw me for a loop was the fact that the key length is listed at 168 bits, with a block length of 64 bits. How can the key length be 168? Three keys of 56 bits? What happens to the other byte? </p>\n\n<p>So with that information I started to read elsewhere how the last byte is really parity and for whatever reason CryptoAPI strips that off. Is that really the case? Seems kind of crazy that they would do that, but OK.</p>\n\n<h2>Consumption of Key in .NET</h2>\n\n<p>Using the <a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.tripledescryptoserviceprovider(VS.80).aspx" rel="nofollow noreferrer">TripleDESCryptoServiceProvider</a>, I noticed the remarks in the docs indicated that:</p>\n\n<blockquote>\n  <p>This algorithm supports key lengths from 128 bits to 192 bits in increments of 64 bits.</p>\n</blockquote>\n\n<p>So if CryptoAPI has key lengths of 168, how will I get that into .NET which supports only supports multiples of 64? Therefore, the .NET side of the API takes parity into account, where the CryptoAPI does not. As one could imagine... <em>confused was I</em>.</p>\n\n<p>So with all of this, I\'m trying to figure out how to reconstruct the key on the .NET side with the proper parity information. Doable, but not very fun... let\'s just leave it at that. Once I got all of this in place, everything ended up failing with a CAPITAL <strong>F</strong>. </p>\n\n<p>Still with me? Good, because I just fell off my horse again.</p>\n\n<h2>Light Bulbs and Fireworks</h2>\n\n<p>Low and behold, as I\'m scraping MSDN for every last bit of information I find a conflicting piece in the Win32 <a href="http://msdn.microsoft.com/en-us/library/aa379931(VS.85).aspx" rel="nofollow noreferrer">CryptExportKey</a> function. Low and behold I find this piece of invaluble information:</p>\n\n<blockquote>\n  <p>For any of the DES key permutations that use a PLAINTEXTKEYBLOB, only the full key size, including parity bit, may be exported. The following key sizes are supported.</p>\n  \n  <p>Algorithm Supported key size </p>\n  \n  <p>CALG_DES 64 bits </p>\n  \n  <p>CALG_3DES_112 128 bits </p>\n  \n  <p>CALG_3DES 192 bits </p>\n</blockquote>\n\n<p>So it does export a key that is a multiple of 64 bits! Woohoo! Now to fix the code on the .NET side.</p>\n\n<h2>.NET Import Code Tweak</h2>\n\n<p>The byte order is important to keep in mind when importing a byte stream that contains a key that was exported as a blob from the CryptoAPI. The two API\'s do not use the same byte order, therefore, as <a href="https://stackoverflow.com/questions/49211/how-can-i-use-a-key-blob-generated-from-win32-cryptoapi-in-my-net-application#49232">@nic-strong</a> indicates, reversing the byte array is essential before actually trying to use the key. Other than that, things work as expected. Simply solved:</p>\n\n<pre><code>Array.Reverse( keyByteArray );\n</code></pre>\n\n<h2>Conclusion</h2>\n\n<p>I hope this helps somebody out there. I spent way too much time trying to track this down. Leave any comments if you have further questions and I can attempt to help fill in any details. </p>\n\n<p>Happy Crypto!</p>\n'}]
['https://Stackoverflow.com/questions/49211', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4916/']