/// <summary> /// Generates a random encryption key for the Rsa algorithm. /// </summary> /// <returns>The random encrytion keys.</returns> public static RsaKeys GenerateRsaKey() { RSACryptoServiceProvider myRsa = new RSACryptoServiceProvider(2048); RSAParameters rsaParams = myRsa.ExportParameters(true); RsaKeys key = new RsaKeys(); key.N = rsaParams.Modulus; key.E = rsaParams.Exponent; key.D = rsaParams.D; key.P = rsaParams.P; key.Q = rsaParams.Q; key.DP = rsaParams.DP; key.DQ = rsaParams.DQ; key.InverseQ = rsaParams.InverseQ; return(key); }
/// <summary> /// Decrypts a byte array using the Rsa algorithm. /// </summary> /// <param name="Byte">The byte array to be be decrypted.</param> /// <param name="Key">The key used to decrypt.</param> /// <returns>The decrypted byte.</returns> public static byte[] RsaDecryptByteToByte(byte[] Byte, RsaKeys Key) { RSACryptoServiceProvider myRsa = new RSACryptoServiceProvider(); RSAParameters rsaParams = new RSAParameters(); rsaParams.Modulus = Key.N; rsaParams.Exponent = Key.E; rsaParams.D = Key.D; rsaParams.P = Key.P; rsaParams.Q = Key.Q; rsaParams.DP = Key.DP; rsaParams.DQ = Key.DQ; rsaParams.InverseQ = Key.InverseQ; myRsa.ImportParameters(rsaParams); return(myRsa.Decrypt(Byte, true)); }
public void RsaTestFunction() { RsaKeys keys = RsaUtils.GenerateRsaKeys(); RsaTestObj obj = new RsaTestObj() { MyProperty = 9527, MyPropertyList = new List <string> { "9527", "HelloWorld" } }; var text = RsaUtils.Encrypt(keys.PublicKey, obj); var newObj = RsaUtils.Decrypt <RsaTestObj>(keys.PrivateKey, text); bool result = newObj != null && newObj.MyProperty == obj.MyProperty && newObj.MyPropertyList != null && newObj.MyPropertyList.Count == obj.MyPropertyList.Count; if (result) { for (int i = 0; i < obj.MyPropertyList.Count; ++i) { var left = obj.MyPropertyList[i]; var right = newObj.MyPropertyList[i]; if (left != right) { result = false; } } } Assert.IsTrue(result); }
public void Prepare() { rsaKeys = RsaKeys.Create(VirtualSecuredTransferProtocolHelper.DwKeySize); serverSideRsaProvider = rsaKeys.CreateRsaProvider(); clientSideRsaProvider = new RsaKeys { DoubleWordKeySize = rsaKeys.DoubleWordKeySize, PublicKey = rsaKeys.PublicKey }.CreateRsaPublicProvider(); }
public RsaKeys GenerateKeys(int size = 512) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(size); RsaKeys keys = new RsaKeys(); keys.PublicKey = rsa.ToXmlString(false); keys.PrivateKey = rsa.ToXmlString(true); return(keys); }
/// <summary> /// 生成密钥 /// </summary> public static RsaKeys BuildingKeys() { using (RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider()) { RsaKeys keys = new RsaKeys(); rsaProvider.KeySize = 1024; keys.PublicKey = ParseKeyRsaKeyValue <RsaPublicKeyValue>(rsaProvider.ToXmlString(false)); keys.PrivateKey = ParseKeyRsaKeyValue <RsaPrivateKeyValue>(rsaProvider.ToXmlString(true)); return(keys); } }
public void RsaEncrpytionTest() { var rsa = new RsaEncryption(); RsaKeys keys = rsa.GenerateKeys(); var data = Encoding.UTF8.GetBytes(testMessage); var encryptedData = rsa.Encrypt(data, keys.publicKey); var decryptedData = rsa.Decrypt(encryptedData, keys.privateKey); Assert.Equal(data, decryptedData); }
/// <summary> /// Encrypts a byte array using the RSA algorithm. /// </summary> /// <param name="Byte">The byte array to be be encrypted.</param> /// <param name="Key">The key used to encrypt.</param> /// <returns>The encrypted byte.</returns> public static byte[] RsaEncryptByteToByte(byte[] Byte, RsaKeys Key) { RSACryptoServiceProvider myRsa = new RSACryptoServiceProvider(); RSAParameters rsaParams = new RSAParameters(); rsaParams.Modulus = Key.N; rsaParams.Exponent = Key.E; myRsa.ImportParameters(rsaParams); return(myRsa.Encrypt(Byte, true)); }
public void RsaSigningTest() { var rsa = new RsaEncryption(); RsaKeys keys = rsa.GenerateKeys(); var hmacKey = Encoding.UTF8.GetBytes("HMAC KEY"); var hmac = new HmacAuthentication(hmacKey); var data = Encoding.UTF8.GetBytes(testMessage); var hash = hmac.ComputeHash(data); var signature = rsa.SignData(hash, keys.privateKey); var isValid = rsa.VerifySignature(hash, signature, keys.publicKey); Assert.True(isValid); }
public string GenerateKeys(RSA rsa) { var rsaParams = rsa.ExportParameters(true); var keys = new RsaKeys { D = rsaParams.D, DP = rsaParams.DP, DQ = rsaParams.DQ, Exponent = rsaParams.Exponent, InverseQ = rsaParams.InverseQ, Modulus = rsaParams.Modulus, P = rsaParams.P, Q = rsaParams.Q, }; var json = JsonConvert.SerializeObject(keys); return(json); }
public void TestRsa() { var dwKeySize = 2048; using (var originRsaProvider = new RSACryptoServiceProvider(dwKeySize)) { var rsaKey = new RsaKeys { PrivateKey = originRsaProvider.ExportCspBlob(true), PublicKey = originRsaProvider.ExportCspBlob(false), DoubleWordKeySize = originRsaProvider.KeySize }; var publicProvider = rsaKey.CreateRsaPublicProvider(); var privateProvider = rsaKey.CreateRsaProvider(); var originalEncyptedText = originRsaProvider.Encrypt(Encoding.UTF8.GetBytes(testData), true).EncodeBase64(); var publicEncyptedText = publicProvider.Encrypt(Encoding.UTF8.GetBytes(testData), true).EncodeBase64(); var privateEncyptedText = privateProvider.Encrypt(Encoding.UTF8.GetBytes(testData), true).EncodeBase64(); //self var originalDecyptedText1 = Encoding.UTF8.GetString(originRsaProvider.Decrypt(originalEncyptedText.DecodeBase64ToByteArray(), true)); var privateDecyptedText1 = Encoding.UTF8.GetString(privateProvider.Decrypt(privateEncyptedText.DecodeBase64ToByteArray(), true)); Assert.AreEqual(testData, originalDecyptedText1); Assert.AreEqual(testData, privateDecyptedText1); // from private var originalDecyptedText2 = Encoding.UTF8.GetString(originRsaProvider.Decrypt(privateEncyptedText.DecodeBase64ToByteArray(), true)); var privateDecyptedText2 = Encoding.UTF8.GetString(privateProvider.Decrypt(originalEncyptedText.DecodeBase64ToByteArray(), true)); Assert.AreEqual(testData, originalDecyptedText2); Assert.AreEqual(testData, privateDecyptedText2); // from public var originalDecyptedText3 = Encoding.UTF8.GetString(originRsaProvider.Decrypt(publicEncyptedText.DecodeBase64ToByteArray(), true)); var privateDecyptedText3 = Encoding.UTF8.GetString(privateProvider.Decrypt(publicEncyptedText.DecodeBase64ToByteArray(), true)); Assert.AreEqual(testData, originalDecyptedText3); Assert.AreEqual(testData, privateDecyptedText3); } }
/// <summary> /// Encrypts a byte array using the RSA algorithm. /// </summary> /// <param name="Byte">The byte array to be be encrypted.</param> /// <param name="Key">The key used to encrypt.</param> /// <returns>The encrypted byte.</returns> public static byte[] RsaEncryptByteToByte(byte[] Byte, RsaKeys Key) { RSACryptoServiceProvider myRsa = new RSACryptoServiceProvider(); RSAParameters rsaParams = new RSAParameters(); rsaParams.Modulus = Key.N; rsaParams.Exponent = Key.E; myRsa.ImportParameters(rsaParams); return myRsa.Encrypt(Byte, true); }
/// <summary> /// Decrypts a byte array using the Rsa algorithm. /// </summary> /// <param name="Byte">The byte array to be be decrypted.</param> /// <param name="Key">The key used to decrypt.</param> /// <returns>The decrypted byte.</returns> public static byte[] RsaDecryptByteToByte(byte[] Byte, RsaKeys Key) { RSACryptoServiceProvider myRsa = new RSACryptoServiceProvider(); RSAParameters rsaParams = new RSAParameters(); rsaParams.Modulus = Key.N; rsaParams.Exponent = Key.E; rsaParams.D = Key.D; rsaParams.P = Key.P; rsaParams.Q = Key.Q; rsaParams.DP = Key.DP; rsaParams.DQ = Key.DQ; rsaParams.InverseQ = Key.InverseQ; myRsa.ImportParameters(rsaParams); return myRsa.Decrypt(Byte, true); }
/// <summary> /// Generates a random encryption key for the Rsa algorithm. /// </summary> /// <returns>The random encrytion keys.</returns> public static RsaKeys GenerateRsaKey() { RSACryptoServiceProvider myRsa = new RSACryptoServiceProvider(2048); RSAParameters rsaParams = myRsa.ExportParameters(true); RsaKeys key = new RsaKeys(); key.N = rsaParams.Modulus; key.E = rsaParams.Exponent; key.D = rsaParams.D; key.P = rsaParams.P; key.Q = rsaParams.Q; key.DP = rsaParams.DP; key.DQ = rsaParams.DQ; key.InverseQ = rsaParams.InverseQ; return key; }
public static void Initialize(RsaKeys keys) { _rsa = RsaKey.ParsePrivateKey(keys.N, keys.E, keys.D); _diffieHellman = new DiffieHellman(); }