Esempio n. 1
0
 private void initKey()
 {
     try
     {
         /**
          * 获取秘钥
          * 没有秘钥则产生一对秘钥,并用非明文方式保存秘钥
          * */
         String strKey = FileReadWrite.Read();
         if (String.IsNullOrEmpty(strKey))
         {
             Key    = AesCryptoHelper.CreateKeyAndIv();
             strKey = SerializationHelper.Serialization(Key);
             strKey = AesCryptoHelper.Encrypt(strKey, keyEncryptorKey);
             FileReadWrite.Write(strKey);
         }
         else
         {
             strKey = AesCryptoHelper.Decrypt(strKey, keyEncryptorKey);
             Key    = SerializationHelper.Deserialization(strKey);
         }
     }
     catch (Exception ex)
     {
         var entity = LogEntityFactory.Create(String.Format("秘钥初始化失败:{0}", ex.ToString()),
                                              LogTypeFacotry.CreateExceptionLogType(),
                                              LogLevelFactory.CreateGravenessLogLevel());
         log.SaveLog(entity);
         throw new Exception("秘钥初始化失败,秘钥文件被破坏!");
     }
 }
Esempio n. 2
0
        public async Task SendMessage(string user, string message)
        {
            var aesDecriptedMesage = AesCryptoHelper.DecryptString_Aes(message);

            SaveMessageToLog(user, aesDecriptedMesage);

            await Clients.All.SendAsync("ReceiveMessage", user, aesDecriptedMesage);
        }
 protected virtual byte[] Encryptor(byte[] bytes)
 {
     /**
      * 对数据加密
      * 用密码加密数据
      * 对数据签名
      * */
     bytes = Cryptor.Encryptor(bytes);
     bytes = AesCryptoHelper.Encrypt(bytes, GetKeyBytesByPasword(), null);
     bytes = Sign.Sign(bytes, GetSignBytes());
     return(bytes);
 }
        public async Task <IActionResult> SendMessage([FromBody] Message message)
        {
            connection = new HubConnectionBuilder()
                         .WithUrl(hubUrl)
                         .Build();

            await connection.StartAsync();

            var aesEncriptedMesage = AesCryptoHelper.EncryptString_Aes(message.MessageContent);
            await connection.InvokeAsync("SendMessage", message.User, aesEncriptedMesage);

            return(new OkResult());
        }
        private async void sendButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var aesEncriptedMesage = AesCryptoHelper.EncryptString_Aes(messageTextBox.Text);

                await connection.InvokeAsync("SendMessage", userTextBox.Text, aesEncriptedMesage);
            }
            catch (Exception ex)
            {
                messagesList.Items.Add(ex.Message);
            }
        }
Esempio n. 6
0
        private string GenerateMasterPassword()
        {
            var buf = AesCryptoHelper.CreateRandomBuf(32);

            for (int i = 0; i < 32; i++)
            {
                if (buf[i] == 0)
                {
                    buf[i] = 0xff;
                }
            }
            var pass = ConvertUtils.ByteArrayToHexString(buf);

            return(pass);
        }
        protected virtual byte[] Decryptor(byte[] bytes)
        {
            /**
             * 验证签名
             * 用密码解密数据
             * 对数据码解
             * */

            if (!Sign.Verify(bytes, GetSignBytes(), out bytes))
            {
                throw new Exception("密文被篡改!");
            }

            try
            {
                bytes = AesCryptoHelper.Decrypt(bytes, GetKeyBytesByPasword(), null);
            }
            catch (Exception ex)
            {
                throw new Exception("密码不正确!", ex.InnerException);
            }
            bytes = Cryptor.Decryptor(bytes);
            return(bytes);
        }
Esempio n. 8
0
 public byte[] Encryptor(byte[] bytes)
 {
     return(AesCryptoHelper.Encrypt(bytes, Convert.FromBase64String(Key.Key), Convert.FromBase64String(Key.Value)));
 }