public RSAKeySetIdentity GenerateRSAKeyPair(RSAKeySize keySize) { RSAKeySetIdentity keySet; using (var rsa = new RSACryptoServiceProvider((int)keySize)) { byte[] rsaPrivateKeyData = rsa.ExportCspBlob(true); byte[] rsaPublicKeyData = rsa.ExportCspBlob(false); StringBuilder tmp = new StringBuilder(Convert.ToBase64String(rsaPrivateKeyData)); for (int i = 64; i < tmp.Length; i += 66) { tmp.Insert(i, "\r\n"); } string privKey = BEGIN_RSA_PRIVATE_KEY + "\r\n" + tmp + "\r\n" + END_RSA_PRIVATE_KEY; tmp = new StringBuilder(Convert.ToBase64String(rsaPublicKeyData)); for (int i = 64; i < tmp.Length; i += 66) { tmp.Insert(i, "\r\n"); } string pubKey = BEGIN_RSA_PUBLIC_KEY + "\r\n" + tmp + "\r\n" + END_RSA_PUBLIC_KEY; keySet = new RSAKeySetIdentity(privKey, pubKey); } return(keySet); }
private static RSAKeySetIdentity GetLocalKeySetIdentity() { RSAKeySetIdentity rsaKeySetIdentity = new RSAKeySetIdentity(SerialNumbersSettings.ProtectedApplications.PrivateKeys.GeneralToolkitLib, SerialNumbersSettings.ProtectedApplications.PublicKeys.GeneralToolkitLib); return(rsaKeySetIdentity); }
public RSAParameters ParseRSAPublicKeyOnlyInfo(RSAKeySetIdentity rsaKeySet) { RSAParameters rsaParams; int startIndex = rsaKeySet.RSA_PublicKey.IndexOf(BEGIN_RSA_PUBLIC_KEY, StringComparison.Ordinal); int endIndex = rsaKeySet.RSA_PublicKey.IndexOf(END_RSA_PUBLIC_KEY, StringComparison.Ordinal); if (startIndex < 0 || endIndex < 0) { throw new ArgumentException("Invalid key data"); } string keyData = rsaKeySet.RSA_PublicKey.Substring(startIndex + BEGIN_RSA_PUBLIC_KEY.Length, endIndex - startIndex - BEGIN_RSA_PUBLIC_KEY.Length).Replace("\r\n", ""); byte[] keyBlobBytes = Convert.FromBase64String(keyData); using (var rsa = new RSACryptoServiceProvider()) { rsa.ImportCspBlob(keyBlobBytes); rsaParams = rsa.ExportParameters(false); } return(rsaParams); }