Ejemplo n.º 1
0
        protected static void Main(string[] args)
        {
            // This is the provided encoded input
            string keyString = "ABvAsOKcGXqc5uQ4O5Z53isJaH31Pa8+PeddljE4mSU=";

            Console.WriteLine("This is the encoded string: " + keyString);

            // Here I am decoding the encoded input
            byte[] data          = Encoding.UTF8.GetBytes(keyString);
            string decodedString = Encoding.Unicode.GetString(data);

            //printing the decoded text
            Console.WriteLine("\nThis is the encoded string decoded: " + decodedString);

            //creating TripleDes call name
            TripleDES decoder = TripleDES.Create();

            //Setting the key and vector parameters
            byte[] key     = Encoding.UTF8.GetBytes("0123456789ABCDEF");
            int    keySize = 128; // could not figure out where to place the key size :(

            byte[] vector = Encoding.UTF8.GetBytes("ABCDEFGH");

            // setting the decryptor to type ICryptoTransform
            ICryptoTransform decryptor;

            // creates the decryptor
            decryptor     = decoder.CreateDecryptor(key, vector);
            decodedString = decryptor.ToString();

            Console.WriteLine("");

            //Is supposed to print the results
            Console.WriteLine("This is the decoded string decrypted: " + decodedString);
        }
Ejemplo n.º 2
0
        public override void init(int mode, byte[] key, byte[] iv)
        {
            m_triDes = new TripleDESCryptoServiceProvider();
            m_triDes.Mode = CipherMode.CBC;
            m_triDes.Padding = PaddingMode.None;

            byte[] tmp;
            if (iv.Length > m_ivsize)
            {
                tmp = new byte[m_ivsize];
                Array.Copy(iv, 0, tmp, 0, tmp.Length);
                iv = tmp;
            }
            if (key.Length > m_bsize)
            {
                tmp = new byte[m_bsize];
                Array.Copy(key, 0, tmp, 0, tmp.Length);
                key = tmp;
            }

            try
            {
                m_cipher = (mode == ENCRYPT_MODE
                    ? m_triDes.CreateEncryptor(key, iv)
                    : m_triDes.CreateDecryptor(key, iv)
                    );
            }
            catch (Exception)
            {
                m_cipher = null;
            }
        }
Ejemplo n.º 3
0
        public static string Decryption(string CypherText)
        {
            byte[] b = Convert.FromBase64String(CypherText);
            System.Security.Cryptography.TripleDES des = CreateDES(key);
            ICryptoTransform ct = des.CreateDecryptor();

            byte[] output = ct.TransformFinalBlock(b, 0, b.Length);
            return(Encoding.Unicode.GetString(output));
        }
        public override byte[] DecryptKey(byte[] rgbEncryptedWrappedKeyData)
        {
            System.Security.Cryptography.TripleDES tdes = (System.Security.Cryptography.TripleDES) this.Key;
            if (((rgbEncryptedWrappedKeyData.Length != 0x20) && (rgbEncryptedWrappedKeyData.Length != 40)) && (rgbEncryptedWrappedKeyData.Length != 0x30))
            {
                throw new CryptographicException("BadSizeForKeyToBeWrapped");
            }
            tdes.Mode    = CipherMode.CBC;
            tdes.Padding = PaddingMode.None;
            ICryptoTransform transform = tdes.CreateDecryptor(tdes.Key, TripleDESKeyExchangeFormatter.s_rgbTripleDES_KW_IV);

            byte[] buffer1 = transform.TransformFinalBlock(rgbEncryptedWrappedKeyData, 0, rgbEncryptedWrappedKeyData.Length);
            Array.Reverse(buffer1);
            byte[] buffer2 = new byte[8];
            Buffer.BlockCopy(buffer1, 0, buffer2, 0, 8);
            byte[] buffer3 = new byte[buffer1.Length - buffer2.Length];
            Buffer.BlockCopy(buffer1, 8, buffer3, 0, buffer3.Length);
            ICryptoTransform transform2 = tdes.CreateDecryptor(tdes.Key, buffer2);

            byte[] buffer4 = transform2.TransformFinalBlock(buffer3, 0, buffer3.Length);
            byte[] buffer5 = new byte[buffer4.Length - 8];
            Buffer.BlockCopy(buffer4, 0, buffer5, 0, buffer5.Length);
            SHA1 sha1 = SHA1.Create();

            byte[] buffer6 = sha1.ComputeHash(buffer5);
            int    bufLen  = buffer5.Length;

            for (int keyBufPos = 0; bufLen < buffer4.Length; keyBufPos++)
            {
                if (buffer4[bufLen] != buffer6[keyBufPos])
                {
                    throw new CryptographicException("BadSizeForKeyToBeUnwrapped");
                }
                bufLen++;
            }
            return(buffer5);
        }
