private const AsymmetricAlgorithm RsaType = AsymmetricAlgorithm.RsaPkcs1; //or OeapSha1 and OaepSha256 public void GenerateKeys() { ICryptographicKey key = GetAsymmetricKeyAlgorithm().CreateKeyPair(RsaKeySize); PrivateKey = key.Export(CryptographicPrivateKeyBlobType.Pkcs1RsaPrivateKey); PublicKey = key.ExportPublicKey(CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey); }
public void CreateSymmetricKey_ExportPublicKey() { var provider = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7); ICryptographicKey key = provider.CreateSymmetricKey(this.keyMaterial); Assert.Throws <NotSupportedException>( () => key.ExportPublicKey()); }
public void CreateKey_NotExportable() { var algorithm = WinRTCrypto.MacAlgorithmProvider.OpenAlgorithm(MacAlgorithm.HmacSha1); ICryptographicKey key = algorithm.CreateKey(this.keyMaterial); ExceptionAssert.Throws <NotSupportedException>( () => key.Export()); ExceptionAssert.Throws <NotSupportedException>( () => key.ExportPublicKey()); }
/// <summary> /// Exports the RSA parameters of a cryptographic key. /// </summary> /// <param name="key">The cryptographic key.</param> /// <param name="includePrivateParameters"><c>true</c> to include the private key in the exported parameters; <c>false</c> to only include the public key.</param> /// <returns>The RSA parameters for the key.</returns> public static RSAParameters ExportParameters(this ICryptographicKey key, bool includePrivateParameters) { Requires.NotNull(key, nameof(key)); byte[] keyBlob = includePrivateParameters ? key.Export(CryptographicPrivateKeyBlobType.Pkcs1RsaPrivateKey) : key.ExportPublicKey(CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey); RSAParameters parameters = KeyFormatter.Pkcs1.Read(keyBlob); return(parameters); }
/// <summary>Exports the RSA parameters of a cryptographic key.</summary> /// <param name="key">The cryptographic key.</param> /// <param name="includePrivateParameters"><c>true</c> to include the private key in the exported parameters; <c>false</c> to only include the public key.</param> /// <returns>The RSA parameters for the key.</returns> public static RSAParameters ExportParameters(this ICryptographicKey key, bool includePrivateParameters) { #if PCL throw new NotImplementedException("Not implemented in reference assembly."); #else Requires.NotNull(key, "key"); byte[] keyBlob = includePrivateParameters ? key.Export(CryptographicPrivateKeyBlobType.Pkcs1RsaPrivateKey) : key.ExportPublicKey(CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey); RSAParameters parameters = KeyFormatter.Pkcs1.Read(keyBlob); return(parameters); #endif }
public void RSAParametersPublicKeyRoundtrip() { var rsa = WinRTCrypto.AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithm.RsaOaepSha1); var keyPair = rsa.CreateKeyPair(512); RSAParameters parameters = keyPair.ExportParameters(includePrivateParameters: false); Assert.Null(parameters.P); Assert.Null(parameters.InverseQ); Assert.Null(parameters.D); Assert.Null(parameters.Q); Assert.Null(parameters.DP); Assert.Null(parameters.DQ); ICryptographicKey publicKey = rsa.ImportParameters(parameters); var blob1 = keyPair.ExportPublicKey(); var blob2 = publicKey.ExportPublicKey(); CollectionAssertEx.AreEqual(blob1, blob2); }
/// <summary> /// Initializes a new instance of the <see cref="OwnEndpoint" /> class. /// </summary> /// <param name="signingKey">The signing key.</param> /// <param name="encryptionKey">The encryption key.</param> /// <param name="inboxOwnerCode">The secret that proves ownership of the inbox at the <see cref="Endpoint.MessageReceivingEndpoint" />.</param> public OwnEndpoint(ICryptographicKey signingKey, ICryptographicKey encryptionKey, string inboxOwnerCode = null) : this() { Requires.NotNull(signingKey, "signingKey"); Requires.NotNull(encryptionKey, "encryptionKey"); this.PublicEndpoint = new Endpoint { SigningKeyPublicMaterial = signingKey.ExportPublicKey(CryptoSettings.PublicKeyFormat), EncryptionKeyPublicMaterial = encryptionKey.ExportPublicKey(CryptoSettings.PublicKeyFormat), }; // We could preserve the key instances, but that could make // our behavior a little less repeatable if we had problems // with key serialization. ////this.signingKey = signingKey; ////this.encryptionKey = encryptionKey; // Since this is a new endpoint we can choose a more modern format for the private keys. this.PrivateKeyFormat = CryptographicPrivateKeyBlobType.Pkcs8RawPrivateKeyInfo; this.SigningKeyPrivateMaterial = signingKey.Export(this.PrivateKeyFormat); this.EncryptionKeyPrivateMaterial = encryptionKey.Export(this.PrivateKeyFormat); this.InboxOwnerCode = inboxOwnerCode; }
/// <summary> /// Initializes a new instance of the <see cref="OwnEndpoint" /> class. /// </summary> /// <param name="signingKey">The signing key.</param> /// <param name="encryptionKey">The encryption key.</param> /// <param name="inboxOwnerCode">The secret that proves ownership of the inbox at the <see cref="Endpoint.MessageReceivingEndpoint" />.</param> public OwnEndpoint(ICryptographicKey signingKey, ICryptographicKey encryptionKey, string inboxOwnerCode = null) : this() { Requires.NotNull(signingKey, "signingKey"); Requires.NotNull(encryptionKey, "encryptionKey"); this.PublicEndpoint = new Endpoint { SigningKeyPublicMaterial = signingKey.ExportPublicKey(CryptoSettings.PublicKeyFormat), EncryptionKeyPublicMaterial = encryptionKey.ExportPublicKey(CryptoSettings.PublicKeyFormat), }; // We could preserve the key instances, but that could make // our behavior a little less repeatable if we had problems // with key serialization. ////this.signingKey = signingKey; ////this.encryptionKey = encryptionKey; // Since this is a new endpoint we can choose a more modern format for the private keys. this.PrivateKeyFormat = CryptographicPrivateKeyBlobType.Pkcs8RawPrivateKeyInfo; this.SigningKeyPrivateMaterial = signingKey.Export(this.PrivateKeyFormat); this.EncryptionKeyPrivateMaterial = encryptionKey.Export(this.PrivateKeyFormat); this.InboxOwnerCode = inboxOwnerCode; }
public App() { // create the key we are going to use var asym = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithm.RsaPkcs1); var hash = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha1); // The root page of your application MainPage = new ContentPage { Content = new StackLayout { VerticalOptions = LayoutOptions.Center, Children = { // create a key (createKeyButton = new Button { Text = "Create Key", Command = new Command(() => { key = asym.CreateKeyPair(512); var publicKey = key.ExportPublicKey(); var publicKeyString = Convert.ToBase64String(publicKey); publicKeyLabel.Text = publicKeyString; }) }), (publicKeyLabel = new Label { Text = "..." }), // enter plain text (valueText = new Entry { Text = "Hello World!" }), // start encryption (encryptButton = new Button { Text = "Encrypt", Command = new Command(() => { try { var plainString = valueText.Text; var plain = Encoding.UTF8.GetBytes(plainString); var encrypted = CryptographicEngine.Encrypt(key, plain); var encryptedString = Convert.ToBase64String(encrypted); encryptLabel.Text = encryptedString; } catch (Exception ex) { encryptLabel.Text = "Error encrypting: " + ex.Message; } }) }), (encryptLabel = new Label { Text = "..." }), // and now decrypt (decryptButton = new Button { Text = "Decrypt", Command = new Command(() => { try { var encryptedString = encryptLabel.Text; var encrypted = Convert.FromBase64String(encryptedString); var decrypted = CryptographicEngine.Decrypt(key, encrypted); var decryptedString = Encoding.UTF8.GetString(decrypted, 0, decrypted.Length); decryptLabel.Text = decryptedString; } catch (Exception ex) { decryptLabel.Text = "Error decrypting: " + ex.Message; } }) }), (decryptLabel = new Label { Text = "..." }), // and hash (hashButton = new Button { Text = "hash", Command = new Command(() => { try { var plainString = valueText.Text; var plain = Encoding.UTF8.GetBytes(plainString); var hashed = hash.HashData(plain); var hashedString = Convert.ToBase64String(hashed); hashLabel.Text = hashedString; } catch (Exception ex) { hashLabel.Text = "Error hashing: " + ex.Message; } }) }), (hashLabel = new Label { Text = "..." }) } } }; // set initial data createKeyButton.Command.Execute(null); encryptButton.Command.Execute(null); decryptButton.Command.Execute(null); hashButton.Command.Execute(null); }
public App() { // create the key we are going to use var asym = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithm.RsaPkcs1); var hash = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha1); // The root page of your application MainPage = new ContentPage { Content = new StackLayout { VerticalOptions = LayoutOptions.Center, Children = { // create a key (createKeyButton = new Button { Text = "Create Key", Command = new Command(() => { key = asym.CreateKeyPair(512); var publicKey = key.ExportPublicKey(); var publicKeyString = Convert.ToBase64String(publicKey); publicKeyLabel.Text = publicKeyString; }) }), (publicKeyLabel = new Label { Text = "..." }), // enter plain text (valueText = new Entry { Text = "Hello World!" }), // start encryption (encryptButton = new Button { Text = "Encrypt", Command = new Command(() => { try { var plainString = valueText.Text; var plain = Encoding.UTF8.GetBytes(plainString); var encrypted = CryptographicEngine.Encrypt(key, plain); var encryptedString = Convert.ToBase64String(encrypted); encryptLabel.Text = encryptedString; } catch (Exception ex) { encryptLabel.Text = "Error encrypting: " + ex.Message; } }) }), (encryptLabel = new Label { Text = "..." }), // and now decrypt (decryptButton = new Button { Text = "Decrypt", Command = new Command(() => { try { var encryptedString = encryptLabel.Text; var encrypted = Convert.FromBase64String(encryptedString); var decrypted = CryptographicEngine.Decrypt(key, encrypted); var decryptedString = Encoding.UTF8.GetString(decrypted, 0, decrypted.Length); decryptLabel.Text = decryptedString; } catch (Exception ex) { decryptLabel.Text = "Error decrypting: " + ex.Message; } }) }), (decryptLabel = new Label { Text = "..." }), // and hash (hashButton = new Button { Text = "hash", Command = new Command(() => { try { var plainString = valueText.Text; var plain = Encoding.UTF8.GetBytes(plainString); var hashed = hash.HashData(plain); var hashedString = Convert.ToBase64String(hashed); hashLabel.Text = hashedString; } catch (Exception ex) { hashLabel.Text = "Error hashing: " + ex.Message; } }) }), (hashLabel = new Label { Text = "..." }) } } }; // set initial data createKeyButton.Command.Execute(null); encryptButton.Command.Execute(null); decryptButton.Command.Execute(null); hashButton.Command.Execute(null); }