Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / System / Security / Cryptography / rsa.cs / 1305376 / rsa.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
// [....]
//
//
// RSA.cs
//
namespace System.Security.Cryptography {
using System.Text;
#if !SILVERLIGHT
using System.Runtime.Serialization;
using System.Security.Util;
#endif // !SILVERLIGHT
using System.Globalization;
using System.Diagnostics.Contracts;
// We allow only the public components of an RSAParameters object, the Modulus and Exponent
// to be serializable.
#if !SILVERLIGHT
[Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
#endif // !SILVERLIGHT
public struct RSAParameters {
public byte[] Exponent;
public byte[] Modulus;
#if SILVERLIGHT
public byte[] P;
public byte[] Q;
public byte[] DP;
public byte[] DQ;
public byte[] InverseQ;
public byte[] D;
#else // SILVERLIGHT
[NonSerialized] public byte[] P;
[NonSerialized] public byte[] Q;
[NonSerialized] public byte[] DP;
[NonSerialized] public byte[] DQ;
[NonSerialized] public byte[] InverseQ;
[NonSerialized] public byte[] D;
#endif // SILVERLIGHT
}
#if !SILVERLIGHT
[System.Runtime.InteropServices.ComVisible(true)]
#endif // !SILVERLIGHT
public abstract class RSA : AsymmetricAlgorithm
{
//
// Extending this class allows us to know that you are really implementing
// an RSA key. This is required for anybody providing a new RSA key value
// implemention.
//
// The class provides no methods, fields or anything else. Its only purpose is
// as a heirarchy member for identification of algorithm.
//
protected RSA() { }
//
// public methods
//
#if !SILVERLIGHT
[System.Security.SecuritySafeCritical] // auto-generated
new static public RSA Create() {
return Create("System.Security.Cryptography.RSA");
}
[System.Security.SecuritySafeCritical] // auto-generated
new static public RSA Create(String algName) {
return (RSA) CryptoConfig.CreateFromName(algName);
}
// Apply the private key to the data. This function represents a
// raw RSA operation -- no implicit depadding of the imput value
abstract public byte[] DecryptValue(byte[] rgb);
// Apply the public key to the data. Again, this is a raw operation, no
// automatic padding.
abstract public byte[] EncryptValue(byte[] rgb);
// Import/export functions
// We can provide a default implementation of FromXmlString because we require
// every RSA implementation to implement ImportParameters
// All we have to do here is parse the XML.
public override void FromXmlString(String xmlString) {
if (xmlString == null) throw new ArgumentNullException("xmlString");
Contract.EndContractBlock();
RSAParameters rsaParams = new RSAParameters();
Parser p = new Parser(xmlString);
SecurityElement topElement = p.GetTopElement();
// Modulus is always present
String modulusString = topElement.SearchForTextOfLocalName("Modulus");
if (modulusString == null) {
throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString","RSA","Modulus"));
}
rsaParams.Modulus = Convert.FromBase64String(Utils.DiscardWhiteSpaces(modulusString));
// Exponent is always present
String exponentString = topElement.SearchForTextOfLocalName("Exponent");
if (exponentString == null) {
throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString","RSA","Exponent"));
}
rsaParams.Exponent = Convert.FromBase64String(Utils.DiscardWhiteSpaces(exponentString));
// P is optional
String pString = topElement.SearchForTextOfLocalName("P");
if (pString != null) rsaParams.P = Convert.FromBase64String(Utils.DiscardWhiteSpaces(pString));
// Q is optional
String qString = topElement.SearchForTextOfLocalName("Q");
if (qString != null) rsaParams.Q = Convert.FromBase64String(Utils.DiscardWhiteSpaces(qString));
// DP is optional
String dpString = topElement.SearchForTextOfLocalName("DP");
if (dpString != null) rsaParams.DP = Convert.FromBase64String(Utils.DiscardWhiteSpaces(dpString));
// DQ is optional
String dqString = topElement.SearchForTextOfLocalName("DQ");
if (dqString != null) rsaParams.DQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(dqString));
// InverseQ is optional
String inverseQString = topElement.SearchForTextOfLocalName("InverseQ");
if (inverseQString != null) rsaParams.InverseQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inverseQString));
// D is optional
String dString = topElement.SearchForTextOfLocalName("D");
if (dString != null) rsaParams.D = Convert.FromBase64String(Utils.DiscardWhiteSpaces(dString));
ImportParameters(rsaParams);
}
// We can provide a default implementation of ToXmlString because we require
// every RSA implementation to implement ImportParameters
// If includePrivateParameters is false, this is just an XMLDSIG RSAKeyValue
// clause. If includePrivateParameters is true, then we extend RSAKeyValue with
// the other (private) elements.
public override String ToXmlString(bool includePrivateParameters) {
// From the XMLDSIG spec, RFC 3075, Section 6.4.2, an RSAKeyValue looks like this:
/*
*/
// we extend appropriately for private components
RSAParameters rsaParams = this.ExportParameters(includePrivateParameters);
StringBuilder sb = new StringBuilder();
sb.Append("");
// Add the modulus
sb.Append(""+Convert.ToBase64String(rsaParams.Modulus)+" ");
// Add the exponent
sb.Append(""+Convert.ToBase64String(rsaParams.Exponent)+" ");
if (includePrivateParameters) {
// Add the private components
sb.Append(""+Convert.ToBase64String(rsaParams.P)+"
");
sb.Append(""+Convert.ToBase64String(rsaParams.Q)+"
");
sb.Append(""+Convert.ToBase64String(rsaParams.DP)+" ");
sb.Append(""+Convert.ToBase64String(rsaParams.DQ)+" ");
sb.Append(""+Convert.ToBase64String(rsaParams.InverseQ)+" ");
sb.Append(""+Convert.ToBase64String(rsaParams.D)+" ");
}
sb.Append(" ");
return(sb.ToString());
}
abstract public RSAParameters ExportParameters(bool includePrivateParameters);
#endif // !SILVERLIGHT
abstract public void ImportParameters(RSAParameters parameters);
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
// [....]
//
//
// RSA.cs
//
namespace System.Security.Cryptography {
using System.Text;
#if !SILVERLIGHT
using System.Runtime.Serialization;
using System.Security.Util;
#endif // !SILVERLIGHT
using System.Globalization;
using System.Diagnostics.Contracts;
// We allow only the public components of an RSAParameters object, the Modulus and Exponent
// to be serializable.
#if !SILVERLIGHT
[Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
#endif // !SILVERLIGHT
public struct RSAParameters {
public byte[] Exponent;
public byte[] Modulus;
#if SILVERLIGHT
public byte[] P;
public byte[] Q;
public byte[] DP;
public byte[] DQ;
public byte[] InverseQ;
public byte[] D;
#else // SILVERLIGHT
[NonSerialized] public byte[] P;
[NonSerialized] public byte[] Q;
[NonSerialized] public byte[] DP;
[NonSerialized] public byte[] DQ;
[NonSerialized] public byte[] InverseQ;
[NonSerialized] public byte[] D;
#endif // SILVERLIGHT
}
#if !SILVERLIGHT
[System.Runtime.InteropServices.ComVisible(true)]
#endif // !SILVERLIGHT
public abstract class RSA : AsymmetricAlgorithm
{
//
// Extending this class allows us to know that you are really implementing
// an RSA key. This is required for anybody providing a new RSA key value
// implemention.
//
// The class provides no methods, fields or anything else. Its only purpose is
// as a heirarchy member for identification of algorithm.
//
protected RSA() { }
//
// public methods
//
#if !SILVERLIGHT
[System.Security.SecuritySafeCritical] // auto-generated
new static public RSA Create() {
return Create("System.Security.Cryptography.RSA");
}
[System.Security.SecuritySafeCritical] // auto-generated
new static public RSA Create(String algName) {
return (RSA) CryptoConfig.CreateFromName(algName);
}
// Apply the private key to the data. This function represents a
// raw RSA operation -- no implicit depadding of the imput value
abstract public byte[] DecryptValue(byte[] rgb);
// Apply the public key to the data. Again, this is a raw operation, no
// automatic padding.
abstract public byte[] EncryptValue(byte[] rgb);
// Import/export functions
// We can provide a default implementation of FromXmlString because we require
// every RSA implementation to implement ImportParameters
// All we have to do here is parse the XML.
public override void FromXmlString(String xmlString) {
if (xmlString == null) throw new ArgumentNullException("xmlString");
Contract.EndContractBlock();
RSAParameters rsaParams = new RSAParameters();
Parser p = new Parser(xmlString);
SecurityElement topElement = p.GetTopElement();
// Modulus is always present
String modulusString = topElement.SearchForTextOfLocalName("Modulus");
if (modulusString == null) {
throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString","RSA","Modulus"));
}
rsaParams.Modulus = Convert.FromBase64String(Utils.DiscardWhiteSpaces(modulusString));
// Exponent is always present
String exponentString = topElement.SearchForTextOfLocalName("Exponent");
if (exponentString == null) {
throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString","RSA","Exponent"));
}
rsaParams.Exponent = Convert.FromBase64String(Utils.DiscardWhiteSpaces(exponentString));
// P is optional
String pString = topElement.SearchForTextOfLocalName("P");
if (pString != null) rsaParams.P = Convert.FromBase64String(Utils.DiscardWhiteSpaces(pString));
// Q is optional
String qString = topElement.SearchForTextOfLocalName("Q");
if (qString != null) rsaParams.Q = Convert.FromBase64String(Utils.DiscardWhiteSpaces(qString));
// DP is optional
String dpString = topElement.SearchForTextOfLocalName("DP");
if (dpString != null) rsaParams.DP = Convert.FromBase64String(Utils.DiscardWhiteSpaces(dpString));
// DQ is optional
String dqString = topElement.SearchForTextOfLocalName("DQ");
if (dqString != null) rsaParams.DQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(dqString));
// InverseQ is optional
String inverseQString = topElement.SearchForTextOfLocalName("InverseQ");
if (inverseQString != null) rsaParams.InverseQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inverseQString));
// D is optional
String dString = topElement.SearchForTextOfLocalName("D");
if (dString != null) rsaParams.D = Convert.FromBase64String(Utils.DiscardWhiteSpaces(dString));
ImportParameters(rsaParams);
}
// We can provide a default implementation of ToXmlString because we require
// every RSA implementation to implement ImportParameters
// If includePrivateParameters is false, this is just an XMLDSIG RSAKeyValue
// clause. If includePrivateParameters is true, then we extend RSAKeyValue with
// the other (private) elements.
public override String ToXmlString(bool includePrivateParameters) {
// From the XMLDSIG spec, RFC 3075, Section 6.4.2, an RSAKeyValue looks like this:
/*
*/
// we extend appropriately for private components
RSAParameters rsaParams = this.ExportParameters(includePrivateParameters);
StringBuilder sb = new StringBuilder();
sb.Append("");
// Add the modulus
sb.Append(""+Convert.ToBase64String(rsaParams.Modulus)+" ");
// Add the exponent
sb.Append(""+Convert.ToBase64String(rsaParams.Exponent)+" ");
if (includePrivateParameters) {
// Add the private components
sb.Append(""+Convert.ToBase64String(rsaParams.P)+"
");
sb.Append(""+Convert.ToBase64String(rsaParams.Q)+"
");
sb.Append(""+Convert.ToBase64String(rsaParams.DP)+" ");
sb.Append(""+Convert.ToBase64String(rsaParams.DQ)+" ");
sb.Append(""+Convert.ToBase64String(rsaParams.InverseQ)+" ");
sb.Append(""+Convert.ToBase64String(rsaParams.D)+" ");
}
sb.Append(" ");
return(sb.ToString());
}
abstract public RSAParameters ExportParameters(bool includePrivateParameters);
#endif // !SILVERLIGHT
abstract public void ImportParameters(RSAParameters parameters);
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- OdbcEnvironment.cs
- LayeredChannelFactory.cs
- VisualBrush.cs
- MarginCollapsingState.cs
- Accessible.cs
- PackWebRequest.cs
- AppLevelCompilationSectionCache.cs
- XamlFigureLengthSerializer.cs
- MaskedTextBoxTextEditorDropDown.cs
- StyleHelper.cs
- ImageUrlEditor.cs
- ProcessModuleCollection.cs
- CompoundFileStorageReference.cs
- PageEventArgs.cs
- BamlRecords.cs
- Speller.cs
- DataGridTextBox.cs
- ClickablePoint.cs
- CriticalHandle.cs
- XPathNavigatorKeyComparer.cs
- GetWinFXPath.cs
- ClientScriptManager.cs
- SchemaTypeEmitter.cs
- BaseTemplateCodeDomTreeGenerator.cs
- ServiceHttpModule.cs
- SafeIUnknown.cs
- AnnotationMap.cs
- XamlGridLengthSerializer.cs
- SoapElementAttribute.cs
- CustomErrorsSectionWrapper.cs
- DataSysAttribute.cs
- CodeAttachEventStatement.cs
- StateMachineSubscriptionManager.cs
- EntityDataSourceValidationException.cs
- ImagingCache.cs
- TextTreeTextElementNode.cs
- FillErrorEventArgs.cs
- FramingChannels.cs
- DbDataAdapter.cs
- BinarySerializer.cs
- PropertyMapper.cs
- ImageField.cs
- TreeSet.cs
- ProjectionCamera.cs
- DataGridViewColumnEventArgs.cs
- BasicViewGenerator.cs
- TriggerActionCollection.cs
- basevalidator.cs
- WrapPanel.cs
- QueryNode.cs
- GreenMethods.cs
- RelationshipConstraintValidator.cs
- ActivityCodeDomReferenceService.cs
- AbsoluteQuery.cs
- Message.cs
- Stack.cs
- SrgsOneOf.cs
- XPathDocument.cs
- Misc.cs
- TableLayoutPanel.cs
- SystemParameters.cs
- ConvertersCollection.cs
- CodeComment.cs
- StylusPointPropertyId.cs
- WindowsGrip.cs
- RightsManagementInformation.cs
- Gdiplus.cs
- BitmapFrameDecode.cs
- IfAction.cs
- InlineCategoriesDocument.cs
- HandleCollector.cs
- UriSectionData.cs
- DocumentXmlWriter.cs
- PeerToPeerException.cs
- DocumentViewerConstants.cs
- GlobalizationSection.cs
- CRYPTPROTECT_PROMPTSTRUCT.cs
- GridErrorDlg.cs
- QilStrConcatenator.cs
- ExeConfigurationFileMap.cs
- XmlSchemaSearchPattern.cs
- KeyInstance.cs
- RuntimeConfig.cs
- UInt16Storage.cs
- FacetChecker.cs
- SafeArrayTypeMismatchException.cs
- SelectionPattern.cs
- ValidationError.cs
- sitestring.cs
- OperationAbortedException.cs
- WebSysDisplayNameAttribute.cs
- InfoCardRSAPKCS1KeyExchangeFormatter.cs
- WebPartConnectionsCancelVerb.cs
- ComponentEditorPage.cs
- SqlXml.cs
- SHA256.cs
- XmlIlGenerator.cs
- SafeCertificateContext.cs
- XPathParser.cs
- WindowsGraphics2.cs