private static string SerializeCertificate(X509Certificate2 certificate) { string base64Certificate = null; var binaryCertificate = certificate.Export(X509ContentType.Cert); uint size = 0; if (Crypt32.CryptBinaryToString(binaryCertificate, (uint)binaryCertificate.Length, CryptBinaryToStringFlags.CRYPT_STRING_BASE64HEADER, null, ref size)) { var builder = new StringBuilder((int)size); if (Crypt32.CryptBinaryToString(binaryCertificate, (uint)binaryCertificate.Length, CryptBinaryToStringFlags.CRYPT_STRING_BASE64HEADER, builder, ref size)) { base64Certificate = builder.ToString(); } } return(base64Certificate); }
public static String CryptBinaryToString(Byte[] inputArray, CryptEncoding outputEncoding, CryptFormatting outputFormatting) { if ( outputEncoding == CryptEncoding.CRYPT_STRING_BASE64_ANY || outputEncoding == CryptEncoding.CRYPT_STRING_ANY || outputEncoding == CryptEncoding.CRYPT_STRING_HEX_ANY ) { throw new ArgumentException("Invalid encoding is specified."); } UInt32 flags = (UInt32)outputEncoding | (UInt32)outputFormatting; UInt32 pcchString = 0; if (Crypt32.CryptBinaryToString(inputArray, (UInt32)inputArray.Length, flags, null, ref pcchString)) { StringBuilder SB = new StringBuilder((Int32)pcchString); Crypt32.CryptBinaryToString(inputArray, (UInt32)inputArray.Length, flags, SB, ref pcchString); return(SB.ToString()); } throw new Win32Exception(Marshal.GetLastWin32Error()); }
public async Task <IActionResult> CertificateRequest(string id, string certificateName) { var vault = await keyVaultAdminService.GetVaultAsync(id); var op = await keyVaultAdminService.GetCertificateOperation(vault.VaultUri, certificateName); string str = null; if (op.Properties.Csr?.Length > 0) { str = Crypt32.CryptBinaryToString(op.Properties.Csr, true, true); } var model = new UpdateCertificateRequestModel { CertificateName = certificateName, VaultName = id, Csr = str }; return(View(model)); }
private static void _Encode(byte[] input, int input_size, ref byte[] output, ref int output_size, Crypt32.CrypBinaryFlags flags, bool get_only_size, bool alloc_buffer) { if (get_only_size) { output_size = 0; output = null; } else { if (alloc_buffer) { output = new byte[output_size]; } } if (!Crypt32.CryptBinaryToString(input, input_size, flags, output, ref output_size)) { throw new InteropException(); } }