Exemplo n.º 1
0
        public static void DecryptFile(string filePath, EncVersion encVersion)
        {
            byte[] fileBytes;

            switch (Save.GetFileType(filePath))
            {
            case ENC_TYPE.JPGorPNG:
                using (var fileStream = File.OpenWrite(Save.GetSavePath(filePath)))
                {
                    fileBytes = Crypt.Decrypt(File.ReadAllBytes(filePath), false, encVersion);
                    fileStream.Write(fileBytes, 0, fileBytes.Length);
                    fileStream.Close();
                }

                break;

            case ENC_TYPE.TXTorSQLorJSON:
                using (var streamWriter = new StreamWriter(Save.GetSavePath(filePath)))
                {
                    fileBytes = Crypt.Decrypt(File.ReadAllBytes(filePath), true, encVersion);
                    var fileText = Encoding.UTF8.GetString(fileBytes).Replace("\r", "");
                    streamWriter.Write(fileText);
                    streamWriter.Close();
                }

                break;

            case ENC_TYPE.BIN:
                using (var streamWriter = new StreamWriter(Save.GetSavePath(filePath)))
                {
                    fileBytes = Crypt.Decrypt(File.ReadAllBytes(filePath), false, encVersion);
                    var fileText = Encoding.UTF8.GetString(fileBytes);
                    streamWriter.Write(fileText);
                    streamWriter.Close();
                }

                break;

            case ENC_TYPE.UNKONWN:
                using (var fileStream = File.OpenWrite(Save.GetSavePath(filePath)))
                {
                    fileBytes = Crypt.Decrypt(File.ReadAllBytes(filePath), false, encVersion);
                    fileStream.Write(fileBytes, 0, fileBytes.Length);
                    fileStream.Close();
                }

                break;

            case ENC_TYPE.ERROR:
                MessageBox.Show(@"无法识别");
                break;

            case ENC_TYPE.ACB:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemplo n.º 2
0
        public static string ConvertFileName(string filePath, EncVersion versionFrom, EncVersion versionTo)
        {
            if (versionFrom == versionTo)
            {
                return(null);
            }
            //var fileBytes = File.ReadAllBytes(filePath);
            //var nowBytes = Decrypt(fileBytes, true, EncVersion.Ver2);
            //using (var fileStream = File.OpenWrite(Save.GetSavePath(filePath)))
            //{
            //    fileStream.Write(nowBytes, 0, nowBytes.Length);
            //    fileStream.Close();
            //}

            if (versionFrom == EncVersion.Ver1)
            {
                var fileName = Path.GetFileNameWithoutExtension(filePath);
                var fileNameWithoutExrension = Path.GetFileNameWithoutExtension(fileName);
                var fileExtension            = Path.GetExtension(fileName);
                var fileNameBytes            = Encoding.UTF8.GetBytes(fileNameWithoutExrension ?? " ");
                var hashBytes = HmacSha256.ComputeHash(fileNameBytes);
                var fileHash  = BitConverter.ToString(hashBytes, 0, hashBytes.Length).Replace("-", "");

                return($"{fileNameWithoutExrension}_{fileHash}{fileExtension}.enc");
            }
            else
            {
                var fileName      = Path.GetFileNameWithoutExtension(filePath);
                var fileExtension = Path.GetExtension(fileName);

                if (fileName == null)
                {
                    return(null);
                }
                var tempFileName   = fileName.Split('_');
                var exportFileName = string.Join("_", tempFileName.Take(tempFileName.Length - 1));

                return(exportFileName + fileExtension + ".enc");
            }
        }
Exemplo n.º 3
0
        public static byte[] Decrypt <T>(T data, bool lz4 = false, EncVersion encVersion = EncVersion.Ver1)
        {
            if (data == null)
            {
                return(null);
            }

            var fileSigSize = SIGNATURE_SIZE; //"t7s-enc",size =7;

            if (encVersion == EncVersion.Ver1)
            {
                fileSigSize = 0;
            }
            var array  = new byte[16];
            var array2 = ConvertByte(data); //文件数据转换为数组

            if (array2.Length <= (16 | fileSigSize))
            {
                return(null);
            }

            var array3 = new byte[array2.Length - fileSigSize - 16]; //文件数组 - 文件头(16) - 文件标签(7)

            Buffer.BlockCopy(array2, fileSigSize, array, 0, 16);
            Buffer.BlockCopy(array2, fileSigSize | 16, array3, 0, array2.Length - fileSigSize - 16);
            byte[] array4;
            if (encVersion == EncVersion.Ver1)
            {
                using (var rhinelandManaged = new RijndaelManaged())
                {
                    rhinelandManaged.BlockSize = 128;
                    rhinelandManaged.KeySize   = 128;
                    rhinelandManaged.IV        = array;
                    rhinelandManaged.Key       = PrivateKey;
                    rhinelandManaged.Mode      = CipherMode.CBC;
                    rhinelandManaged.Padding   = PaddingMode.PKCS7;
                    using (var cryptoTransform = rhinelandManaged.CreateDecryptor())
                    {
                        array4 = cryptoTransform.TransformFinalBlock(array3, 0, array3.Length);
                    }
                }
            }
            else
            {
                SetShuffledKey(PrivateKey2);
                using (var rhinelandManaged = new RijndaelManaged())
                {
                    rhinelandManaged.BlockSize = 128;
                    rhinelandManaged.KeySize   = 128;
                    rhinelandManaged.IV        = array;
                    rhinelandManaged.Key       = _shuffledKey;
                    rhinelandManaged.Mode      = CipherMode.CBC;
                    rhinelandManaged.Padding   = PaddingMode.PKCS7;
                    using (var cryptoTransform = rhinelandManaged.CreateDecryptor())
                    {
                        array4 = cryptoTransform.TransformFinalBlock(array3, 0, array3.Length);
                    }
                }

                ClearShuffledKey();
            }

            return(!lz4 ? array4 : LZ4Codec.Decode(array4, 4, array4.Length - 4, BitConverter.ToInt32(array4, 0)));
        }