public static byte[] EncryptBytesToBytes_Aes(byte[] plainBytes, byte[] Key, byte[] IV)
 {
     if (plainBytes == null || plainBytes.Length <= 0)
     {
         FDebug.Log("密文无效,无法加密");
         return(null);
     }
     if (Key == null || Key.Length <= 0)
     {
         FDebug.Log("密钥无效,无法加密");
         return(null);
     }
     if (IV == null || IV.Length <= 0)
     {
         FDebug.Log("偏移量无效,无法加密");
         return(null);
     }
     byte[] encrypted;
     using (AesManaged aes = new AesManaged())
     {
         aes.Key = Key;
         aes.IV  = IV;
         ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
         using (MemoryStream msEncrypt = new MemoryStream())
         {
             using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
             {
                 csEncrypt.Write(plainBytes, 0, plainBytes.Length);
                 csEncrypt.FlushFinalBlock();
             }
             encrypted = msEncrypt.ToArray();;
         }
     }
     return(encrypted);
 }
Exemple #2
0
        public static IEnumerable <FileInfo> Searchfile(DirectoryInfo info)
        {
            var infos = info.GetFiles();

            foreach (var item in infos)
            {
                FDebug.Log(item.Name);
            }
            return(infos);
        }
Exemple #3
0
        public static IEnumerable <DirectoryInfo> SearchDirectory(DirectoryInfo info)
        {
            var infos = info.EnumerateDirectories();

            foreach (var item in infos)
            {
                FDebug.Log(item.Name);
            }
            return(infos);
        }
Exemple #4
0
        /// <summary>
        /// 检索指定目录下给定类型的文件
        /// </summary>
        /// <param name="TargetDirectory">路径</param>
        /// <param name="searchPattern">文件扩展名 列 .xlsx  .docx * ?</param>
        /// <returns></returns>
        public static IEnumerable <string> SearchFileForDirectory(string TargetDirectory, string searchPattern)
        {
            var files = Directory.EnumerateFiles(TargetDirectory, searchPattern, SearchOption.AllDirectories);

            foreach (var item in files)
            {
                FDebug.Log(item);
            }
            return(files);
        }
        /// <summary>
        /// 反序列化json字符串
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="JsonString">json字符串</param>
        /// <returns></returns>
        public static T JsonDeserializer <T>(string JsonString) where T : new()
        {
            T TT = new T();

            try
            {
                TT = JsonConvert.DeserializeObject <T>(JsonString);
            }
            catch (JsonSerializationException e)
            {
                FDebug.Log(e.Message);
            }
            return(TT);
        }
