Пример #1
0
        public string LoadKey(GeneralKeyType key)
        {
            try
            {
                switch (key.Type)
                {
                case "Public":
                    OpenParameter    = key.Param;
                    GeneralParameter = key.GeneralParameter;
                    break;

                case "Private":
                    SecretParameter  = key.Param;
                    GeneralParameter = key.GeneralParameter;
                    break;

                default:
                    throw new ApplicationException();
                }
                RegenerateSizeParameters();
                return(key.Type);
            }
            catch
            {
                throw new ApplicationException("Failed to load the key, perhaps the key does not match the selected algorithm");
            }
        }
Пример #2
0
        public static void WriteKeyToFile(IKey Key, string path)
        {
            #region SavePrivateKey

            GeneralKeyType privateKey = new GeneralKeyType
            {
                Type             = "Private",
                GeneralParameter = Key.GeneralParameter,
                Param            = Key.SecretParameter
            };
            string keyString = JsonSerializer.Serialize <GeneralKeyType>(privateKey);
            string result    = Base64ConverterClass.Base64Encode(keyString);
            File.WriteAllText(path + @"\privatekey", result);

            #endregion

            #region SavePublicKey

            GeneralKeyType publicKey = new GeneralKeyType
            {
                Type             = "Public",
                GeneralParameter = Key.GeneralParameter,
                Param            = Key.OpenParameter
            };
            keyString = JsonSerializer.Serialize(publicKey);
            result    = Base64ConverterClass.Base64Encode(keyString);
            File.WriteAllText(path + @"\publickey", result);

            #endregion
        }
Пример #3
0
 public static GeneralKeyType LoadKeyFromFile(string path)
 {
     try
     {
         string         content    = File.ReadAllText(path);
         string         decodedKey = Base64ConverterClass.Base64Decode(content);
         GeneralKeyType key        = JsonSerializer.Deserialize <GeneralKeyType>(decodedKey);
         return(key);
     }
     catch
     {
         throw new ApplicationException("Key is not valid!");
     }
 }