예제 #1
0
        public static RSAViewModel rsa2(RSAViewModel model)
        {
            try
            {
                int keySize = 1024;
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(keySize);

                RSAParameters publickey  = rsa.ExportParameters(false); // don't export private key
                RSAParameters privatekey = rsa.ExportParameters(true);  // export private key
                //\b 123\b0
                model.PublicKey  = "e=" + ByteToString(publickey.Exponent) + Environment.NewLine + "n=" + ByteToString(publickey.Modulus);
                model.PrivateKey = "d=" + ByteToString(privatekey.D) + Environment.NewLine + "n=" + ByteToString(publickey.Modulus);

                rsa.ImportParameters(publickey);
                byte[] encryptedData = rsa.Encrypt(StringToByte(model.PlainText), true);

                model.CipherText = ByteToString(encryptedData);

                rsa.ImportParameters(privatekey);
                byte[] decryptedData = rsa.Decrypt(encryptedData, true);

                model.DecryptedText = ByteToAscii(decryptedData);
            }
            catch (CryptographicException ex)
            {
            }
            return(model);
        }
예제 #2
0
        public static RSAViewModel rsa2(RSAViewModel model)
        {
            try
            {
                int keySize = 1024;
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(keySize);

                RSAParameters publickey = rsa.ExportParameters(false); // don't export private key
                RSAParameters privatekey = rsa.ExportParameters(true); // export private key
                //\b 123\b0
                model.PublicKey = "e=" + ByteToString(publickey.Exponent) + Environment.NewLine + "n=" + ByteToString(publickey.Modulus);
                model.PrivateKey = "d=" + ByteToString(privatekey.D) + Environment.NewLine + "n=" + ByteToString(publickey.Modulus);

                rsa.ImportParameters(publickey);
                byte[] encryptedData = rsa.Encrypt(StringToByte(model.PlainText), true);

                model.CipherText=ByteToString(encryptedData);

                rsa.ImportParameters(privatekey);
                byte[] decryptedData = rsa.Decrypt(encryptedData, true);

                model.DecryptedText = ByteToAscii(decryptedData);

            }
            catch (CryptographicException ex)
            {

            }
            return model;
        }
예제 #3
0
        public ActionResult RSA(RSAViewModel model)
        {
            RSAViewModel viewModel = RSAUtility.rsa2(model);

            ModelState.Clear();
            return View(viewModel);
        }
예제 #4
0
 public ActionResult RSA()
 {
     RSAViewModel model = new RSAViewModel();
     return View(model);
 }