Ad
FromXmlString In CSharp And Javascript
Can anyone tell me what the below piece of code is doing. I want to implement this using Nodejs. I'm totally new to CSharp
Does this method FromXmlString really mandatory for encryption here. Please help
public void FromXmlString(RSACryptoServiceProvider rsa, string xmlString)
{
RSAParameters parameters = new RSAParameters();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlString);
if (xmlDoc.DocumentElement.Name.Equals("RSAKeyValue"))
{
foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
{
switch (node.Name)
{
case "Modulus": parameters.Modulus = Convert.FromBase64String(node.InnerText); break;
case "Exponent": parameters.Exponent = Convert.FromBase64String(node.InnerText); break;
case "P": parameters.P = Convert.FromBase64String(node.InnerText); break;
case "Q": parameters.Q = Convert.FromBase64String(node.InnerText); break;
case "DP": parameters.DP = Convert.FromBase64String(node.InnerText); break;
case "DQ": parameters.DQ = Convert.FromBase64String(node.InnerText); break;
case "InverseQ": parameters.InverseQ = Convert.FromBase64String(node.InnerText); break;
case "D": parameters.D = Convert.FromBase64String(node.InnerText); break;
}
}
}
rsa.ImportParameters(parameters);
}
public string EncryptAsymmetric(string encryptText, string publicKey)
{
bool isOAEP = false;
byte[] byteEncrypt = Encoding.UTF8.GetBytes(encryptText);
byte[] encryptedData;
var RSA = new RSACryptoServiceProvider();
FromXmlString(RSA, publicKey);
encryptedData = RSA.Encrypt(byteEncrypt, isOAEP);
return Convert.ToBase64String(encryptedData);
}
Ad
Answer
In a nutshell, it's RSA encrypting a string (encryptText
), using parameters specified as an XML formatted string (publicKey
) and returning the result as a Base64 encoded string.
FromXmlString
is necessary as it is being used to load the encryption parameters from the XML formatted publicKey
.
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad