/// <summary> /// Create and initialize structure from binary data with defined header /// </summary> /// <exception cref="CryptographicException">On validate errors</exception> /// <returns>Initialized structure</returns> public static PublicKeyBlob FromBinary(BinaryReader reader, BlobHeader header) { var publicKeyBlob = new PublicKeyBlob { Header = header, RSAPubKey = RSAPubKey.FromBinary(reader), }; int modulusLength = (int)(publicKeyBlob.RSAPubKey.BitLength >> 3); publicKeyBlob.Modulus = new byte[modulusLength]; reader.Read(publicKeyBlob.Modulus, 0, publicKeyBlob.Modulus.Length); return(publicKeyBlob); }
/// <summary> /// Create and initialize structure from RSAParameters /// </summary> /// <returns>Initialized structure</returns> /// <note>http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsaparameters.aspx</note> public static PublicKeyBlob FromRSAParameters(RSAParameters @params) { var publicKeyBlob = new PublicKeyBlob { Header = BlobHeader.FromRSAParameters(KeyBlobType.PublicKeyBlob), RSAPubKey = RSAPubKey.FromRSAParameters(@params, false), Modulus = new byte[@params.N.Length], }; for (int i = 0; i < publicKeyBlob.Modulus.Length; i++) { publicKeyBlob.Modulus[i] = @params.N[@params.N.Length - i - 1]; } return(publicKeyBlob); }
/// <summary> /// Exports a blob that contains the key information associated with an AsymmetricAlgorithm object. /// </summary> /// <param name="includePrivateParameters">true to include the private key; otherwise, false.</param> /// <returns>A byte array that contains the key information associated with an AsymmetricAlgorithm object</returns> public byte[] ExportCspBlob(bool includePrivateParameters) { var @params = this.ExportParameters(); using (var stream = new MemoryStream()) { using (var writer = new BinaryWriter(stream)) { if (includePrivateParameters) { PrivateKeyBlob.FromRSAParameters(@params).ToBinary(writer); } else { PublicKeyBlob.FromRSAParameters(@params).ToBinary(writer); } return(stream.ToArray()); } } }
/// <summary> /// Imports a blob that represents asymmetric key information. /// </summary> /// <param name="rawData">A byte array that represents an asymmetric key blob.</param> /// <exception cref="CryptographicException">Invalid key blob data</exception> /// <returns>Initialized RSAParameters structure</returns> public void ImportCspBlob(byte[] rawData) { using (var stream = new MemoryStream(rawData)) { using (var reader = new BinaryReader(stream)) { BlobHeader header = BlobHeader.FromBinary(reader); if (header.BlobType == KeyBlobType.PublicKeyBlob) { this.ImportParameters(PublicKeyBlob.FromBinary(reader, header).ToRSAParameters()); return; } if (header.BlobType == KeyBlobType.PrivateKeyBlob) { this.ImportParameters(PrivateKeyBlob.FromBinary(reader, header).ToRSAParameters()); return; } } } throw new CryptographicException("Invalid key blob data"); }
/// <summary> /// Create and initialize structure from RSAParameters /// </summary> /// <returns>Initialized structure</returns> /// <note>http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsaparameters.aspx</note> public static PublicKeyBlob FromRSAParameters(RSAParameters @params) { var publicKeyBlob = new PublicKeyBlob { Header = BlobHeader.FromRSAParameters(KeyBlobType.PublicKeyBlob), RSAPubKey = RSAPubKey.FromRSAParameters(@params, false), Modulus = new byte[@params.N.Length], }; for (int i = 0; i < publicKeyBlob.Modulus.Length; i++) { publicKeyBlob.Modulus[i] = @params.N[@params.N.Length - i - 1]; } return publicKeyBlob; }
/// <summary> /// Create and initialize structure from binary data with defined header /// </summary> /// <exception cref="CryptographicException">On validate errors</exception> /// <returns>Initialized structure</returns> public static PublicKeyBlob FromBinary(BinaryReader reader, BlobHeader header) { var publicKeyBlob = new PublicKeyBlob { Header = header, RSAPubKey = RSAPubKey.FromBinary(reader), }; int modulusLength = (int)(publicKeyBlob.RSAPubKey.BitLength >> 3); publicKeyBlob.Modulus = new byte[modulusLength]; reader.Read(publicKeyBlob.Modulus, 0, publicKeyBlob.Modulus.Length); return publicKeyBlob; }