Skip to content

Instantly share code, notes, and snippets.

@skendrot
Created June 5, 2014 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skendrot/2b2b529605f0b07aba55 to your computer and use it in GitHub Desktop.
Save skendrot/2b2b529605f0b07aba55 to your computer and use it in GitHub Desktop.
Pcl Crypto conversion
private static string EncryptData(PublicKey key, string plainText)
{
byte[] exponent = HexToBytes(key.Exponent);
byte[] modulus = HexToBytes(key.Modulus);
//**************** NEW **************************
IAsymmetricKeyAlgorithmProvider provider = PCLCrypto.WinRTCrypto.AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithm.RsaSignPssSha512);
byte[] keyBlob == null; // how to convert my PublicKey class?
ICryptographicKey cryptographicKey = provider.ImportKeyPair(keyBlob)
var bytes = PCLCrypto.WinRTCrypto.CryptographicEngine.Encrypt(cryptographicKey, Encoding.UTF8.GetBytes(plainText));
return BytesToHex(bytes);
// *************** OLD ***************
//using (var rsa = new System.Security.Cryptography.RSACryptoServiceProvider(512))
//{
// var rsaParms = new System.Security.Cryptography.RSAParameters() { Exponent = exponent, Modulus = modulus };
// rsa.ImportParameters(rsaParms);
// return BytesToHex(rsa.Encrypt(Encoding.UTF8.GetBytes(plainText), false));
//}
}
/// <summary>
/// Decode a hex encoded string to a byte array.
/// </summary>
/// <param name="hex">The hex encoded string.</param>
/// <returns>The resulting byte array.</returns>
private static byte[] HexToBytes(string hex)
{
int length = hex.Length;
if (length % 2 != 0)
{
length += 1;
hex = "0" + hex;
}
byte[] bytes = new byte[length / 2];
for (int i = 0; i < length; i += 2)
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}
/// <summary>
/// Encodes a byte array as a string of hex values.
/// </summary>
/// <param name="bytes">The input byte array to encode.</param>
/// <returns>The hex encoded bytes as a string.</returns>
private static string BytesToHex(byte[] bytes)
{
StringBuilder stringBuilder = new StringBuilder(bytes.Length * 2);
for (int i = 0; i < bytes.Length; i++)
{
byte b = bytes[i];
stringBuilder.AppendFormat("{0:x2}", b);
}
return stringBuilder.ToString();
}
@ParticularName
Copy link

Is it possible in PCLCrypto?
I have a RSA key which I am getting from a service provider .I just want to encrypt the data with that RSA key by using the PCLCrypto library.I don't want to create the RSA key by using PCLCrypto.I only wanted to encrypt the data?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment