Exemplo n.º 1
0
        /// <summary>
        /// Client side encryption
        /// </summary>
        public CryptoMC(Stream baseStream, EncryptionRequest req)
        {
            this.BaseStream = baseStream;
            RijndaelManaged rm = new RijndaelManaged();

            rm.KeySize = 128;
            rm.GenerateKey();
            SharedKey = rm.Key;
            InitCiphers();

            //Encrypt shared key using public key in req
            AsymmetricKeyParameter asymmetricKeyParameter = PublicKeyFactory.CreateKey(req.PublicKey);
            RsaKeyParameters       key = (RsaKeyParameters)asymmetricKeyParameter;
            //SubjectPublicKeyInfo s = new SubjectPublicKeyInfo(AlgorithmIdentifier.Der, req.PublicKey);
            //RsaKeyParameters key = (RsaKeyParameters)PublicKeyFactory.CreateKey(s);

            Pkcs1Encoding padding = new Pkcs1Encoding(new RsaEngine());

            padding.Init(true, key);
            SharedKeyEncrypted = padding.ProcessBlock(SharedKey, 0, SharedKey.Length);

            Pkcs1Encoding padding2 = new Pkcs1Encoding(new RsaEngine());

            padding2.Init(true, keyParameters);
            TestEncrypted = padding.ProcessBlock(req.VerifyToken, 0, req.VerifyToken.Length);
        }
Exemplo n.º 2
0
        public void Encrypt(EncryptionRequest req)
        {
            string cmdArgs;

            try
            {
                cmdArgs = req.Execute();
                using (System.IO.FileStream fsEncryptFile = new System.IO.FileStream(this.fullPathToPgpRoot + "\\" + req.File.Name, System.IO.FileMode.Create))
                    using (System.IO.FileStream fsPublicKeyFile = new System.IO.FileStream(this.fullPathToPgpRoot + "\\" + req.CryptionArgs.PublicKey.Name, System.IO.FileMode.Create))
                    {
                        fsEncryptFile.Write(req.File.Content, 0, req.File.Content.Length);
                        fsPublicKeyFile.Write(req.CryptionArgs.PublicKey.Content, 0, req.CryptionArgs.PublicKey.Content.Length);
                    }
            }
            catch (Exception)
            {
                throw;
            }

            if (this.ExecuteGnuGpg(cmdArgs))
            {
                System.IO.FileInfo encryptedFile = new System.IO.FileInfo(String.Format("{0}\\{1}", this.fullPathToPgpRoot, req.File.Name + ".gpg"));
                if (encryptedFile.Exists)
                {
                    var callback = this.clientCallback;
                    if (callback != null)
                    {
                        byte[] fileContent = System.IO.File.ReadAllBytes(encryptedFile.FullName);
                        callback.OnFileEncrypted(new FileCryptionEvent {
                            File = new File(encryptedFile.Name, fileContent), ExecutedCommand = cmdArgs
                        });
                    }
                }
            }
        }
Exemplo n.º 3
0
        private void EncryptClick(object sender, RoutedEventArgs e)
        {
            System.IO.FileInfo file = new System.IO.FileInfo(@"D:\Users\Goose\Documents\helloWorld.txt");

            string result = null;

            System.IO.DirectoryInfo assem = new System.IO.DirectoryInfo(Assembly.GetExecutingAssembly().FullName);
            using (System.IO.Stream stream = new System.IO.FileStream(assem.Parent.FullName + "\\Keys\\FileCryptographyService.Public.gpg", System.IO.FileMode.Open))
                using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))
                {
                    result = reader.ReadToEnd();
                }

            EncryptionRequest req = new EncryptionRequest();

            req.File = new File {
                Name = file.Name, Content = System.IO.File.ReadAllBytes(file.FullName)
            };
            req.CryptionArgs           = new FileCryptionArgs();
            req.CryptionArgs.PublicKey = new KeyFile {
                Name = "FileCryptographyService.Public.gpg", Email = "*****@*****.**", Content = Encoding.ASCII.GetBytes(result)
            };

            this.svc.Encrypt(req);
            int dd = 5;
        }
Exemplo n.º 4
0
 public static void Encrypt()
 {
     EncryptionRequest request = new EncryptionRequest
     {
         RequestType    = CryptoRequestType.Encrypt,
         EncryptionType = Server.Cryptography.DataTypes.EncryptionType.AES,
         KeyEntityId    = Guid.Parse("78CD19D9-3D46-407C-81FC-ABB10118DDBA"),
         PlainText      = "Hello World!"
     };
     RestClientFactory client = new RestClientFactory("CryptographyApi");
     var response             = client.PostCall <EncryptionResponse, EncryptionRequest>
                                    (client.apiConfiguration.Apis["Encrypt"], request);
 }