Exemple #6
0
        /// <summary>
        /// 读取文本文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string ReadTextfile(string path)
        {
            string t;

            if (File.Exists(path))
            {
                t = File.ReadAllText(path, Encoding.UTF8);
                return(t);
            }
            else
            {
                FDebug.Log("file {0} not found", path);
            }
            return(null);
        }
 /// <summary>
 /// 解密经过加密的Byte[]流文件
 /// </summary>
 /// <param name="EncryptedBytes">密文</param>
 /// <param name="Key">密钥</param>
 /// <param name="IV">加密偏移量</param>
 /// <returns>解密后的Byte[]流</returns>
 public static byte[] DecryptBytesFromBytes_Aes(byte[] EncryptedBytes, byte[] Key, byte[] IV)
 {
     if (EncryptedBytes == null || EncryptedBytes.Length <= 0)
     {
         FDebug.Log("密文无效,无法解密");
         return(null);
     }
     if (Key == null || Key.Length <= 0)
     {
         FDebug.Log("密钥无效,无法解密");
         return(null);
     }
     if (IV == null || IV.Length <= 0)
     {
         FDebug.Log("偏移量无效,无法解密");
         return(null);
     }
     byte[] DecrypteBytes = null;
     using (AesManaged aesAlg = new AesManaged())
     {
         aesAlg.Key = Key;
         aesAlg.IV  = IV;
         ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
         using (MemoryStream outs = new MemoryStream())
         {
             using (MemoryStream msDecrypt = new MemoryStream(EncryptedBytes))
             {
                 using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                 {
                     try
                     {
                         byte[] buffer = new byte[EncryptedBytes.Length];
                         int    len    = csDecrypt.Read(buffer, 0, buffer.Length);//len为解密流中数据的实际可用长度
                         DecrypteBytes = new byte[len];
                         Array.Copy(buffer, 0, DecrypteBytes, 0, len);
                     }
                     catch (Exception e)
                     {
                         FDebug.Log("解码密钥错误 :" + e.Message);
                     }
                 }
             }
         }
     }
     return(DecrypteBytes);
 }
        /// <summary>
        /// 对字符串进行加密
        /// </summary>
        /// <param name="plainText">明文</param>
        /// <param name="Key">密钥</param>
        /// <param name="IV">偏移量</param>
        /// <returns>加密得到的Byte[]</returns>
        public static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
        {
            if (plainText == null || plainText.Length <= 0)
            {
                FDebug.Log("密文无效,无法加密");
                return(null);
            }
            if (Key == null || Key.Length <= 0)
            {
                FDebug.Log("密钥无效,无法加密");
                return(null);
            }
            if (IV == null || IV.Length <= 0)
            {
                FDebug.Log("偏移量无效,无法加密");
                return(null);
            }
            byte[] encrypted;
            using (AesManaged aesAlg = new AesManaged())
            {
                aesAlg.Key = Key;
                aesAlg.IV  = IV;

                // Create an encryptor to perform the stream transform.
                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for encryption.
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {
                            //Write all data to the stream.
                            swEncrypt.Write(plainText);
                        }
                        encrypted = msEncrypt.ToArray();
                    }
                }
                Console.WriteLine(aesAlg.Mode);
            }
            return(encrypted);
        }
Exemple #9
0
        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="TargetDirectory"></param>
        public static void CreateDirectory(string TargetDirectory)
        {
            DirectoryInfo di = new DirectoryInfo(TargetDirectory);

            try
            {
                if (di.Exists)
                {
                    FDebug.Log("That path exists already.");
                    return;
                }
                di.Create();
                FDebug.Log("The directory {0} was created successfully.", TargetDirectory);
            }
            catch (Exception e)
            {
                FDebug.Log("The process failed: {0}", e.ToString());
            }
            finally { }
        }
Exemple #10
0
 /// <summary>
 /// 读取二进制文件
 /// </summary>
 /// <param name="path"></param>
 /// <returns></returns>
 public static byte[] ReadBytefile(string path)
 {
     byte[] data;
     if (File.Exists(path))
     {
         try
         {
             data = File.ReadAllBytes(path);
             return(data);
         }
         catch (Exception e)
         {
             FDebug.Log(e.Message);
             return(null);
         }
     }
     else
     {
         FDebug.Log("file {0} not found", path);
     }
     return(null);
 }
        /// <summary>
        /// 将字符串加密得到的Byte[]解密并转化为字符串
        /// 默认UTF8编码
        /// </summary>
        /// <param name="EncryptedBytes">密文</param>
        /// <param name="Key">密钥</param>
        /// <param name="IV">偏移量</param>
        /// <returns>解密得到的字符串</returns>
        public static string DecryptStringFromBytes_Aes(byte[] EncryptedBytes, byte[] Key, byte[] IV)
        {
            if (EncryptedBytes == null || EncryptedBytes.Length <= 0)
            {
                FDebug.Log("密文无效,无法解密");
                return(null);
            }
            if (Key == null || Key.Length <= 0)
            {
                FDebug.Log("密钥无效,无法解密");
                return(null);
            }
            if (IV == null || IV.Length <= 0)
            {
                FDebug.Log("偏移量无效,无法解密");
                return(null);
            }
            string plaintext = null;

            using (AesManaged aesAlg = new AesManaged())
            {
                aesAlg.Key = Key;
                aesAlg.IV  = IV;
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                using (MemoryStream msDecrypt = new MemoryStream(EncryptedBytes))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }
            return(plaintext);
        }