コード例 #1
0
ファイル: Encryption.cs プロジェクト: iot369/CSFramework
        private static String _enCrypt(SymmetricAlgorithm Algorithm, String ValueToEnCrypt)
        {
            // 将字符串保存到字节数组中
            Byte [] InputByteArray = Encoding.UTF8.GetBytes(ValueToEnCrypt);

            // 获得需要的密钥
            String EncryptionKey = EncryptionHelper.EncryptionKey;

            // 创建一个key.
            Byte [] Key = ASCIIEncoding.ASCII.GetBytes(EncryptionKey);
            Algorithm.Key = (Byte [])ArrayFunctions.ReDim(Key, Algorithm.Key.Length);
            Algorithm.IV  = (Byte [])ArrayFunctions.ReDim(Key, Algorithm.IV.Length);

            MemoryStream MemStream  = new MemoryStream();
            CryptoStream CrypStream = new CryptoStream(MemStream, Algorithm.CreateEncryptor(), CryptoStreamMode.Write);

            // Write the byte array into the crypto stream( It will end up in the memory stream).
            CrypStream.Write(InputByteArray, 0, InputByteArray.Length);
            CrypStream.FlushFinalBlock();

            // Get the data back from the memory stream, and into a string.
            StringBuilder StringBuilder = new StringBuilder();

            for (Int32 i = 0; i < MemStream.ToArray().Length; i++)
            {
                Byte ActualByte = MemStream.ToArray()[i];

                // Format the actual byte as HEX.
                StringBuilder.AppendFormat("{0:X2}", ActualByte);
            }

            return(StringBuilder.ToString());
        }
コード例 #2
0
ファイル: Encryption.cs プロジェクト: iot369/CSFramework
        private static String _deCrypt(SymmetricAlgorithm Algorithm, String ValueToDeCrypt)
        {
            // Put the input string into the byte array.
            Byte [] InputByteArray = new Byte[ValueToDeCrypt.Length / 2];

            for (Int32 i = 0; i < ValueToDeCrypt.Length / 2; i++)
            {
                Int32 Value = (Convert.ToInt32(ValueToDeCrypt.Substring(i * 2, 2), 16));
                InputByteArray[i] = (Byte)Value;
            }

            // Create the crypto objects.
            String EncryptionKey = EncryptionHelper.EncryptionKey;

            // Create the key.
            Byte [] Key = ASCIIEncoding.ASCII.GetBytes(EncryptionKey);
            Algorithm.Key = (Byte [])ArrayFunctions.ReDim(Key, Algorithm.Key.Length);
            Algorithm.IV  = (Byte [])ArrayFunctions.ReDim(Key, Algorithm.IV.Length);

            MemoryStream MemStream  = new MemoryStream();
            CryptoStream CrypStream = new CryptoStream(MemStream, Algorithm.CreateDecryptor(), CryptoStreamMode.Write);

            // Flush the data through the crypto stream into the memory stream.
            CrypStream.Write(InputByteArray, 0, InputByteArray.Length);
            CrypStream.FlushFinalBlock();

            // Get the decrypted data back from the memory stream.
            StringBuilder StringBuilder = new StringBuilder();

            for (Int32 i = 0; i < MemStream.ToArray().Length; i++)
            {
                StringBuilder.Append((Char)MemStream.ToArray()[i]);
            }

            return(StringBuilder.ToString());
        }