Exemplo n.º 5
0
        public override void Handle(byte[] data, ClientConnectionInfo connection)
        {
            var encryptionRequest = new EncryptionRequest(data);

            if (encryptionRequest.PublicRSAKey.Length < 32)
            {
                _gameServer.PacketSenderManager.SendEncryptionResponse(connection, EncryptionResponseCode.INVALID_KEY, new byte[] { 0 }, new byte[] { 0 });
                return;
            }
            connection.Encryption.EnableRSAEncryption(encryptionRequest.PublicRSAKey);
            _gameServer.PacketSenderManager.SendEncryptionResponse(connection, EncryptionResponseCode.OK, _gameServer.EncryptionManager.GetKeyAES(), _gameServer.EncryptionManager.GetIVAes());
            connection.Encryption.DisableRSAEncryption();
            connection.Encryption.AESEncryptionEnabled = true;
        }
 public EncryptionResult Encrypt(EncryptionRequest encryptionRequest)
 {
     result = new EncryptionResult();
     try
     {
         using (FileManager fileManager = new FileManager())
         {
             FileManagerResult fileManagerResult = fileManager.GetStreamReaderText(encryptionRequest.OriginalFilePath, encryptionRequest.TextEncoding);
             if (fileManagerResult.IsFileStreamTextValid)
             {
                 if (string.IsNullOrEmpty(_key))
                 {
                     _key = EncryptionConfig.DefaultEncryptionKey;
                 }
                 result.EncryptedText       = Encrypt(fileManagerResult.FileStreamText, _key);
                 result.EncryptedToFilePath = encryptionRequest.EncryptToFilePath;
                 if (!string.IsNullOrEmpty(encryptionRequest.EncryptToFilePath))
                 {
                     SaveEncryptionToFile(encryptionRequest.EncryptToFilePath, result.EncryptedText, encryptionRequest.TextEncoding);
                     result.IsEncrypted = true;
                 }
             }
             else
             {
                 result.IsEncrypted = false;
                 result.Errors.Add(EncryptionManagerConstants.InvalidFileStream);
                 WriteToConsole(EncryptionManagerConstants.InvalidFileStream);
             }
         }
         return(result);
     }
     catch (Exception e)
     {
         result.IsEncrypted = false;
         result.Exceptions.Add(e);
         WriteToConsole(EncryptionManagerConstants.InvalidInput + e.Message);
         return(result);
     }
 }
Exemplo n.º 7
0
        //This is not needed by the client
        /// <summary>
        /// Registers an encryption object. This will request that the encryption be established by sending a message to the server.
        /// Do not try to register an encryption object if you are not connected.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="encryptorInstance"></param>
        /// <returns></returns>
        public bool Register <T>(T encryptorInstance, Action OnSuccess) where T : EncryptionBase
        {
            if (encryptorInstance == null)
            {
                ClassLogger.LogError("Cannot register a null encryption object.");
                return(false);
            }

            if (this.EncryptionRegister.HasKey(encryptorInstance.EncryptionTypeByte))
            {
                ClassLogger.LogError("Tried to register an already known encryption object.");
                return(false);
            }

            encryptorInstance.OnEstablished += OnSuccess;

            this.EncryptionRegister.Register(encryptorInstance, encryptorInstance.EncryptionTypeByte);

            PacketBase packet = new EncryptionRequest(encryptorInstance.EncryptionTypeByte, encryptorInstance.NetworkInitRequiredData());

            return(this.SendMessage(Packet.OperationType.Request, packet, (byte)InternalPacketCode.EncryptionRequest,
                                    Packet.DeliveryMethod.ReliableUnordered, 0, 0, true) != Packet.SendResult.FailedNotConnected);
        }
Exemplo n.º 8
0
        //This is not needed by the client
        /// <summary>
        /// Registers an encryption object. This will request that the encryption be established by sending a message to the server.
        /// Do not try to register an encryption object if you are not connected.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="encryptorInstance"></param>
        /// <returns></returns>
        public bool Register <T>(T encryptorInstance) where T : EncryptionBase
        {
            if (encryptorInstance == null)
            {
                ClassLogger.LogError("Cannot register a null encryption object.");
                return(false);
            }

            if (this.EncryptionRegister.HasKey(encryptorInstance.EncryptionTypeByte))
            {
                ClassLogger.LogError("Tried to register an already known encryption object.");
                return(false);
            }

            if (!isConnected || RecieverListener == null)
            {
                ClassLogger.LogError("Cannot register encryption objects when not connected.");
                return(false);
            }

            //Set the callback for when the server acknowledges our encryption request.
            encryptorInstance.OnEstablished += () =>
            {
                lock (networkIncomingEnqueueSyncObj)
                {
                    this.networkPackageQueue.Enqueue(() => { RecieverListener.OnStatusChange(StatusChange.EncryptionEstablished); });
                }
            };

            this.EncryptionRegister.Register(encryptorInstance, encryptorInstance.EncryptionTypeByte);

            PacketBase packet = new EncryptionRequest(encryptorInstance.EncryptionTypeByte, encryptorInstance.NetworkInitRequiredData());

            return(this.SendMessage(Packet.OperationType.Request, packet, (byte)InternalPacketCode.EncryptionRequest,
                                    Packet.DeliveryMethod.ReliableUnordered, 0, 0, true) != Packet.SendResult.FailedNotConnected);
        }
 public IActionResult Encrypt([FromBody] EncryptionRequest request)
 {
     return(Ok(SecretService.Encrypt(request.Payload, request.Key)));
 }
Exemplo n.º 10
0
 public void OnEncryptionRequest(EncryptionRequest packet)
 {
 }
Exemplo n.º 11
0
 private void InvokeEncryptionRequest(EncryptionRequest packet)
 {
     packetListener.OnEncryptionRequest(packet);
 }