Beispiel #1
0
 public void Encrypt(ISymmetric testObject, int keySize)
 {
     Assert.NotNull(testObject.Encrypt(
                        new byte[] { 0, 1, 2, 3, 4, 5 },
                        testObject.CreateKey(),
                        "Salt".ToByteArray(),
                        HashingAlgorithms.SHA512,
                        2,
                        testObject.CreateInitialVector(),
                        keySize));
 }
Beispiel #2
0
        /// <summary>
        /// Encrypts a byte array
        /// </summary>
        /// <param name="Data">Data to be encrypted</param>
        /// <param name="Key">Password to encrypt with</param>
        /// <param name="InitialVector">Needs to be 16 ASCII characters long</param>
        /// <param name="KeySize">
        /// Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)
        /// </param>
        /// <param name="Algorithm">Algorithm</param>
        /// <returns>An encrypted byte array</returns>
        public byte[] Encrypt(byte[] Data, DeriveBytes Key,
                              string Algorithm     = "AES",
                              string InitialVector = "OFRna73m*aze01xY",
                              int KeySize          = 256)
        {
            Contract.Requires <NullReferenceException>(SymmetricAlgorithms != null, "SymmetricAlgorithms");
            ISymmetric Found = SymmetricAlgorithms.FirstOrDefault(x => x.CanHandle(Algorithm));

            if (Found == null)
            {
                throw new ArgumentException(Algorithm + " not found");
            }
            return(Found.Encrypt(Data, Key, Algorithm, InitialVector, KeySize));
        }
Beispiel #3
0
        public async Task SymmetricTest()
        {
            ISymmetric alg = SymmetricBuilder.CreateAesFixed();

            using var msInput  = new MemoryStream(Plaintext.GetBytes <UTF8Encoding>());
            using var msOutput = new MemoryStream();

            await alg.EncryptAsync(msInput, msOutput, "Smith".GetBytes <UTF8Encoding>());

            msOutput.Position = 0;
            using var msFinal = new MemoryStream();
            await alg.DecryptAsync(msOutput, msFinal, "Smith".GetBytes <UTF8Encoding>());

            Assert.AreEqual(Plaintext, msFinal.ToArray().GetString <UTF8Encoding>());
        }
Beispiel #4
0
        /// <summary>
        /// Encrypts a byte array
        /// </summary>
        /// <param name="Data">Data to be encrypted</param>
        /// <param name="Key">Password to encrypt with</param>
        /// <param name="Salt">Salt to encrypt with</param>
        /// <param name="HashAlgorithm">Can be either SHA1 or MD5</param>
        /// <param name="PasswordIterations">Number of iterations to do</param>
        /// <param name="InitialVector">Needs to be 16 ASCII characters long</param>
        /// <param name="KeySize">
        /// Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)
        /// </param>
        /// <param name="Algorithm">Algorithm</param>
        /// <returns>An encrypted byte array</returns>
        public byte[] Encrypt(byte[] Data, string Key,
                              string Algorithm,
                              string Salt            = "Kosher",
                              string HashAlgorithm   = "SHA1",
                              int PasswordIterations = 2,
                              string InitialVector   = "OFRna73m*aze01xY",
                              int KeySize            = 256)
        {
            Contract.Requires <NullReferenceException>(SymmetricAlgorithms != null, "SymmetricAlgorithms");
            ISymmetric Found = SymmetricAlgorithms.FirstOrDefault(x => x.CanHandle(Algorithm));

            if (Found == null)
            {
                throw new ArgumentException(Algorithm + " not found");
            }
            return(Found.Encrypt(Data, Key, Algorithm, Salt, HashAlgorithm, PasswordIterations, InitialVector, KeySize));
        }
Beispiel #5
0
        public DataRevController(
            IConfiguration configuration,
            IAsymmetric asymmetric,
            ISymmetric symmetric,
            V2RayDbContent db)
        {
            this.configuration = configuration;
            this.symmetric     = symmetric;
            this.asymmetric    = asymmetric;
            this.db            = db;
            string key = configuration.GetValue <string>("KeyFile");

            using (TextReader reader = System.IO.File.OpenText(key))
            {
                PemReader pem = new PemReader(reader);
                AsymmetricCipherKeyPair pair = (AsymmetricCipherKeyPair)pem.ReadObject();
                this.key = pair.Private;
            }
        }
Beispiel #6
0
        public void Decrypt(ISymmetric testObject, int keySize)
        {
            var Key = testObject.CreateKey();
            var IV  = testObject.CreateInitialVector();

            Assert.NotNull(testObject.Decrypt(
                               testObject.Encrypt(
                                   new byte[] { 0, 1, 2, 3, 4, 5 },
                                   (byte[])Key.Clone(),
                                   "Salt".ToByteArray(),
                                   HashingAlgorithms.SHA512,
                                   2,
                                   IV,
                                   keySize),
                               (byte[])Key.Clone(),
                               "Salt".ToByteArray(),
                               HashingAlgorithms.SHA512,
                               2,
                               IV,
                               keySize));
        }
Beispiel #7
0
        public TimeBasedService(
            IV2RayCollectService v2ray,
            IAsymmetric asymmetric,
            ISymmetric symmetric,
            IHttpClientFactory factory,
            IConfiguration configuration)
        {
            this.v2rayService = v2ray;
            this.http         = factory.CreateClient();
            this.asymmetric   = asymmetric;
            this.symmetric    = symmetric;
            string path = configuration.GetValue <string>("PublicKey");

            using (TextReader reader = File.OpenText(path))
            {
                PemReader pem       = new PemReader(reader);
                var       pemObject = pem.ReadObject();
                this.parameter = pemObject as AsymmetricKeyParameter;
            }
            http.BaseAddress = new Uri(configuration.GetConnectionString("DataServer"));
        }
Beispiel #8
0
 public void CreateKey(ISymmetric testObject, int keySize)
 {
     Assert.NotNull(testObject.CreateKey());
 }
Beispiel #9
0
 public void CreateInitialVector(ISymmetric testObject, int keySize)
 {
     Assert.NotNull(testObject.CreateInitialVector());
 }
Beispiel #10
0
 public ContextCrypto()
 {
     symmetric = new Symmetric();
 }