Malik
August 8th, 2002, 09:02 AM
Hi,
Im trying to encrypt file with public key via class" RSACryptoServiceProvider"getting exception:
System.Security.Cryptography.CryptographicException: Direct Encryption and decry
ption using RSA OAEP padding are not available on this platform.
at System.Security.Cryptography.RSACryptoServiceProvider._EncryptPKWin2KEnh(I
ntPtr hPubKey, Byte[] rgbKey, Boolean fOAEP)
i really don't understand these exceptions..
Cheer's
Malik
Holiday
May 7th, 2003, 03:27 PM
Originally posted by Malik
Hi,
Im trying to encrypt file with public key via class" RSACryptoServiceProvider"getting exception:
System.Security.Cryptography.CryptographicException: Direct Encryption and decry
ption using RSA OAEP padding are not available on this platform.
at System.Security.Cryptography.RSACryptoServiceProvider._EncryptPKWin2KEnh(I
ntPtr hPubKey, Byte[] rgbKey, Boolean fOAEP)
i really don't understand these exceptions..
Cheer's
Malik
try using Encrypt(byte[] data,false);
instead EncryptValue(byte[] data);
EncryptValue gives you a NotSupportedException
pareshgh
May 7th, 2003, 06:11 PM
check this sample,
--------------------------------------------------------------------
using System;
using System.IO;
using System.Security.Cryptography;
class SampleCryptography
{
static void Main( string[] args )
{
if( (args.Length == 1) && args[0].StartsWith("/g") )
{
CreateAndSaveKeys();
Console.WriteLine();
Console.WriteLine("Public and private keys written to pubprivkey.xml.");
Console.WriteLine("Public key written to pubkey.xml.");
}
else if( (args.Length == 3) && args[0].StartsWith("/s") )
{
SignFile(args[1], args[2]);
Console.WriteLine();
Console.WriteLine("Signature for {0} written to {1}.", args[1], args[2]);
}
else if( (args.Length == 3) && args[0].StartsWith("/v") )
{
if( VerifyFile(args[1], args[2]) )
{
Console.WriteLine();
Console.WriteLine("{0} has not been tampered with since being signed.", args[1]);
}
else
{
Console.WriteLine();
Console.WriteLine("WARNING: {0} has been tampered with since being signed.", args[1]);
}
}
else
{
Console.WriteLine("Usage:");
Console.WriteLine(" signfile /g[enkeys]");
Console.WriteLine(" signfile /s[ign] inputDataFile outputSignatureFile");
Console.WriteLine(" signfile /v[erify] inputDataFile signatureFile");
}
}
static void CreateAndSaveKeys()
{
// Generate a new key pair, saving both the public & private keys to
// pubprivkey.xml, and just the public key to pubkey.xml.
//
RSACryptoServiceProvider cryptoProvider = new RSACryptoServiceProvider();
SaveKeyInfoAsXml(cryptoProvider, "pubprivkey.xml", true);
SaveKeyInfoAsXml(cryptoProvider, "pubkey.xml", false);
cryptoProvider.Clear();
}
static void SaveKeyInfoAsXml( AsymmetricAlgorithm cryptoProvider, string fileName, bool includePrivateKey )
{
StreamWriter writer = new StreamWriter(fileName);
writer.Write(cryptoProvider.ToXmlString(includePrivateKey));
writer.Close();
}
static string GetKeyInfoAsXml( string fileName )
{
StreamReader reader = new StreamReader(fileName);
string keyInfo = reader.ReadToEnd();
reader.Close();
return(keyInfo);
}
static void SignFile( string dataFile, string sigFile )
{
RSACryptoServiceProvider cryptoProvider = new RSACryptoServiceProvider();
cryptoProvider.FromXmlString(GetKeyInfoAsXml("pubkey.xml"));
// Get the data from the file to sign.
//
byte[] data = GetFileContents(dataFile);
// Compute a hash of that data.
//
SHA1Managed hashGenerator = new SHA1Managed();
byte[] hash = hashGenerator.ComputeHash(data);
// Encrypt the hash to generate a signature, and store signature
// to disk.
//
byte[] signature = cryptoProvider.Encrypt(hash, false);
WriteFileContents(sigFile, signature);
cryptoProvider.Clear();
}
static bool VerifyFile( string dataFile, string sigFile )
{
bool hashesMatch = false;
try
{
RSACryptoServiceProvider cryptoProvider = new RSACryptoServiceProvider();
cryptoProvider.FromXmlString(GetKeyInfoAsXml("pubprivkey.xml"));
// Get the data from the file to verify.
//
byte[] data = GetFileContents(dataFile);
// Compute a hash of that data.
//
SHA1Managed hashGenerator = new SHA1Managed();
byte[] hashComputed = hashGenerator.ComputeHash(data);
// Read the signature from the signature file and then
// decrypt it.
//
byte[] signature = GetFileContents(sigFile);
byte[] hashTransmitted = cryptoProvider.Decrypt(signature, false);
// Compare the computed hash to the hash that was retrieved from
// the signature file.
//
hashesMatch = ArrayEquals(hashTransmitted, hashComputed);
cryptoProvider.Clear();
}
catch
{
}
return(hashesMatch);
}
static bool ArrayEquals( byte[] a1, byte[] a2 )
{
if( a1.Length != a2.Length )
return(false);
for( int n = 0; n < a1.Length; n++ )
{
if( a1[n] != a2[n] )
return(false);
}
return(true);
}
static byte[] GetFileContents( string fileName )
{
FileInfo fileInfo = new FileInfo(fileName);
BinaryReader reader = new BinaryReader(File.OpenRead(fileName));
byte[] data = new byte[fileInfo.Length];
reader.Read(data, 0, data.Length);
reader.Close();
return(data);
}
static void WriteFileContents( string fileName, byte[] data )
{
BinaryWriter writer = new BinaryWriter(File.Create(fileName));
writer.Write(data);
writer.Close();
}
}
------------------------------------------------------------
-Paresh
womalley
July 2nd, 2004, 07:31 AM
pareshgh:
I am always impressed by your posts..
If you dont mind me asking, could you point out some good books or resources for Encryption and DeCryption?
I am very interested in Encryption including double symmetric encryption.
...
I was reading something.. cant really remember what but it was about encryption.. anyways they were saying that you take a string and encrypt it. Then you read the string in backwords so last character to first character and encrypt it that way. Sounded very cool
well thanks for the post and help
Will