Signature SHA1withRSA en .Net
Bonjour et Merci pour votre aide futur.
Je veux signer numeriquement un certificat avec l'algo SHA1withRSA en C#
quelqun a déjà fait ?
je suis ouvert a toutes vos suggestion.
PS:
le bout de code suivant ne marche pas ...
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| byte[] dataToSign = Convert.FromBase64String(certificat);
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
//invocation des classes de signature
//Creation de l'objet RSAOPKCS1SignatureFormatter.
RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(rsa);
// on passe les informations sur la clé privée du certificat
RSAFormatter.SetKey(certificatServ.PrivateKey);
RSAFormatter.SetHashAlgorithm("SHA1withRSA");
byte[] SignedHash = RSAFormatter.CreateSignature(dataToSign);
// on reconstruit la chaine de caractere a partire des byte[] signer.
retour = Convert.ToBase64String(SignedHash, 0, SignedHash.Length);
} |
Merci
Re:Signature SHA1withRSA en .Net
J’ai pu résoudre le problème en utilisant le Framework Bouncy castle.
C’est un excellent Framwork pour tout ce qui est Cryptage, signature... (PGP, X509, RSA …)
Le bout de code qui permet cela :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| if (!String.IsNullOrEmpty(m_CheminCertificat) && !String.IsNullOrEmpty(m_Password))
{
byte[] dataToSign = Base64.Decode(certificat);
// on signe avec l'algo SHA1withRSA
ISigner signer = SignerUtilities.GetSigner(Config.ALGO_ENCRYPTION);
StreamReader reader = new StreamReader(m_CheminCertificat);
Stream st = reader.BaseStream;
// on récupere le magasin de clé du certificat
Pkcs12Store ks = new Pkcs12Store(st, m_Password.ToCharArray());
string pName = string.Empty;
foreach (string n in ks.Aliases)
{
if (ks.IsKeyEntry(n))
{
pName = n;
//break;
}
}
// on récupere la clé privée.
AsymmetricKeyEntry pk = ks.GetKey(pName);
// on initialise la classe Signature
signer.Init(true, pk.Key);
signer.BlockUpdate(dataToSign, 0, dataToSign.Length);
byte[] sigBytes = signer.GenerateSignature();
retour = Convert.ToBase64String(sigBytes, 0, sigBytes.Length);
} |