Пример #1
0
        public byte[] EncryptFile(byte[] fileBytes, byte[] key)
        {
            Initialize(fileBytes.Length, key);

            byte[] cipherText = new byte[fileBytes.Length];

            for (int i = 0; i < numOfFileChunks; i++)
            {
                MakeNonceAndCounter();
                byte[] output = cryptoAlgorithm.Encrypt(key, nonceAndCounter);

                for (int j = 0; j < output.Length; j++)
                {
                    cipherText[(i * (cryptoAlgorithm.OutputLength / 8)) + j] = (byte)(fileBytes[(i * (cryptoAlgorithm.OutputLength / 8)) + j] ^ output[j]);
                }

                counter++;
            }

            // treba i ostatak kriptovati
            int restOfBytes = fileBytes.Length % (cryptoAlgorithm.OutputLength / 8);

            MakeNonceAndCounter();
            byte[] lastOutput = cryptoAlgorithm.Encrypt(key, nonceAndCounter);
            for (int i = 0; i < restOfBytes; i++)
            {
                //int startOfLastChunk = (numOfFileChunks - 1) * (cryptoAlgorithm.OutputLength/8);
                int startOfLastChunk = numOfFileChunks * (cryptoAlgorithm.OutputLength / 8);
                cipherText[startOfLastChunk + i] = (byte)(fileBytes[startOfLastChunk + i] ^ lastOutput[i]);
            }

            return(cipherText);
        }
Пример #2
0
        public IActionResult Post([FromBody] RegistryCredentials credentials)
        {
            // must specify a registry
            if (string.IsNullOrEmpty(credentials.Registry))
            {
                return(Unauthorized());
            }

            // deny requests for foreign instances, if configured
            if (!string.IsNullOrEmpty(Config.Catalog?.Registry) && credentials.Registry.ToLowerInvariant() != Config.Catalog.Registry.ToLowerInvariant())
            {
                return(Unauthorized());
            }
            try
            {
                var handler = new AuthHandler(_Cache);
                handler.Login(credentials.Registry, credentials.Username, credentials.Password);
                var json       = JsonConvert.SerializeObject(credentials);
                var cipherText = _Crypto.Encrypt(json);

                return(Ok(new
                {
                    token = Jose.JWT.Encode(new Token
                    {
                        Crd = cipherText,
                        Usr = credentials.Username,
                        Reg = credentials.Registry,
                        Iat = DateTimeOffset.UtcNow.ToUnixTimeSeconds(),
                        Exp = DateTimeOffset.UtcNow.ToUnixTimeSeconds() + Config.Security.TokenLifetime
                    }, _Crypto.ToDotNetRSA(), Jose.JwsAlgorithm.RS256)
                }));
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Error authenticating token request.");
                return(Unauthorized());
            }
        }
Пример #3
0
 private string EncryptValue(string text)
 {
     return(_crypto.Encrypt(text));
 }