public DigitalSignatureCreationResult Sign(DigitalSignatureCreationArguments arguments) { var res = new DigitalSignatureCreationResult(); try { var rsaProviderReceiver = new RSACryptoServiceProvider(); rsaProviderReceiver.FromXmlString(arguments.PublicKeyForEncryption.ToString()); var encryptionResult = rsaProviderReceiver.Encrypt(Encoding.UTF8.GetBytes(arguments.Message), false); var hashed = _hashingService.Hash(Convert.ToBase64String(encryptionResult)); var rsaProviderSender = new RSACryptoServiceProvider(); rsaProviderSender.FromXmlString(arguments.FullKeyForSignature.ToString()); var signatureFormatter = new RSAPKCS1SignatureFormatter(rsaProviderSender); signatureFormatter.SetHashAlgorithm(_hashingService.HashAlgorithmCode()); var signature = signatureFormatter.CreateSignature(hashed.HashedBytes); res.Signature = signature; res.CipherText = Convert.ToBase64String(encryptionResult); res.Success = true; } catch (Exception ex) { res.ExceptionMessage = ex.Message; } return(res); }
public void DigitalSignatureServiceTest() { var asymmetricService = new RsaAsymmetricCryptographyService(); var keyPairGenerationResultReceiver = asymmetricService.GenerateAsymmetricKeys(1024); var keyPairGenerationResultSender = asymmetricService.GenerateAsymmetricKeys(1024); var digitalSignatureService = new RsaPkcs1DigitalSignatureService(new Sha1ManagedHashingService()); var signatureCreationArgumentsFromSender = new DigitalSignatureCreationArguments() { Message = "eper málna körte", FullKeyForSignature = keyPairGenerationResultSender.FullKeyPairXml, PublicKeyForEncryption = keyPairGenerationResultReceiver.PublicKeyOnlyXml }; var signatureCreationResult = digitalSignatureService.Sign(signatureCreationArgumentsFromSender); Assert.AreEqual(true, signatureCreationResult.Success); var verificationArgumentsFromReceiver = new DigitalSignatureVerificationArguments(); verificationArgumentsFromReceiver.CipherText = signatureCreationResult.CipherText; verificationArgumentsFromReceiver.Signature = signatureCreationResult.Signature; verificationArgumentsFromReceiver.PublicKeyForSignatureVerification = keyPairGenerationResultSender.PublicKeyOnlyXml; verificationArgumentsFromReceiver.FullKeyForDecryption = keyPairGenerationResultReceiver.FullKeyPairXml; var verificationResult = digitalSignatureService.VerifySignature(verificationArgumentsFromReceiver); Assert.AreEqual(true, verificationResult.Success); Assert.AreEqual(true, verificationResult.SignaturesMatch); }
public DigitalSignatureCreationResult Sign(DigitalSignatureCreationArguments arguments) { var res = new DigitalSignatureCreationResult(); try { var rsaProviderReceiver = new RSACryptoServiceProvider(); rsaProviderReceiver.FromXmlString(arguments.PublicKeyForEncryption.ToString()); var encryptionResult = rsaProviderReceiver.Encrypt(Encoding.UTF8.GetBytes(arguments.Message), false); var hashed = _hashingService.Hash(Convert.ToBase64String(encryptionResult)); var rsaProviderSender = new RSACryptoServiceProvider(); rsaProviderSender.FromXmlString(arguments.FullKeyForSignature.ToString()); var signatureFormatter = new RSAPKCS1SignatureFormatter(rsaProviderSender); signatureFormatter.SetHashAlgorithm(_hashingService.HashAlgorithmCode()); var signature = signatureFormatter.CreateSignature(hashed.HashedBytes); res.Signature = signature; res.CipherText = Convert.ToBase64String(encryptionResult); res.Success = true; } catch (Exception ex) { res.ExceptionMessage = ex.Message; } return res; }