kgtemp文件轉mp3工具


kgtemp文件是酷我音樂軟件的緩存文件,本文從技術層面探討如何解密該文件為mp3文件,並通過讀取ID3信息來重命名。

kgtemp解密

kgtemp文件前1024個字節是固定的包頭信息,解密方案詳細可以參見(http://www.cnblogs.com/KMBlog/p/6877752.html):

class Program
	{
		static void Main(string[] args)
		{

			byte[] key={0xAC,0xEC,0xDF,0x57};
			using (var input = new FileStream(@"E:\KuGou\Temp\236909b6016c6e98365e5225f488dd7a.kgtemp", FileMode.Open, FileAccess.Read))
			{
				var output = File.OpenWrite(@"d:\test.mp3");//輸出文件
				input.Seek(1024, SeekOrigin.Begin);//跳過1024字節的包頭
				byte[] buffer = new byte[key.Length];
				int length;
				while((length=input.Read(buffer,0,buffer.Length))>0)
				{
					for(int i=0;i<length;i++)
					{
						var k = key[i];
						var kh = k >> 4;
						var kl = k & 0xf;
						var b = buffer[i];
						var low = b & 0xf ^ kl;//解密后的低4位
						var high = (b >> 4) ^ kh ^ low & 0xf;//解密后的高4位
						buffer[i] = (byte)(high << 4 | low);
					}
					output.Write(buffer, 0, length);
				}
				output.Close();
			}
			Console.WriteLine("按任意鍵退出...");
			Console.ReadKey();
		}
	}

這樣解密出來就是mp3文件了

讀取ID3信息

解密出來的文件還需要手動命名,不是很方便,可以讀取ID3V1信息重命名文件。
ID3V1比較簡單,它是存放在MP3文件的末尾,用16進制的編輯器打開一個MP3文件,查看其末尾的128個順序存放字節,數據結構定義如下:
char Header3; /標簽頭必須是"TAG"否則認為沒有標簽/
char Title[30]; /標題/
char Artist[30]; /作者/
char Album[30]; /專集/
char Year4; /出品年代/
char Comment[30]; /備注/
char Genre; /類型,流派/

解析代碼比較簡單,注意中文歌曲用GBK編碼就可以了:

  private static Mp3Info FormatMp3Info(byte[] Info, System.Text.Encoding Encoding)
		{
			Mp3Info myMp3Info = new Mp3Info();
			string str = null;
			int i;
			int position = 0主要代碼jia,; //循環的起始值
			int currentIndex = 0; //Info的當前索引值

			//獲取TAG標識
			for (i = currentIndex; i < currentIndex + 3; i++)
			{
				str = str + (char)Info[i];
				position++;
			}
			currentIndex = position;
			myMp3Info.identify = str;

			//獲取歌名
			str = null;
			byte[] bytTitle = new byte[30]; //將歌名部分讀到一個單獨的數組中
			int j = 0;
			for (i = currentIndex; i < currentIndex + 30; i++)
			{
				bytTitle[j] = Info[i];
				position++;
				j++;
			}
			currentIndex = position;
			myMp3Info.Title = ByteToString(bytTitle, Encoding);

			//獲取歌手名
			str = null;
			j = 0;
			byte[] bytArtist = new byte[30]; //將歌手名部分讀到一個單獨的數組中
			for (i = currentIndex; i < currentIndex + 30; i++)
			{
				bytArtist[j] = Info[i];
				position++;
				j++;
			}
			currentIndex = position;
			myMp3Info.Artist = ByteToString(bytArtist, Encoding);

			//獲取唱片名
			str = null;
			j = 0;
			byte[] bytAlbum = new byte[30]; //將唱片名部分讀到一個單獨的數組中
			for (i = currentIndex; i < currentIndex + 30; i++)
			{
				bytAlbum[j] = Info[i];
				position++;
				j++;
			}
			currentIndex = position;
			myMp3Info.Album = ByteToString(bytAlbum, Encoding);

			//獲取年
			str = null;
			j = 0;
			byte[] bytYear = new byte[4]; //將年部分讀到一個單獨的數組中
			for (i = currentIndex; i < currentIndex + 4; i++)
			{
				bytYear[j] = Info[i];
				position++;
				j++;
			}
			currentIndex = position;
			myMp3Info.Year = ByteToString(bytYear, Encoding);

			//獲取注釋
			str = null;
			j = 0;
			byte[] bytComment = new byte[28]; //將注釋部分讀到一個單獨的數組中
			for (i = currentIndex; i < currentIndex + 25; i++)
			{
				bytComment[j] = Info[i];
				position++;
				j++;
			}
			currentIndex = position;
			myMp3Info.Comment = ByteToString(bytComment, Encoding);

			//以下獲取保留位
			myMp3Info.reserved1 = (char)Info[++position];
			myMp3Info.reserved2 = (char)Info[++position];
			myMp3Info.reserved3 = (char)Info[++position];

			//
			return myMp3Info;
		}

轉換小工具

寫了一個小工具,來進行轉換

裝換工具

下載地址:https://pan.baidu.com/s/1o7FIsPk

PS:上面只讀取了IDV1,部分歌曲可能不存在
可以下載@繽紛 提供的程序,增加了ID3V2的支持:
https://files.cnblogs.com/files/gxlxzys/kgtemp文件轉mp3工具.zip


作者:Jadepeng
出處:jqpeng的技術記事本--http://www.cnblogs.com/xiaoqi
您的支持是對博主最大的鼓勵,感謝您的認真閱讀。
本文版權歸作者所有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM