please help;
i have creted a server application that generate a x.509v3 certificates. than, i export a publique and private key in .txt file ( format hex, or pem) ;
i must now encrypt files in another client application, my problem is that i can't do this using a public key directly , for this i have written a following code:
string fichier = @"C:\\Users\\Me\\Documents\\marie.docx";
FileInfo f = new FileInfo(fichier);
byte[] buffer = new byte[(int)f.Length];
System.IO.StreamReader strem = new System.IO.StreamReader(fichier);
String line = strem.ReadToEnd();
strem.Close();
IBufferedCipher cipher = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");
string str = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA7cXmz0lTtzEuFAP0rR1f//RZZ8dGxa26I/LwJiNFqJULDvLjmiEHs+u+2gjc3Qm1cwcusuP+akCJIsGyn+9joEbEUKCaJOXbXsuqFKWFlsnfoCGTT8M1a5J2SNixsrLCim9P+qoGfml7JONUhWoiUVYqagRG4WgFmQHonttdnWEqk5yHtXp4ZzL+Bw54IaliyEzigBMW4k0rwMceR31MGnGGcplOynX6Ga5J3HZvLm2XoUQ6PAWw19yjmjiurwBugDOA2NdLOOR96Jxv39Kv/MyGXjjN3Z2hEsSVhHQ4j9RlEg0zsMhXoHJ/MsoVr5WKhEpCuuwp8IuQmwAFW1GoEwIDAQAB";
char[] cArray = str.ToCharArray();
byte[] pub_Array = new byte[cArray.Length];
for (int j = 0; j < cArray.Length; j++)
pub_Array[j] = (byte)cArray[j];
KeyParameter par = new KeyParameter(pub_Array);
cipher.Init(false, par);
//byte[] output = cipher.DoFinal(data);
int blockSize = cipher.GetBlockSize();
int outputSize = cipher.GetOutputSize(buffer.Length);
int leavedSize = buffer.Length % blockSize;
int blocksSize = leavedSize != 0 ? buffer.Length / blockSize + 1 : buffer.Length / blockSize;
byte[] raw = new byte[outputSize * blocksSize];
int i = 0;
while (buffer.Length - i * blockSize > 0)
{
if (buffer.Length - i * blockSize > blockSize)
cipher.DoFinal(buffer, i * blockSize, blockSize,
raw, i * outputSize);
else
cipher.DoFinal(buffer, i * blockSize,
buffer.Length - i * blockSize,
raw, i * outputSize);
i++;
}
// Ecriture du fichier chiffré sur le disque dur
StreamWriter envfos = null;
using (envfos = new StreamWriter("fichier_chiffrer.txt"))
{
envfos.Write(raw);
}
envfos.Close();
i have an
exception in : cipher.Init(false,par); << Impossible d'effectuer un cast d'un objet de type 'Org.BouncyCastle.Crypto.Parameters.KeyParameter' en type 'Org.BouncyCastle.Crypto.AsymmetricKeyParame开发者_运维技巧ter'.
Help please
any documentation in c# please;
think's in advance
Cordialement marie;
RSA encryption needs an AsymmeticKeyParameter
as key. You can see the exception complain about that. You could use the RsaKeyParameter
for this, but this will require you to supply 2 properties: Modulus
and Exponent
wich are both in the key.
Since you are using an already made X509 certificate you could also try to get the key from that file. I supplied some possible example code, but i don't know if it will work as i never even attemted to do something with X509 certificate. The code would replace your KeyParameter par = new KeyParameter(pub_Array);
line.
X509CertificateParser parser = new X509CertificateParser();
AsymmeticKeyParameter par = parser.ReadCertificate(pub_array).GetPublicKey();
Here is a little snippet showing what is probably the easiest way to read the public key.
string str = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA7cXmz0lTtzEuFAP0rR1f//RZZ8dGxa26I/LwJiNFqJULDvLjmiEHs+u+2gjc3Qm1cwcusuP+akCJIsGyn+9joEbEUKCaJOXbXsuqFKWFlsnfoCGTT8M1a5J2SNixsrLCim9P+qoGfml7JONUhWoiUVYqagRG4WgFmQHonttdnWEqk5yHtXp4ZzL+Bw54IaliyEzigBMW4k0rwMceR31MGnGGcplOynX6Ga5J3HZvLm2XoUQ6PAWw19yjmjiurwBugDOA2NdLOOR96Jxv39Kv/MyGXjjN3Z2hEsSVhHQ4j9RlEg0zsMhXoHJ/MsoVr5WKhEpCuuwp8IuQmwAFW1GoEwIDAQAB";
string header = "-----BEGIN PUBLIC KEY\n";
string trailer = "\n-----END PUBLIC KEY";
str = header + str + trailer;
StringReader sr = new StringReader(str);
PemReader PemRdr = new PemReader(sr);
RsaKeyParameters KeyParams = (RsaKeyParameters)PemRdr.ReadObject();
精彩评论