Code:
/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / ndp / clr / src / BCL / System / Security / Cryptography / rsa.cs / 1 / rsa.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
//
// RSA.cs
//
namespace System.Security.Cryptography {
using System.Text;
using System.Runtime.Serialization;
using System.Security.Util;
using System.Globalization;
// We allow only the public components of an RSAParameters object, the Modulus and Exponent
// to be serializable.
[Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct RSAParameters {
public byte[] Exponent;
public byte[] Modulus;
[NonSerialized] public byte[] P;
[NonSerialized] public byte[] Q;
[NonSerialized] public byte[] DP;
[NonSerialized] public byte[] DQ;
[NonSerialized] public byte[] InverseQ;
[NonSerialized] public byte[] D;
}
[System.Runtime.InteropServices.ComVisible(true)]
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
//
new static public RSA Create() {
return Create("System.Security.Cryptography.RSA");
}
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");
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(String.Format(CultureInfo.CurrentCulture, 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(String.Format(CultureInfo.CurrentCulture, 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);
abstract public void ImportParameters(RSAParameters parameters);
}
}
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- RuleInfoComparer.cs
- DataBindingCollection.cs
- GlobalEventManager.cs
- ReadingWritingEntityEventArgs.cs
- AppDomainGrammarProxy.cs
- TabControlEvent.cs
- LockCookie.cs
- AsyncCodeActivity.cs
- PlanCompilerUtil.cs
- ConstraintStruct.cs
- ConsoleTraceListener.cs
- LoginName.cs
- NativeRecognizer.cs
- XmlFormatMapping.cs
- TimerElapsedEvenArgs.cs
- PrintPreviewControl.cs
- TrackingStringDictionary.cs
- XPathArrayIterator.cs
- OptionUsage.cs
- ToolStripItem.cs
- Metafile.cs
- RecommendedAsConfigurableAttribute.cs
- ValidationSummary.cs
- FunctionDetailsReader.cs
- OleDbPermission.cs
- DesignerDataStoredProcedure.cs
- FixedLineResult.cs
- WS2007HttpBindingCollectionElement.cs
- SynchronousChannelMergeEnumerator.cs
- EntitySetBase.cs
- ParameterToken.cs
- HtmlElementEventArgs.cs
- TextTreeUndo.cs
- EntityDataSourceColumn.cs
- QilStrConcatenator.cs
- PropertyInformation.cs
- TreeViewHitTestInfo.cs
- RequestChannelBinder.cs
- AutoCompleteStringCollection.cs
- DataGridViewButtonColumn.cs
- ComponentEditorForm.cs
- RowUpdatedEventArgs.cs
- UnsafePeerToPeerMethods.cs
- GeometryModel3D.cs
- DocumentPaginator.cs
- ProvideValueServiceProvider.cs
- AnonymousIdentificationModule.cs
- SchemaCollectionPreprocessor.cs
- AuthenticationConfig.cs
- ConditionCollection.cs
- MemberPathMap.cs
- XmlAttributeOverrides.cs
- RemotingConfiguration.cs
- WebPartCatalogCloseVerb.cs
- MissingFieldException.cs
- RefreshPropertiesAttribute.cs
- ViewGenerator.cs
- SafeNativeMethods.cs
- SqlBooleanMismatchVisitor.cs
- EnumerableValidator.cs
- NotImplementedException.cs
- HostedTcpTransportManager.cs
- HtmlElementEventArgs.cs
- RijndaelCryptoServiceProvider.cs
- SiteMapHierarchicalDataSourceView.cs
- WebPartEditVerb.cs
- HttpWebRequestElement.cs
- Transactions.cs
- TypeNameParser.cs
- DataListItem.cs
- CacheRequest.cs
- TdsParserSafeHandles.cs
- ManagedFilter.cs
- ProgramPublisher.cs
- Oci.cs
- CalendarTable.cs
- UIElementParagraph.cs
- TextHintingModeValidation.cs
- Authorization.cs
- XmlSchemaComplexContent.cs
- EventRecord.cs
- EngineSiteSapi.cs
- Wildcard.cs
- ZipIOExtraFieldZip64Element.cs
- HttpSocketManager.cs
- DataBoundLiteralControl.cs
- AppDomainProtocolHandler.cs
- ItemCollection.cs
- FileSystemWatcher.cs
- SBCSCodePageEncoding.cs
- ADConnectionHelper.cs
- HeaderElement.cs
- InkCanvas.cs
- ObjectStorage.cs
- ContextBase.cs
- BamlLocalizationDictionary.cs
- TypedTableBaseExtensions.cs
- PlainXmlSerializer.cs
- SafeRightsManagementHandle.cs
- TabControlAutomationPeer.cs