4 Commits

Author SHA1 Message Date
2058504384 feat(QMCv2): support .mggl and .bkc*
`.mggl`: QQ Music MacOS Ogg (fix https://github.com/unlock-music/cli/issues/25)
`.bkc*` (".bkc" + {"mp3", "m4a", "flac", "wav", "ape", "ogg", "wma"}): Moo Music
2022-01-09 06:30:53 +08:00
76df8e6c1a fix(QMCv2): unlock error on 32bit platform 2022-01-09 06:24:58 +08:00
42d658625f test(QMCv2): add case mflac_rc4 2022-01-09 06:21:35 +08:00
def2925900 feat(QMCv2): better rc4 cipher 2021-12-17 04:31:58 +08:00
10 changed files with 85 additions and 65 deletions

View File

@ -6,10 +6,10 @@ import (
// A rc4Cipher is an instance of RC4 using a particular key.
type rc4Cipher struct {
box []byte
key []byte
hash uint32
boxTmp []byte
box []byte
key []byte
hash uint32
n int
}
// NewRC4Cipher creates and returns a new rc4Cipher. The key argument should be the
@ -20,9 +20,8 @@ func NewRC4Cipher(key []byte) (*rc4Cipher, error) {
return nil, errors.New("qmc/cipher_rc4: invalid key size")
}
var c = rc4Cipher{key: key}
var c = rc4Cipher{key: key, n: n}
c.box = make([]byte, n)
c.boxTmp = make([]byte, n)
for i := 0; i < n; i++ {
c.box[i] = byte(i)
@ -39,7 +38,7 @@ func NewRC4Cipher(key []byte) (*rc4Cipher, error) {
func (c *rc4Cipher) getHashBase() {
c.hash = 1
for i := 0; i < len(c.key); i++ {
for i := 0; i < c.n; i++ {
v := uint32(c.key[i])
if v == 0 {
continue
@ -52,7 +51,10 @@ func (c *rc4Cipher) getHashBase() {
}
}
const rc4SegmentSize = 5120
const (
rc4SegmentSize = 5120
rc4FirstSegmentSize = 128
)
func (c *rc4Cipher) Decrypt(src []byte, offset int) {
toProcess := len(src)
@ -64,10 +66,10 @@ func (c *rc4Cipher) Decrypt(src []byte, offset int) {
return toProcess == 0
}
if offset < 128 {
if offset < rc4FirstSegmentSize {
blockSize := toProcess
if blockSize > 128-offset {
blockSize = 128 - offset
if blockSize > rc4FirstSegmentSize-offset {
blockSize = rc4FirstSegmentSize - offset
}
c.encFirstSegment(src[:blockSize], offset)
if markProcess(blockSize) {
@ -80,8 +82,7 @@ func (c *rc4Cipher) Decrypt(src []byte, offset int) {
if blockSize > rc4SegmentSize-offset%rc4SegmentSize {
blockSize = rc4SegmentSize - offset%rc4SegmentSize
}
k := src[processed : processed+blockSize]
c.encASegment(k, offset)
c.encASegment(src[processed:processed+blockSize], offset)
if markProcess(blockSize) {
return
}
@ -96,38 +97,28 @@ func (c *rc4Cipher) Decrypt(src []byte, offset int) {
}
}
func (c *rc4Cipher) encFirstSegment(buf []byte, offset int) {
n := len(c.box)
for i := 0; i < len(buf); i++ {
idx1 := offset + i
segmentID := int(c.key[idx1%n])
idx2 := int(float64(c.hash) / float64((idx1+1)*segmentID) * 100.0)
buf[i] ^= c.key[idx2%n]
buf[i] ^= c.key[c.getSegmentSkip(offset+i)]
}
}
func (c *rc4Cipher) encASegment(buf []byte, offset int) {
n := len(c.box)
copy(c.boxTmp, c.box)
segmentID := (offset / rc4SegmentSize) & 0x1FF
if n <= segmentID {
return
}
idx2 := int64(float64(c.hash) /
float64((offset/rc4SegmentSize+1)*int(c.key[segmentID])) *
100.0)
skipLen := int((idx2 & 0x1FF) + int64(offset%rc4SegmentSize))
box := make([]byte, c.n)
copy(box, c.box)
j, k := 0, 0
skipLen := (offset % rc4SegmentSize) + c.getSegmentSkip(offset/rc4SegmentSize)
for i := -skipLen; i < len(buf); i++ {
j = (j + 1) % n
k = (int(c.boxTmp[j]) + k) % n
c.boxTmp[j], c.boxTmp[k] = c.boxTmp[k], c.boxTmp[j]
j = (j + 1) % c.n
k = (int(box[j]) + k) % c.n
box[j], box[k] = box[k], box[j]
if i >= 0 {
buf[i] ^= c.boxTmp[int(c.boxTmp[j])+int(c.boxTmp[k])%n]
buf[i] ^= box[(int(box[j])+int(box[k]))%c.n]
}
}
}
func (c *rc4Cipher) getSegmentSkip(id int) int {
seed := int(c.key[id%c.n])
idx := int64(float64(c.hash) / float64((id+1)*seed) * 100.0)
return int(idx % int64(c.n))
}

View File

@ -6,16 +6,17 @@ import (
"testing"
)
func loadTestRC4CipherData() ([]byte, []byte, []byte, error) {
key, err := os.ReadFile("./testdata/mflac0_rc4_key.bin")
func loadTestRC4CipherData(name string) ([]byte, []byte, []byte, error) {
prefix := "./testdata/" + name
key, err := os.ReadFile(prefix + "_key.bin")
if err != nil {
return nil, nil, nil, err
}
raw, err := os.ReadFile("./testdata/mflac0_rc4_raw.bin")
raw, err := os.ReadFile(prefix + "_raw.bin")
if err != nil {
return nil, nil, nil, err
}
target, err := os.ReadFile("./testdata/mflac0_rc4_target.bin")
target, err := os.ReadFile(prefix + "_target.bin")
if err != nil {
return nil, nil, nil, err
}
@ -23,26 +24,50 @@ func loadTestRC4CipherData() ([]byte, []byte, []byte, error) {
return key, raw, target, nil
}
func Test_rc4Cipher_Decrypt(t *testing.T) {
key, raw, target, err := loadTestRC4CipherData()
if err != nil {
t.Fatalf("load testing data failed: %s", err)
tests := []struct {
name string
wantErr bool
}{
{"mflac0_rc4", false},
{"mflac_rc4", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
key, raw, target, err := loadTestRC4CipherData(tt.name)
if err != nil {
t.Fatalf("load testing data failed: %s", err)
}
c, err := NewRC4Cipher(key)
if err != nil {
t.Errorf("init rc4Cipher failed: %s", err)
return
}
c.Decrypt(raw, 0)
if !reflect.DeepEqual(raw, target) {
t.Error("overall")
}
})
}
t.Run("overall", func(t *testing.T) {
c, err := NewRC4Cipher(key)
if err != nil {
t.Errorf("init rc4Cipher failed: %s", err)
return
}
c.Decrypt(raw, 0)
if !reflect.DeepEqual(raw, target) {
t.Error("overall")
}
})
}
func BenchmarkRc4Cipher_Decrypt(b *testing.B) {
key, raw, _, err := loadTestRC4CipherData("mflac0_rc4")
if err != nil {
b.Fatalf("load testing data failed: %s", err)
}
c, err := NewRC4Cipher(key)
if err != nil {
b.Errorf("init rc4Cipher failed: %s", err)
return
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Decrypt(raw, 0)
}
}
func Test_rc4Cipher_encFirstSegment(t *testing.T) {
key, raw, target, err := loadTestRC4CipherData()
key, raw, target, err := loadTestRC4CipherData("mflac0_rc4")
if err != nil {
t.Fatalf("load testing data failed: %s", err)
}
@ -60,7 +85,7 @@ func Test_rc4Cipher_encFirstSegment(t *testing.T) {
}
func Test_rc4Cipher_encASegment(t *testing.T) {
key, raw, target, err := loadTestRC4CipherData()
key, raw, target, err := loadTestRC4CipherData("mflac0_rc4")
if err != nil {
t.Fatalf("load testing data failed: %s", err)
}

View File

@ -27,7 +27,6 @@ func loadDecryptKeyData(name string) ([]byte, []byte, error) {
return keyRaw, keyDec, nil
}
func TestDecryptKey(t *testing.T) {
tests := []struct {
name string
filename string
@ -35,13 +34,15 @@ func TestDecryptKey(t *testing.T) {
}{
{"mflac0_rc4(512)", "mflac0_rc4", false},
{"mflac_map(256)", "mflac_map", false},
{"mflac_rc4(256)", "mflac_rc4", false},
{"mgg_map(256)", "mgg_map", false},
}
for _, tt := range tests {
raw, want, err := loadDecryptKeyData(tt.filename)
if err != nil {
t.Fatalf("load test data failed: %s", err)
}
t.Run(tt.name, func(t *testing.T) {
raw, want, err := loadDecryptKeyData(tt.filename)
if err != nil {
t.Fatalf("load test data failed: %s", err)
}
got, err := DecryptKey(raw)
if (err != nil) != tt.wantErr {
t.Errorf("DecryptKey() error = %v, wantErr %v", err, tt.wantErr)

View File

@ -193,9 +193,9 @@ func init() {
"qmcflac", //QQ Music FLAC
"qmcogg", //QQ Music OGG
"tkm", //QQ Music Accompaniment M4A
"bkcmp3", //Moo Music Mp3
"bkcflac", //Moo Music Flac
"tkm", //QQ Music Accompaniment M4A
"bkcmp3", "bkcm4a", "bkcflac", "bkcwav", "bkcape", "bkcogg", "bkcwma", //Moo Music
"666c6163", //QQ Music Weiyun Flac
"6d7033", //QQ Music Weiyun Mp3
@ -203,7 +203,7 @@ func init() {
"6d3461", //QQ Music Weiyun M4a
"776176", //QQ Music Weiyun Wav
"mgg", "mgg1", //QQ Music New Ogg
"mgg", "mgg1", "mggl", //QQ Music New Ogg
"mflac", "mflac0", //QQ Music New Flac
}
for _, ext := range supportedExts {

View File

@ -32,6 +32,7 @@ func TestMflac0Decoder_Read(t *testing.T) {
wantErr bool
}{
{"mflac0_rc4", false},
{"mflac_rc4", false},
{"mflac_map", false},
{"mgg_map", false},
{"qmc0_static", false},

1
algo/qmc/testdata/mflac_rc4_key.bin vendored Normal file
View File

@ -0,0 +1 @@
pUtyvqr0TgAvR95mNmY7DmNl386TsJNAEIz95CEcgIgJCcs28686O7llxD5E74ldn70xMtd5cG58TA5ILw09I8BOTf5EdHKd6wwPn689DUK13y3Req6H0P33my2miJ5bQ2AA22B8vp4V0NJ3hBqNtFf7cId48V6W51e1kwgu1xKKawxe9BByT92MFlqrFaKH32dB2zFgyd38l2P1outr4l2XLq48F9G17ptRz4W8Loxu28RvZgv0BzL26Ht9I2L5VCwMzzt7OeZ55iQs40Tr6k81QGraIUJj5zeBMgJRMTaSgi19hU5x5a08Qd662MbFhZZ0FjVvaDy1nbIDhrC62c1lX6wf70O45h4W42VxloBVeZ9Sef4V7cWrjrEjj3DJ5w2iu6Q9uoal2f4390kue42Um5HcDFWqv3m56k6O89bRV424PaRra1k9Cd2L56IN2zfBYqNo2WP5VC68G8w1hfflOY0O52h4WdcpoHSjZm4b35N7l47dT4dwEXj1U4J5

View File

@ -0,0 +1 @@
cFV0eXZxcjAF/IXJ9qJT1u5C3S5AgY9BoVtIQNBKfxQMt5hH7BF36ndIJGV5L6qw5h4G0IOIOOewdHmMCNfKJftHM4nv3B0iRlSdqJKdL08wO3sV0v8eZk0OiYAlxgseGcBquQWYS/0b5Lj/Ioi2NfpOthAY9vUiRPnfH3+7/2AJGudHjj4Gg1KkpPW3mXIKbsk+Ou9fhrUqs873BCdsmI6qRmVNhOkLaUcbG6Zin3XU0WkgnnjebR43S8N4bw5BTphFvhy42QvspnD7Ewb1tVZQMQ2N1s38nBjukdfCB9R6aRwITOvg2U7Lr0RjLpbrIn6A6iVilpINjK4VptuKUTlpDXQwgCjoqeHQaHNCWgYpdjB69lXn8km/BfzK7QyDbh0VgTikwAHF9tvPhin3AIDRcU0xsaWYKURRfJelX3pSN495ADlhXdEKL/+l60hVnY7t6iCMxJL3lOtdGtdUYUGUCc76PB1fX+0HTWCcfcwvXTEdczr9J1h2yTeJNqFQ5pNy8vX7Ws8k7vDQVFkw4llZjPhb0kg9aDNePTNIKSGwy/7eofrcUQlC9DI+qqqwQ5abA/93fNsPq6XU3uwawnrbBsdz8DDdjJiEDI7abkPIDIfr/uR0YzgBxW90t5bt6xAtuW+VSYAM7kGxI3RZTl7JgOT60MLyIWkYASrRhRPMGks8zL10ED/4yGTEB1nt

BIN
algo/qmc/testdata/mflac_rc4_raw.bin vendored Normal file

Binary file not shown.

BIN
algo/qmc/testdata/mflac_rc4_suffix.bin vendored Normal file

Binary file not shown.

BIN
algo/qmc/testdata/mflac_rc4_target.bin vendored Normal file

Binary file not shown.