Ejemplo n.º 5
0
 public override string Decrypt(string cipherText, string key, string iv)
 {
     try
     {
         System.Security.Cryptography.TripleDES tdes = System.Security.Cryptography.TripleDES.Create();
         byte[] inputBytes = Convert.FromBase64String(cipherText);
         tdes.Key = Convert.FromBase64String(key);
         tdes.IV  = Convert.FromBase64String(iv);
         MemoryStream ms = new MemoryStream();
         CryptoStream cs = new CryptoStream(ms, tdes.CreateDecryptor(), CryptoStreamMode.Write);
         cs.Write(inputBytes, 0, inputBytes.Length);
         cs.FlushFinalBlock();
         return(Encoding.UTF8.GetString(ms.ToArray()));
     }
     catch (Exception e)
     {
         return("\nERROR: " + e.Message);
     }
 }
Ejemplo n.º 6
0
        public override void init(int mode, byte[] key, byte[] iv)
        {
            if (mode != ENCRYPT_MODE && mode != DECRYPT_MODE) throw new ArgumentOutOfRangeException();
            ms = new PipedMemoryStream();
            desm = TripleDES.Create();
            desm.KeySize = BlockSize * 8;
            desm.Padding = PaddingMode.None;
            ICryptoTransform ict;
            if (mode == ENCRYPT_MODE)
            {
                ict = desm.CreateEncryptor(key, iv);
            }
            else
            {
                ict = desm.CreateDecryptor(key, iv);
            }

            cs = new CryptoStream(ms, ict, CryptoStreamMode.Write);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="cryptoText">密文</param>
        /// <param name="decryptKey">解密密钥</param>
        /// <returns></returns>
        public string Decrypt(string cryptoText, string decryptKey)
        {
            if (!string.IsNullOrEmpty(decryptKey))
            {
                this.key    = decryptKey;
                mCrypto.Key = GetLegalKey();
            }

            byte[] arrCryptoText = Convert.FromBase64String(cryptoText);
            using (MemoryStream stream = new MemoryStream(arrCryptoText, 0, arrCryptoText.Length))
            {
                ICryptoTransform decryptor = mCrypto.CreateDecryptor();
                using (CryptoStream cStream = new CryptoStream(stream, decryptor, CryptoStreamMode.Read))
                {
                    StreamReader reader = new StreamReader(cStream);
                    return(reader.ReadToEnd());
                }
            }
        }
Ejemplo n.º 8
0
        public bool Decrypt(ref string value)
        {
            Byte[] tmp    = Convert.FromBase64String(value);
            string result = string.Empty;

            System.Security.Cryptography.TripleDES tripleDES = System.Security.Cryptography.TripleDES.Create();
            ICryptoTransform decryptor = tripleDES.CreateDecryptor(key, iv);

            using (MemoryStream ms = new MemoryStream(tmp)) {
                try {
                    using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read)) {
                        StreamReader reader = new StreamReader(cs);
                        result = reader.ReadLine();
                    }
                } catch (System.Security.Cryptography.CryptographicException ex) {
                    value = ex.Message;
                    return(false);
                }
            }
            tripleDES.Clear();
            value = result;
            return(true);
        }
 public override ICryptoTransform CreateDecryptor() => _impl.CreateDecryptor();
Ejemplo n.º 10
0
        public override void init(int mode, byte[] key, byte[] iv)
        {
            triDes = new TripleDESCryptoServiceProvider();
            triDes.Mode = CipherMode.CBC;
            triDes.Padding = PaddingMode.None;
            //String pad="NoPadding";
            //if(padding) pad="PKCS5Padding";
            byte[] tmp;
            if (iv.Length > ivsize)
            {
                tmp = new byte[ivsize];
                Array.Copy(iv, 0, tmp, 0, tmp.Length);
                iv = tmp;
            }
            if (key.Length > bsize)
            {
                tmp = new byte[bsize];
                Array.Copy(key, 0, tmp, 0, tmp.Length);
                key = tmp;
            }

            try
            {
                //      cipher=javax.crypto.Cipher.getInstance("DESede/CBC/"+pad);
                /*
                      // The following code does not work on IBM's JDK 1.4.1
                      SecretKeySpec skeySpec = new SecretKeySpec(key, "DESede");
                      cipher.init((mode==ENCRYPT_MODE?
                           javax.crypto.Cipher.ENCRYPT_MODE:
                           javax.crypto.Cipher.DECRYPT_MODE),
                          skeySpec, new IvParameterSpec(iv));
                */
                //      DESedeKeySpec keyspec=new DESedeKeySpec(key);
                //      SecretKeyFactory keyfactory=SecretKeyFactory.getInstance("DESede");
                //      SecretKey _key=keyfactory.generateSecret(keyspec);
                //      cipher.init((mode==ENCRYPT_MODE?
                //		   javax.crypto.Cipher.ENCRYPT_MODE:
                //		   javax.crypto.Cipher.DECRYPT_MODE),
                //		  _key, new IvParameterSpec(iv));
                cipher = (mode == ENCRYPT_MODE ?
                                                   triDes.CreateEncryptor(key, iv) :
                                                                                       triDes.CreateDecryptor(key, iv));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                cipher = null;
            }
        }