Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WinRT port question #4

Closed
benb1n opened this issue Mar 1, 2014 · 3 comments
Closed

WinRT port question #4

benb1n opened this issue Mar 1, 2014 · 3 comments

Comments

@benb1n
Copy link

benb1n commented Mar 1, 2014

Hey @sgbj - This is a question, not an issue, if you have time for it. I'm working to port this library to WinRT so that I can use it in an app I'm creating. WinRT uses a new cryptography API that's different than the one that ships with .Net 4.5. One difference that I've found is that the 3DES implementation in WinRT apparently doesn't support a 16-byte key. Passing a 16-byte key to the function that creates the key throws an ArgumentException, "Value does not fall in expected range." If I expand it to a 24-byte key, padding the extra 8 bytes with zeros, it no longer throws an exception but the encrypted value is larger than the one produced by the 16-byte key and the rest of the algorithm doesn't produce the correct result. Do you have any thoughts/insights that could point me in the right direction? Thanks!

@sgbj
Copy link
Owner

sgbj commented Mar 4, 2014

Sorry I haven't been able to get back with you, the weekend and work sort of snuck up on me.

I must confess that I don't know very much about WinRT. I'm wondering though if you could just do away with the cryptography API that comes with it and use Bouncy Castle's extensive collection of APIs used in cryptography instead? You can easily add it to your project through NuGet.

All you'd have to do is replace the Transform method with something like this:

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Parameters;
using System;
using System.Numerics;
using System.Security.Cryptography;

...

public static BigInteger Transform(string name, bool encrypt, BigInteger key, BigInteger message)
{
    var cipher = new BufferedBlockCipher(new CbcBlockCipher(name == "TripleDES" ? new DesEdeEngine() : new DesEngine()));
    cipher.Init(encrypt, new KeyParameter(key.GetBytes()));
    var input = message.GetBytes();

    byte[] output = new byte[cipher.GetOutputSize(input.Length)];
    var bytesProcessed = cipher.ProcessBytes(input, 0, input.Length, output, 0);
    var outputLength = bytesProcessed;
    bytesProcessed = cipher.DoFinal(output, bytesProcessed);
    outputLength += bytesProcessed;
    var truncatedOutput = new byte[outputLength];
    Array.Copy(output, truncatedOutput, outputLength);

    return BigInt.FromBytes(truncatedOutput);
}

Let me know if this doesn't help you on your way. I'm interested in hearing about what you decide to do.

@benb1n
Copy link
Author

benb1n commented Mar 5, 2014

@sgbj Thanks a million! That advice was golden. We brought in a WinRT port of Bouncy Castle from Nuget, updated the Transform to use your example code above, and it worked beautifully! Thanks again!

We'd like to contribute back to this project if possible. I'm curious if you'd like us to fork your repo and add a separate solution for the WinRT port, or if we should publish a separate repo for it.

@sgbj
Copy link
Owner

sgbj commented Mar 5, 2014

Glad I could help! It's cool that you'd like to contribute, but how you choose to do that is entirely up to you. If you want to fork it, I think that'd give us the added benefit of being able to pull each others changes more easily. But if you choose to make a separate repo for it that's cool too. Just link me to the page when you're done so I can refer people to it in my readme if that's ok with you.

@ichoes ichoes closed this as completed Apr 23, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants