2005-03-28 15:26:23 UTC
Hello,
I want to encypt a small ( I'm aware that max 117 bytes may be encryptes
with RSA ) portion of data with private key and later to decrypt it with a
public key which will be embeded in my code.
The problem I am expieriencing is that I _cannot_ decrypt anything with
public key - no matter whether data was encrypted with public or private
key. And I _can_ decrypt everything with private key that was encrypted with
public _or_ private key.
As far as I understand, only the following situations must be possible in
public-private key encryption:
1) encrypt_with_public / decrypt_with_private
2) encrypt_with_private / decrypt_with_public
Could You please take a look at the short example below and tell me what I
am doing wrong.
Thank You for Your time!
Linas
// A new public-private key pair is generated after instantiation
RSACryptoServiceProvider RSA = new
System.Security.Cryptography.RSACryptoServiceProvider();
string publicKey = RSA.ToXmlString( false ); // gets the public key as XML
string privateKey = RSA.ToXmlString( true ); // gets the private key as XML
// The private key contains a public one (at the beginning of XML string) -
I suppose it is normal
Console.WriteLine('nnPUBLIC KEYn' + publicKey );
Console.WriteLine('nnPRIVATE KEYn' + privateKey );
string str = 'AAA';
RSACryptoServiceProvider RSA2 = new RSACryptoServiceProvider();
// Load private key
RSA2.FromXmlString( privateKey );
// Encrypt with private
byte[] EncryptedStrAsByt = RSA2.Encrypt(
System.Text.Encoding.Default.GetBytes( str ), false );
string EncryptedStrAsString = System.Text.Encoding.Default.GetString(
EncryptedStrAsByt );
RSACryptoServiceProvider RSA3 = new RSACryptoServiceProvider();
// Load public key
RSA3.FromXmlString( publicKey );
// Try to decrypt with public
byte[] DecryptedStrAsByt = RSA3.Decrypt(
System.Text.Encoding.Default.GetBytes( EncryptedStrAsString ), false );
string DecryptedStrAsString = System.Text.Encoding.Default.GetString(
DecryptedStrAsByt );
Console.WriteLine('Decrypted string = {0}', DecryptedStrAsString );
---------------------------------------------------------------------------
I get the following exception when trying to decrypt
System.Security.Cryptography.CryptographicException: Bad Key.
at
System.Security.Cryptography.RSACryptoServiceProvider._DecryptPKWin2KEnh(I
ntPtr hPubKey, Byte[] rgbKey, Boolean fOAEP)
at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[]
rgb,
Boolean fOAEP)
at Test.Main(String[] args) in d:testtest.cs:line 116
Decryption succeeds if I change 'RSA3.FromXmlString( publicKey );' to
'RSA3.FromXmlString( privateKey );'
What is the problem ?
P.S. I tried to pass instance of CspParameters as a RSACryptoServiceProvider
constructors parameter but it does not work as I expect.
I don't want to use any key containers ( that are stored in user profile
data in registry if I understood correctly ) because a program that uses
decryption would run of various computers (could be spread as a demo
program, etc ).
I want to encypt a small ( I'm aware that max 117 bytes may be encryptes
with RSA ) portion of data with private key and later to decrypt it with a
public key which will be embeded in my code.
The problem I am expieriencing is that I _cannot_ decrypt anything with
public key - no matter whether data was encrypted with public or private
key. And I _can_ decrypt everything with private key that was encrypted with
public _or_ private key.
As far as I understand, only the following situations must be possible in
public-private key encryption:
1) encrypt_with_public / decrypt_with_private
2) encrypt_with_private / decrypt_with_public
Could You please take a look at the short example below and tell me what I
am doing wrong.
Thank You for Your time!
Linas
// A new public-private key pair is generated after instantiation
RSACryptoServiceProvider RSA = new
System.Security.Cryptography.RSACryptoServiceProvider();
string publicKey = RSA.ToXmlString( false ); // gets the public key as XML
string privateKey = RSA.ToXmlString( true ); // gets the private key as XML
// The private key contains a public one (at the beginning of XML string) -
I suppose it is normal
Console.WriteLine('nnPUBLIC KEYn' + publicKey );
Console.WriteLine('nnPRIVATE KEYn' + privateKey );
string str = 'AAA';
RSACryptoServiceProvider RSA2 = new RSACryptoServiceProvider();
// Load private key
RSA2.FromXmlString( privateKey );
// Encrypt with private
byte[] EncryptedStrAsByt = RSA2.Encrypt(
System.Text.Encoding.Default.GetBytes( str ), false );
string EncryptedStrAsString = System.Text.Encoding.Default.GetString(
EncryptedStrAsByt );
RSACryptoServiceProvider RSA3 = new RSACryptoServiceProvider();
// Load public key
RSA3.FromXmlString( publicKey );
// Try to decrypt with public
byte[] DecryptedStrAsByt = RSA3.Decrypt(
System.Text.Encoding.Default.GetBytes( EncryptedStrAsString ), false );
string DecryptedStrAsString = System.Text.Encoding.Default.GetString(
DecryptedStrAsByt );
Console.WriteLine('Decrypted string = {0}', DecryptedStrAsString );
---------------------------------------------------------------------------
I get the following exception when trying to decrypt
System.Security.Cryptography.CryptographicException: Bad Key.
at
System.Security.Cryptography.RSACryptoServiceProvider._DecryptPKWin2KEnh(I
ntPtr hPubKey, Byte[] rgbKey, Boolean fOAEP)
at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[]
rgb,
Boolean fOAEP)
at Test.Main(String[] args) in d:testtest.cs:line 116
Decryption succeeds if I change 'RSA3.FromXmlString( publicKey );' to
'RSA3.FromXmlString( privateKey );'
What is the problem ?
P.S. I tried to pass instance of CspParameters as a RSACryptoServiceProvider
constructors parameter but it does not work as I expect.
I don't want to use any key containers ( that are stored in user profile
data in registry if I understood correctly ) because a program that uses
decryption would run of various computers (could be spread as a demo
program, etc ).
Example Rsacryptoserviceprovider Decrypt
Rsacryptoserviceprovider Example Java
- Examples The following code example uses the RSACryptoServiceProvider class to encrypt a string into an array of bytes and then decrypt the bytes back into a string.
- For example, in php, using opensslprivateencrypt - keeping the private key PRIVATE on the server as it should be. Now in the 'client', this would need to be decrypted using the public key (example clients would be a program written in c# and 'signed' with AuthentiCode).
- The below shown example works well with SHA1, but fails when i try with SHA256 or higher. The documentation i found in MSDN does not restrict from using other Hash Algorithms.Any clarification would be appreciated. //Secret message. String message = 'Meet me at 17:00 hours'; //The sender creates a rsa service instance.
- 'The following sample uses the Cryptography class to simulate the roll of a dice. Imports System.IO Imports System.Text Imports System.Security.Cryptography Class RNGCSP Private Shared rngCsp As New RNGCryptoServiceProvider ' Main method.
C# (CSharp) RSACryptoServiceProvider.SignData - 15 examples found. These are the top rated real world C# (CSharp) examples of RSACryptoServiceProvider.SignData extracted from open source projects. You can rate examples to help us improve the quality of examples. Programming Language: C# (CSharp).