/// <summary> /// Create and initialize structure from RSAParameters /// </summary> /// <returns>Initialized structure</returns> public static RSAPubKey FromRSAParameters(RSAParameters @params, bool includePrivateParameters) { var rsaPubKey = new RSAPubKey { Magic = (uint)(includePrivateParameters ? 0x32415352 : 0x31415352), BitLength = (uint)(@params.N.Length << 3), }; var bytes = new byte[sizeof(uint)]; bytes[sizeof(uint) - 1] = 0; for (int i = 0; i < @params.E.Length; i++) { bytes[i] = @params.E[@params.E.Length - i - 1]; } rsaPubKey.PublicExponent = BitConverter.ToUInt32(bytes, 0); return(rsaPubKey); }
/// <summary> /// Create and initialize structure from binary data /// </summary> /// <exception cref="CryptographicException">On validate errors</exception> /// <returns>Initialized structure</returns> public static RSAPubKey FromBinary(BinaryReader reader) { var rsaPubKey = new RSAPubKey { Magic = reader.ReadUInt32(), BitLength = reader.ReadUInt32(), PublicExponent = reader.ReadUInt32() }; // Validate if (rsaPubKey.Magic != 0x31415352 && rsaPubKey.Magic != 0x32415352) { throw new CryptographicException(string.Format("Invalid magic number [0x{0:X4}] in RSAPubKey", rsaPubKey.Magic)); } if (rsaPubKey.BitLength % 8 != 0) { throw new CryptographicException(string.Format("Invalid # of bits in modulus [{0}] in RSAPubKey", rsaPubKey.BitLength)); } return(rsaPubKey); }
/// <summary> /// Create and initialize structure from RSAParameters /// </summary> /// <returns>Initialized structure</returns> public static RSAPubKey FromRSAParameters(RSAParameters @params, bool includePrivateParameters) { var rsaPubKey = new RSAPubKey { Magic = (uint)(includePrivateParameters ? 0x32415352 : 0x31415352), BitLength = (uint)(@params.N.Length << 3), }; var bytes = new byte[sizeof(uint)]; bytes[sizeof(uint) - 1] = 0; for (int i = 0; i < @params.E.Length; i++) { bytes[i] = @params.E[@params.E.Length - i - 1]; } rsaPubKey.PublicExponent = BitConverter.ToUInt32(bytes, 0); return rsaPubKey; }
/// <summary> /// Create and initialize structure from binary data /// </summary> /// <exception cref="CryptographicException">On validate errors</exception> /// <returns>Initialized structure</returns> public static RSAPubKey FromBinary(BinaryReader reader) { var rsaPubKey = new RSAPubKey { Magic = reader.ReadUInt32(), BitLength = reader.ReadUInt32(), PublicExponent = reader.ReadUInt32() }; // Validate if (rsaPubKey.Magic != 0x31415352 && rsaPubKey.Magic != 0x32415352) throw new CryptographicException(string.Format("Invalid magic number [0x{0:X4}] in RSAPubKey", rsaPubKey.Magic)); if (rsaPubKey.BitLength % 8 != 0) throw new CryptographicException(string.Format("Invalid # of bits in modulus [{0}] in RSAPubKey", rsaPubKey.BitLength)); return rsaPubKey; }