【转】酷狗音乐 krc格式文件解包

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;

namespace krcdec
{
	class KRC : IDisposable
	{

		//异或加密 密钥
		public static char[] EncKey = { '@', 'G', 'a', 'w', '^', '2', 't', 'G', 'Q', '6', '1', '-', 'Î', 'Ò', 'n', 'i' };


		FileStream fs = null;

		//头部4字节
		byte[] HeadBytes;

		//异或加密内容
		byte[] EncodedBytes;

		//解异或加密后ZIP数据
		byte[] ZipBytes;

		//UNZIP后数据
		byte[] UnzipBytes;


		public KRC()
		{
			
		}

		public string Decode(string FilePath)
		{

			fs = new FileStream(FilePath, FileMode.Open);

			HeadBytes = new byte[4];
			EncodedBytes = new byte[fs.Length];
			ZipBytes = new byte[fs.Length];

			fs.Read(HeadBytes, 0, 4);

			fs.Read(EncodedBytes, 0, (int)(fs.Length));

			int EncodedBytesLength = EncodedBytes.Length;

			for (int i = 0; i < EncodedBytesLength; i++)
			{
				int l = i % 16;

				ZipBytes[i] = (byte)(EncodedBytes[i] ^ EncKey[l]);
			}
			UnzipBytes = unzipFileStream(ZipBytes);
			string s = Encoding.UTF8.GetString(UnzipBytes);
			return s;
		}


		byte[] unzipFileStream(byte[] data)
		{
			Inflater inf = new Inflater(false);
			Deflater def = new Deflater();




			MemoryStream memoryStream = new MemoryStream();
			byte[] buffer = new byte[1024];
			int buffer_len = 0;
			using (InflaterInputStream input = new InflaterInputStream(new MemoryStream(data), inf))
			{
				do
				{
					buffer_len = input.Read(buffer, 0, buffer.Length);
					memoryStream.Write(buffer, 0, buffer_len);
					input.Flush();
				}
				while (buffer_len > 0);

				input.Close();
			}

			memoryStream.Close();
			buffer = null;
			return memoryStream.ToArray();
		}



		public void Dispose()
		{
			fs.Close();
		}

		public void GetDateTime()
		{
		}


	}
}

转自 https://www.cnblogs.com/binsys/articles/2124157.html