Ejemplo n.º 1
0
        public static KeyModel GetKeyByHeader(string header)
        {
            var keyModel = new KeyModel();
            string[] forKey = header.Split('_');
            if (forKey.Length < 2)
            {
                throw new InputFormatException("header");
            }
            string[] keys = forKey[1].Split(',');
            if (keys.Length < 2)
            {
                throw new InputFormatException("header");
            }
            try
            {
                if (DefinedEncrypt(header))
                    keyModel.E = BigInteger.Parse(keys[0]);
                else
                    keyModel.D = BigInteger.Parse(keys[0]);

                keyModel.N = BigInteger.Parse(keys[1]);
            }
            catch (Exception e)
            {
                throw new InputFormatException(e.Message);
            }
            return keyModel;
        }
Ejemplo n.º 2
0
        private void Input(out bool forEncode, out string data, out KeyModel key)
        {
            using (var reader = new StreamReader(_settings.InputFilename, Encoding.UTF8))
            {
                string firstLine = reader.ReadLine();
                bool isEncrypt = RSAHelper.DefinedEncrypt(firstLine);
                KeyModel keyModel;
                try
                {
                     keyModel = RSAHelper.GetKeyByHeader(firstLine);
                }catch(InputFormatException)
                {
                    //need to decrypt secret key to  encrypt the public
                    //if the encrypt key is not specified - it will generate
                    if (isEncrypt)
                        keyModel = _cryptService.GenerateKey();
                    else
                        throw new InputFormatException("header");
                }
                string content = reader.ReadLine();

                if (string.IsNullOrEmpty(content))
                {
                    throw new InputFormatException("data");
                }

                key = keyModel;
                forEncode = isEncrypt;
                data = content;
            }
        }
Ejemplo n.º 3
0
        public void EncryptDecryptTest()
        {
            var service = new RSACryptService(1024,_encode);
            const string testString = "rsa is complete";
            var key = new KeyModel {D = 44273, E = 65537, N = 49163};
            var encryptData = service.Encrypt(testString, key);
            string decryptString = service.Decrypt(encryptData, key);

            Assert.AreEqual(testString,decryptString);
        }
Ejemplo n.º 4
0
        public BigInteger[] Encrypt(string data, KeyModel key)
        {
            byte[] dataBytes = _encoding.GetBytes(data);
            var toReturn = new BigInteger[dataBytes.Length];
            for (int i = 0; i< dataBytes.Length;i++)
            {
                toReturn[i] = BigInteger.ModPow(dataBytes[i], key.E, key.N);
            }

            return toReturn;
        }
Ejemplo n.º 5
0
 public string Decrypt(BigInteger[] encryptedData, KeyModel key)
 {
     var dataBytes = new byte[encryptedData.Length];
     for (int i = 0; i < encryptedData.Length; i++)
     {
         var decodeNum = BigInteger.ModPow(encryptedData[i], key.D, key.N);
         dataBytes[i] = decodeNum.ToByteArray()[0];
     }
     string result = _encoding.GetString(dataBytes);
     return result;
 }
Ejemplo n.º 6
0
 public static string SecretKeyAsString(KeyModel model)
 {
     bool isDefined = model.D != default(BigInteger) && model.N != default(BigInteger);
     return isDefined ? String.Format("Secret key (d,n): {0},{1}{2}", model.D, model.N, Environment.NewLine) : null;
 }
Ejemplo n.º 7
0
 public static string PublicKeyAsString(KeyModel model)
 {
     bool isDefined = model.E != default(BigInteger) && model.N != default(BigInteger);
     return isDefined ? String.Format("Public key (e,n): {0},{1}{2}", model.E, model.N, Environment.NewLine) : null;
 }
Ejemplo n.º 8
0
 private void WriteHeader(KeyModel key)
 {
     string writeData = "";
     writeData += RSAHelper.PublicKeyAsString(key);
     writeData += RSAHelper.SecretKeyAsString(key);
     ToOutput(writeData, false);
 }
Ejemplo n.º 9
0
 private void ResultToOutput(string data, KeyModel key)
 {
     WriteHeader(key);
     ToOutput(data, true);
 }