private static void ConsoleTest() { Console.WriteLine("Введите простые числа P и Q"); var p = ulong.Parse(Console.ReadLine() ?? throw new InvalidOperationException("Пустая строка - число")); var q = ulong.Parse(Console.ReadLine() ?? throw new InvalidOperationException("Пустая строка - число")); var encryptor = new RSACryptor(p, q); Console.WriteLine("Введите строку для шифрования"); var input = Console.ReadLine(); var inputBytes = Encoding.UTF8.GetBytes(input ?? string.Empty); Console.WriteLine(input); Console.WriteLine("Input Bytes \r\n" + BitConverter.ToString(inputBytes)); var encrypted = encryptor.Encrypt(inputBytes); Console.WriteLine(Encoding.UTF8.GetString(encrypted)); Console.WriteLine("Encrypted Bytes \r\n" + BitConverter.ToString(encrypted)); var decrypted = encryptor.Decrypt(encrypted); Console.WriteLine(Encoding.UTF8.GetString(decrypted)); Console.WriteLine("Decrypted Bytes \r\n" + BitConverter.ToString(decrypted)); Console.WriteLine( $"Public Key {encryptor.GetPublicKey()}\r\nPrivate Key {encryptor.GetPrivateKey()}\r\n"); }
public XElement Login(string userName, string password, bool rememberMe, out string errorMessage) { string publicKey = GetPublicKey(out errorMessage); if (errorMessage != null) { return(null); } XElement value = new XElement("Login"); value.SetElementValue("UserName", RSACryptor.Encrypt(userName, publicKey)); value.SetElementValue("Password", RSACryptor.Encrypt(password, publicKey)); value.SetElementValue("RememberMe", rememberMe); XElement result = XmlHttpClient.Post(AccountUri, value); errorMessage = result.GetErrorMessage(); if (errorMessage == null) { XElement user = result.Element("element").Elements().First().Elements().First(); return(user); } return(null); }
public string GetPublicKey() { var keys = RSACryptor.CreateKeys(2048, KeyFormat.XML); //where store private key? put into redis ? return(keys[1]); }
public static void EncryptionDecryptionTest(string p, string q, string input) { var rsa = new RSACryptor(ulong.Parse(p), ulong.Parse(q)); var inputBytes = Encoding.UTF8.GetBytes(input); var decryptedBytes = rsa.Decrypt(rsa.Encrypt(inputBytes)); Assert.AreEqual(inputBytes, decryptedBytes); }
public void Test_Encrypt() { var privateKey = "MIICXgIBAAKBgQC+s9ulHlOyAFOr4qHArvW6IVcq9SZnZEs4EPJvzjNGtIq58iNzOEvcMITNAeGqnlLvw3aFoqXqmFc1/yXXuJJGnmoDTjYo3TcdAL4/EeCDXtPhKLeU+iiNNLyp0KpVK7iQlzyFLqVXeb5ii8eYonADeczz58V7xGKma0ZytG3ozwIDAQABAoGBAIrvuK+k169Qo6UP+W7LSUWxMrOeZbtgC5kuZ6LjZOI3ePaeHgu80S/7vVUq8MGAlcYO7xPPevfew3MYj/aJhy9fA+YnJEbusUkfH/N7M35aEmpZAM+jnRftQhjn0Y2IXQ5gGmhEIQ9McDnCqZATYN3D1zQUQ94TtFKh06KEVK75AkEA7854UKO9A594wegTH0KZK8przssYjy1SLXIKRkd2HLuVZa89paBrGgbfIkBf6SrBVAZ4kOFg7FoRq/zs87xe9QJBAMuUh3DgRs6P+FseXh2GnEKdFm7Hjqk/OpDEmpu9PpTF3C5mgf1kEAEelAMm918e+zA9xxXjKizK9VzBeb4wRjMCQGtjJnXWHTqWG1maN5X0GOuHRifgwyq6vOYk/3zhW38acZiLlSuqXsvU7+9CrLCZuOGL1Ens455z5x4BsYqkfFECQQC5+gAZL+m/fmpl40IbOwO5HwSFQyyilK6N3e1X5PQsuOxCP0b1EtpEC4kvsANAFG9oTKd46UN4FBk+GUl8Y2FdAkEAnfAcLBjBBtaPvPuaEMrLdRzYJ1cfax6gpUjSy4LjElq3j1LyFlHg8uASJ6kZmTjLM0x9XTwYS2/zITOW7Zw4xQ=="; var publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+s9ulHlOyAFOr4qHArvW6IVcq9SZnZEs4EPJvzjNGtIq58iNzOEvcMITNAeGqnlLvw3aFoqXqmFc1/yXXuJJGnmoDTjYo3TcdAL4/EeCDXtPhKLeU+iiNNLyp0KpVK7iQlzyFLqVXeb5ii8eYonADeczz58V7xGKma0ZytG3ozwIDAQAB"; string source = "只是告诉素数生成器帮我生成一个接近位的素数而已然后生成这是什么鬼额额额为啥呢呢"; Output.WriteLine("加密字符串长度:" + source.Length); var result = RSACryptor.Encrypt(source, publicKey); Output.WriteLine(result); }
public void Test_CreateKey() { string privateKey; string publicKey; int size = 1024; RSACryptor.CreateKey(out privateKey, out publicKey, size); Output.WriteLine("密钥长度:" + size); Output.WriteLine("私钥:" + privateKey); Output.WriteLine("公钥:" + publicKey); Output.WriteLine("java版私钥:" + privateKey.RsaPrivateKeyDotNet2Java()); Output.WriteLine("java版公钥:" + publicKey.RsaPublicKeyDotNet2Java()); }
public void Test_EncryptAndDecrypt() { var opensslPrivateKey = "MIICWwIBAAKBgQDCBtp6c2QRrJo+Z7zGpm9/9nCK83m0CUnDgiwa5eyQ4ltBDbncGztm9U1HY0lI+GkZgSIUNe0is0wP/iYe30ANWE8s73kS3P5MuA24nPI4R//BL7AgmhlLll9a+FBYaUrpCjZcNI4w1qXyR0n6X5HeU3UzybOdie1o35stHmCgBQIDAQABAoGAaD0YWVru8xPg1hATekHmez/h3LTLuK6Yw4GGwniuLHR/hCakqJy0wC6fcu/jamGSzVH0BhmmqdLb1We8AS/9j330Xc/0Kqw/7OdMwr+6qXjLXJ9oEgW2BePWQMavUibD5cvOwKBYReCsvGDBwI0tdlU/SgN9Cg5hU62VsHcd32ECQQDsXUU+W6k1vebv1NqFkGz2Xly9VHrSvwSqiByG+kFFHJ8/pJOJ5K54f3rc8jUeNcXoJ4zO6+RSD7n3eGuPH8tpAkEA0iUyCL5ZTFFWbKXi925Kl5PQi11f+t+p/OPTSP1SUXw4eWaZWN2YnBf18IEEE3zcidRFPwgLf+VJwysCFwboPQJAAY0DHUugqpeaYkx1Opcd/+fSl/Nr8uIJ98x403Hk570uVk6QIUF825GKjtSQAKi9qa5IwDrP/rHXuIXzvraosQJAAyiN9PWvb+c1DlL7804UDu0o0D9qBuI/ss5VyZ4NE65zRtfU7DIAbjAqASBfSE+zHNs04zqiuZxfnHBUCraO3QJAeBsgY+ECntfPBN2Hky5T29Zpl2P1aWStihR9LePUiAwTQXZLa8F3D5MX43YJ4Pf2MqMkI/fsLioTXHj/b38tMA=="; var opensslPublicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCBtp6c2QRrJo+Z7zGpm9/9nCK83m0CUnDgiwa5eyQ4ltBDbncGztm9U1HY0lI+GkZgSIUNe0is0wP/iYe30ANWE8s73kS3P5MuA24nPI4R//BL7AgmhlLll9a+FBYaUrpCjZcNI4w1qXyR0n6X5HeU3UzybOdie1o35stHmCgBQIDAQAB"; var source = "只是告诉素数生成器帮我生成一个接近位的素数而已然后生成这是什么鬼额额额为啥呢呢"; var encryptResult = RSACryptor.Encrypt(source, opensslPublicKey); Output.WriteLine("加密结果:" + encryptResult); var decryptResult = RSACryptor.Decrypt(encryptResult, opensslPrivateKey); Output.WriteLine("加密结果:" + decryptResult); }
public void Test_CreateKeyToBase64String() { string privateKey; string publicKey; RSACryptor.CreateKey(out privateKey, out publicKey, RsaSize.R1024); Output.WriteLine("私钥:" + privateKey); Output.WriteLine("公钥:" + publicKey); string privateKeyBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(privateKey)); string publicKeyBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(publicKey)); Output.WriteLine("Base64私钥:" + privateKeyBase64); Output.WriteLine("Base64公钥:" + publicKeyBase64); }
public void ChangePassword(string password, string newPassword, out string errorMessage) { string publicKey = GetPublicKey(out errorMessage); if (errorMessage != null) { return; } XElement value = new XElement("ChangePassword"); value.SetElementValue("Password", RSACryptor.Encrypt(password, publicKey)); value.SetElementValue("NewPassword", RSACryptor.Encrypt(newPassword, publicKey)); XElement result = XmlHttpClient.Put(AccountUri, value); errorMessage = result.GetErrorMessage(); }
public void CreatPrivateKeyTest() { var keys = RSACryptor.CreateKeys(2048, KeyFormat.XML); Assert.NotNull(keys); Assert.True(keys.Count == 2); var publicKey = keys[0]; var privateKey = keys[1]; Assert.NotNull(publicKey); Assert.NotNull(privateKey); Assert.True(!publicKey.IsBase64()); // xml content is not base64 string keys = RSACryptor.CreateKeys(2048, KeyFormat.Pkcs1); publicKey = keys[0]; privateKey = keys[1]; Assert.True(publicKey.IsBase64() && privateKey.IsBase64()); // is base64 keys = RSACryptor.CreateKeys(2048, KeyFormat.Pkcs8); publicKey = keys[0]; privateKey = keys[1]; Assert.True(publicKey.IsBase64() && privateKey.IsBase64()); // is base64 }
private RSACryptor CreateCryptor() { var keys = RSACryptor.CreateKeys(2048, KeyFormat.Pkcs1); return(new RSACryptor(RSAType.RSA2, Encoding.UTF8, keys[0], keys[1])); }
/* * This is entry point for PollardAlgo, it should * find all needed value for cryptografic functions * and take care of correct values in GUI */ private void decryptButton_Click(object sender, EventArgs eventArg) { BigInteger N, e; try { e = BigInteger.Parse(eInput.Text); N = BigInteger.Parse(nInput.Text); } catch { MessageBox.Show("N or E is incorrect!"); return; } if (N < 2 || e < 2) { MessageBox.Show("N and E should be more than 1!"); return; } RhoPollard factorer = new RhoPollard(); BigInteger factor; BigInteger iterations; Logger timeLog = new Logger(reportOutput); /* TestOptimizations print time and iterations * in timeLog ( special widget ) */ //(factor, iterations) = factorer.TestOptimizations( N, timeLog ); Stopwatch timer = new Stopwatch(); timer.Start(); (factor, iterations) = factorer.GetOneDivider(N); timer.Stop(); timeLog.Log($"All optimizations timing = {timer.Elapsed}\n"); BigInteger p, q; p = factor; q = N / p; RSACryptor rsa = RSACryptor.UnsafeFactory(p, q, e); pOutput.Text = p.ToString(); qOutput.Text = q.ToString(); dOutput.Text = rsa.D.ToString(); BigInteger ciphered; try { ciphered = BigInteger.Parse(cipheredText.Text); } catch { MessageBox.Show("Bad ciphered text"); return; } BigInteger encodedText = new BigInteger(rsa.Decrypt(ciphered)); uncipheredText.Text = BigIntToMubarakov(encodedText); }