-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
43 lines (36 loc) · 1.48 KB
/
Copy pathProgram.cs
File metadata and controls
43 lines (36 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;
namespace CryptographyProject2019
{
public static class Program
{
public static void Run()
{
// AccountsController.GetInstance().AddAccount(new Account("stepanov", "step3110", "Gothman/Batman/Beyond"));
}
public static (string, byte[]) MakeCertificate(string username, DateTimeOffset notBefore,
DateTimeOffset notAfter)
{
var ecdsa = ECDsa.Create(); // generate asymmetric key pair
var req = new CertificateRequest("cn=stepanov", ecdsa, HashAlgorithmName.SHA256);
var cert = req.CreateSelfSigned(notBefore, notAfter);
// Create PFX (PKCS #12) with private key
var pfxBytes = cert.Export(X509ContentType.Pfx, "P@55w0rd");
// Create Base 64 encoded CER (public key only)
var cerString = Encoding.Unicode.GetString(cert.Export(X509ContentType.Cert));
return (cerString, pfxBytes);
}
public static string ReadName(X509Certificate2 cert)
{
var username = cert.SubjectName.Name;
var match = Regex.Match(username, "[cC][nN]=(.*?),");
username = match.Groups[1].Value;
username = Regex.Replace(username, @"\s+", ".");
username = username.ToLower();
return username;
}
}
}