Ejemplo n.º 1
0
 public void setCryptKey(byte[] encrypt)
 {
     m_cryptAlgorithm = CryptAlgorithm.DES;
     m_cryptKey       = encrypt;
     Dec.DES_set_key_unchecked(m_cryptKey, m_cryptKeyArr[(int)CryptAlgorithm.DES] as DES_key_schedule);
 }
 //根据加密算法 截取 keyData
 //DES 密钥要求为8位 3DES的密钥要求为3*8
 private byte[] MakeKey(byte[] keyData, CryptAlgorithm alg)
 {
     byte[] key = keyData;
     if (keyData.Length > 8)
     {
         if (CryptAlgorithm.DES == alg)
         {
             key = new byte[8];
             Array.Copy(keyData, 0, key, 0, 8);
         }
         if (CryptAlgorithm.TRIPLE_DES == alg)
         {
             if (keyData.Length > 24)
             {
                 key = new byte[24];
                 Array.Copy(keyData, 0, key, 0, 24);
             }
         }
     }
     return key;
 }
Ejemplo n.º 3
0
 public void setCryptKey(byte[] encrypt)
 {
     m_cryptAlgorithm = CryptAlgorithm.DES;
     m_cryptKey = encrypt;
     Dec.DES_set_key_unchecked(m_cryptKey, m_cryptKeyArr[(int)CryptAlgorithm.DES] as DES_key_schedule);
 }
        private byte[] Encrypt(byte[] keyData, byte[] sourceData, CryptAlgorithm alg)
        {
            byte[] result = null;
            KeyParameter keyParam = null;
            IBufferedCipher engine = null;
            switch (alg)
            {
                case CryptAlgorithm.TRIPLE_DES:
                    keyParam = new DesEdeParameters(keyData);
                    engine = CipherUtilities.GetCipher("DESede/CBC/PKCS7PADDING");
                    engine.Init(true, keyParam);
                    result = engine.DoFinal(sourceData);
                    break;
                case CryptAlgorithm.RSA:
                    throw new NotSupportedException("CryptAlgorithm.RSA NOT SUPPORT YET");
                case CryptAlgorithm.DES:
                default://默认DES
                    keyParam = new DesParameters(keyData);
                    engine = CipherUtilities.GetCipher("DES/ECB/PKCS7PADDING");
                    engine.Init(true, keyParam);
                    result = engine.DoFinal(sourceData);
                    break;
            }

            return result;
        }