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(); | |
} |
I don't understand how to create a key, I know you use the IAsymmetricKeyAlgorithmProvider, but which method, or how to create the keyblob is where I am stuck. I don't want to implement the interface, as I have no clue how the export should work.
No need to implement the interface. You can create a key by calling:
PCLCrypto.WinRTCrypto.AsymmetricKeyAlgorithmProvider.CreateKeyPair(512)
You don't need to transform it into a blob, you can import modulus and exponent and get the key.
var provider = PCLCrypto.WinRTCrypto.AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithm.RsaPkcs1);
PCLCrypto.RSAParameters rpar = new RSAParameters();
rpar.Exponent = byteExpo;
rpar.Modulus = byteModulus;
var key = provider.ImportParameters(rpar);
var enc = WinRTCrypto.CryptographicEngine.Encrypt(key, System.Text.Encoding.UTF8.GetBytes(data));
May I know how to decrypt the encrypted value.. and i have public private key as a string.. how do i convert he string to public key..
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?
You mentioned your PublicKey class is your own invention. Can you get rid of it in favor of using
ICryptographicKey
everywhere?Ultimately, the PCLCrypto library knows how to deal with public key and exponent variables and convert them into a keyBlob format that can then be consumed, but I don't think that is exposed anywhere. But if we can solve your problem by eradicating your PublicKey class, less code for you to maintain anyway.