Exemplo n.º 1
0
        private void GenerateMatrixKey()
        {
            if (!string.IsNullOrEmpty(CipherKey))
            {
                string[] words = CipherKey.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                byte[]   bytes = new byte[16];

                for (var i = 0; i < words.Length; i++)
                {
                    bytes[i] = byte.Parse(words[i]);
                }

                for (int i = 0; i < 4; i++)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        int currentIndex = (i * 4) + j;
                        Key[j, i] = bytes[currentIndex];
                    }
                }
            }
            else
            {
                throw new Exception("Não foi informada uma chave de criptografia!");
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Sync a key in passive mode (from Server-side)
 /// </summary>
 public async Task SaveKeyAsync(CipherKey key)
 {
     if (key != null)
     {
         // Save to Memory Cache
         await this.SaveKeyAsync(key.KeyType, key);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Extract a KeyParams and CipherKey
        /// </summary>
        ///
        /// <param name="KeyHeader">The <see cref="VTDev.Libraries.CEXEngine.Crypto.Processing.Structure.CipherKey"/> that receives the cipher description, key id, and extension key</param>
        /// <param name="KeyParam">The <see cref="VTDev.Libraries.CEXEngine.Crypto.Common.KeyParams"/> container that receives the key material from the file</param>
        ///
        /// <exception cref="CryptoProcessingException">Thrown if the key file could not be found or a Header parameter does not match the keystream length</exception>
        public void Extract(out CipherKey KeyHeader, out KeyParams KeyParam)
        {
            m_keyStream.Seek(0, SeekOrigin.Begin);
            KeyHeader = new CipherKey(m_keyStream);
            CipherDescription dsc = KeyHeader.Description;

            if (m_keyStream.Length < dsc.KeySize + dsc.IvSize + dsc.MacKeySize + CipherKey.GetHeaderSize())
            {
                throw new CryptoProcessingException("KeyFactory:Extract", "The size of the key file does not align with the CipherKey sizes! Key is corrupt.", new ArgumentOutOfRangeException());
            }

            m_keyStream.Seek(CipherKey.GetHeaderSize(), SeekOrigin.Begin);
            KeyParam = KeyParams.DeSerialize(m_keyStream);
        }
        /// <summary>
        /// Create RSA key
        /// </summary>
        /// <param name="meta">Key's metadata</param>
        /// <returns>Cipherkey object</returns>
        public async Task <CipherKey> CreateKeyAsync()
        {
            var keys       = RsaKeyGenerator.Pkcs8Key(2048, false);
            var privateKey = keys[0];
            var publicKey  = keys[1];
            var key        = new CipherKey()
            {
                Id         = Guid.NewGuid().ToString(),
                KeyType    = KeyTypeEnum.RSA,
                PublicKey  = publicKey,
                PrivateKey = privateKey
            };

            return(await Task.FromResult(key));
        }
Exemplo n.º 5
0
        private void generateKey(String key)
        {
            shiftKey1 = (int)jaftKey[0];

            for (int i = 0; i < blockKey[0].key.Length; i++)
            {
                if (i < key.Length)
                {
                    blockKey[0].key[i] = key[i];
                }
                else
                {
                    blockKey[0].key[i] = ' ';
                }
            }

            for (int i = 1; i < 7; i++)
            {
                blockKey[i] = new CipherKey();
                roundKey(i);
            }

            shiftKey7 = (int)blockKey[6].key[3];
        }
Exemplo n.º 6
0
        /// <summary>
        /// Create a single use key file using a <see cref="KeyParams"/> containing the key material, and a <see cref="VTDev.Libraries.CEXEngine.Crypto.Common.CipherDescription"/> containing the cipher implementation details
        /// </summary>
        ///
        /// <param name="Description">The <see cref="VTDev.Libraries.CEXEngine.Crypto.Common.CipherDescription">Cipher Description</see> containing the cipher details</param>
        /// <param name="KeyParam">An initialized and populated key material container; must include a 16 byte populated ExtKey property</param>
        ///
        /// <exception cref="CryptoProcessingException">Thrown if a KeyParams member is null, but specified in the Header or a Header parameter does not match a KeyParams value</exception>
        public void Create(CipherDescription Description, KeyParams KeyParam)
        {
            if (KeyParam.Key == null)
            {
                throw new CryptoProcessingException("KeyFactory:Create", "The key can not be null!", new ArgumentNullException());
            }

            if (KeyParam.Key.Length != Description.KeySize)
            {
                throw new CryptoProcessingException("KeyFactory:Create", "The key parameter does not match the key size specified in the Header!", new ArgumentOutOfRangeException());
            }

            if (Description.IvSize > 0 && KeyParam.IV != null)
            {
                if (KeyParam.IV.Length != Description.IvSize)
                {
                    throw new CryptoProcessingException("KeyFactory:Create", "The KeyParam IV size does not align with the IVSize setting in the Header!", new ArgumentOutOfRangeException());
                }
            }
            if (Description.MacKeySize > 0)
            {
                if (KeyParam.IKM == null)
                {
                    throw new CryptoProcessingException("KeyFactory:Create", "Digest key is specified in the header MacSize, but is null in KeyParam!", new ArgumentNullException());
                }
                if (KeyParam.IKM.Length != Description.MacKeySize)
                {
                    throw new CryptoProcessingException("KeyFactory:Create", "Header MacSize does not align with the size of the KeyParam IKM!", new ArgumentOutOfRangeException());
                }
            }

            byte[] hdr = new CipherKey(Description).ToBytes();
            m_keyStream.Write(hdr, 0, hdr.Length);
            byte[] key = ((MemoryStream)KeyParams.Serialize(KeyParam)).ToArray();
            m_keyStream.Write(key, 0, key.Length);
        }
Exemplo n.º 7
0
        private void CipherKeyTest()
        {
            CipherDescription ds = new CipherDescription(
                SymmetricEngines.RHX,
                192,
                IVSizes.V128,
                CipherModes.CTR,
                PaddingModes.PKCS7,
                BlockSizes.B128,
                RoundCounts.R22,
                Digests.Skein512,
                64,
                Digests.SHA512);

            CSPPrng rnd = new CSPPrng();

            byte[] id = new byte[16];
            byte[] ek = new byte[16];
            rnd.GetBytes(id);
            rnd.GetBytes(ek);

            // test serialization
            CipherKey ck = new CipherKey(ds, id, ek);

            byte[]    sk  = ck.ToBytes();
            CipherKey ck2 = new CipherKey(sk);

            if (!ck.Equals(ck2))
            {
                throw new Exception("KeyFactoryTest: CipherKey serialization has failed!");
            }

            MemoryStream mk  = ck.ToStream();
            CipherKey    ck3 = new CipherKey(mk);

            if (!ck.Equals(ck3))
            {
                throw new Exception("KeyFactoryTest: CipherKey serialization has failed!");
            }

            // test access funcs
            CipherKey.SetCipherDescription(mk, ds);
            CipherDescription ds2 = CipherKey.GetCipherDescription(mk);

            if (!ck.Description.Equals(ds2))
            {
                throw new Exception("KeyFactoryTest: CipherKey access has failed!");
            }

            rnd.GetBytes(ek);
            CipherKey.SetExtensionKey(mk, ek);
            if (!Evaluate.AreEqual(CipherKey.GetExtensionKey(mk), ek))
            {
                throw new Exception("KeyFactoryTest: CipherKey access has failed!");
            }

            rnd.GetBytes(id);
            CipherKey.SetKeyId(mk, id);
            if (!Evaluate.AreEqual(CipherKey.GetKeyId(mk), id))
            {
                throw new Exception("KeyFactoryTest: CipherKey access has failed!");
            }
        }