public static RSAParametersEx?Parse(byte[] value) { //https://stackoverflow.com/questions/1162504/decrypting-with-private-key-from-pem-file-in-c-sharp-with-net-crypto-library/1162519#1162519 using (var memoryStream = new MemoryStream(value)) using (var reader = new BinaryReader(memoryStream)) { var result = new RSAParametersEx(); var twoBytes = reader.ReadUInt16(); if (twoBytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81) { reader.ReadByte(); //advance 1 byte } else if (twoBytes == 0x8230) { reader.ReadInt16(); //advance 2 bytes } else { throw new ArgumentException(); } twoBytes = reader.ReadUInt16(); if (twoBytes != 0x0102) //version number { return(null); } if (reader.ReadByte() != 0x00) { return(null); } var size = GetIntegerSize(reader); result.Modulus = reader.ReadBytes(size); size = GetIntegerSize(reader); result.Exponent = reader.ReadBytes(size); size = GetIntegerSize(reader); result.D = reader.ReadBytes(size); size = GetIntegerSize(reader); result.P = reader.ReadBytes(size); size = GetIntegerSize(reader); result.Q = reader.ReadBytes(size); size = GetIntegerSize(reader); result.DP = reader.ReadBytes(size); size = GetIntegerSize(reader); result.DQ = reader.ReadBytes(size); size = GetIntegerSize(reader); result.InverseQ = reader.ReadBytes(size); return(result); } }
private static string ToJson(RSAParametersEx parameters) { return(JsonConvert.SerializeObject(parameters, _jsonSettings)); }