I try to encrypt a file with RSA
, but it don't have the toXmlString()
and fromXmlString
method. how to use RSA
class in .net core ?
And I want to encrypt with private key and decrypt with public key ,so others can only read this file but can't generate this file , Is that possible ?

- 716
- 1
- 7
- 24
-
Can't you use the other methods that are avaiable? – svick Jul 10 '16 at 11:00
-
all example that I find online , they all use the `fromXmlString` method . Can you show me another way? – John Jul 10 '16 at 11:53
-
1You can use an extension like this one: https://gist.github.com/Jargon64/5b172c452827e15b21882f1d76a94be4/ – Manuel Alves Oct 28 '16 at 09:16
2 Answers
While the ToXmlString
and FromXmlString
methods are not available, ImportParameters(RSAParameters)
and ExportParameters(bool)
are.
If you have existing XML formatted values that you need to continue to read, the XML format of the keys isn't very interesting:
Public only:
<RSAKeyValue>
<Modulus>[base64-encoded value]</Modulus>
<Exponent>[base64-encoded value]</Exponent>
</RSAKeyValue>
Public+Private:
<RSAKeyValue>
<Modulus>[base64-encoded value]</Modulus>
<Exponent>[base64-encoded value]</Exponent>
<P>[base64-encoded value]</P>
<Q>[base64-encoded value]</Q>
<DP>[base64-encoded value]</DP>
<DQ>[base64-encoded value]</DQ>
<InverseQ>[base64-encoded value]</InverseQ>
</RSAKeyValue>
(where each named XML element maps to the field of the same name on the RSAParameters
struct)
Otherwise you can/should use whatever serialization mechanism is the best suited for you and your application.

- 30,352
- 2
- 71
- 111
-
I create my own `ToXMlString` and `FromXmlString` with `exportParameters` and `ImportParameters` , it work fine ,but that can not use priviate key to encrypto and public key to decrypto, any Suggestions? – John Jul 18 '16 at 05:45
-
3@John A private key can encrypt, since it has the public, but a public key cannot decrypt. Using the private key to prove authenticity is called signing, so perhaps you want SignData and VerifyData, as opposed to Encrypt and Decrypt. – bartonjs Jul 18 '16 at 06:00
-
Perhaps you realize that I want to write a license file, that license is a Encrypted xml file( binary formate ) contains some necessary information . My application need that information to startup and do something. So I really need to encrypt and decrypt that, currently i use AES to encrypt that file, but if someone decompile my application, they will get the AES key , so they can easily generate any license thay want . – John Jul 18 '16 at 07:46
-
2This is straying from the original question, perhaps you need a new one. A license file is usually not encrypted, but rather signed. { License: { User: John, Serial: 123456789 }, Signature: (binary/base64 blob) }.Then you build a binary form of license (e.g. UTF-8 User concat serial) and call rsaPublic.VerifyData(data, license.Signature, ...). But .NET (Core or Framework) does not allow private encrypt + public decrypt except for RSA signing. – bartonjs Jul 18 '16 at 15:37
Currently you get less when using pure .net core:
In the end this means you can build on .NET Core today, and expect more features to light up later, so things will get easier as time goes on. Whether you want to be an early adopter with the less featured framework now, or jump in later when more features have been added and the third party eco-system has caught up is going to be a (tough) decision that we all have to make on your own.
You will have them available if you target the full .net framework in .net core.
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"net461": {}
}
}
They won't be available with for example:
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},

- 21,202
- 16
- 97
- 123