Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
orzogc committed Dec 26, 2020
1 parent f05c355 commit 12fc388
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# fake115uploader
模拟115网盘客户端的上传功能,部分实现参考 [Fake115Upload](https://github.com/T3rry7f/Fake115Upload)
模拟115网盘客户端的上传功能,部分实现参考 [Fake115Upload](https://github.com/T3rry7f/Fake115Upload),仅用于研究目的

### 特点
* 支持秒传模式,需要已经有人上传过指定文件到115
Expand Down
104 changes: 56 additions & 48 deletions cipher/cipher.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Cipher struct {
publicKey *rsa.PublicKey
privateKey *rsa.PrivateKey
randKey []byte
keyS []byte
}

// NewCipher 新建Cipher
Expand All @@ -68,60 +69,28 @@ func NewCipher() (*Cipher, error) {
}
c.privateKey = privateKey

randKey := make([]byte, keySize)
_, err = rand.Read(randKey)
if err != nil {
return nil, err
}
c.randKey = randKey

return c, nil
}

// 生成key
func genKey(randKey []byte, keyLen int) []byte {
xorKey := make([]byte, 0, keyLen)
length := keyLen * (keyLen - 1)
index := 0
if len(randKey) != 0 {
for i := 0; i < keyLen; i++ {
x := byte(uint8(randKey[i]) + uint8(gKts[index]))
xorKey = append(xorKey, gKts[length]^x)
length -= keyLen
index += keyLen
}
}

return xorKey
}

// 利用key进行异或操作
func xor(src, key []byte) []byte {
secret := make([]byte, 0, len(src))
pad := len(src) % 4
if pad > 0 {
for i := 0; i < pad; i++ {
secret = append(secret, src[i]^key[i])
}
src = src[pad:]
}
keyLen := len(key)
num := 0
for _, s := range src {
if num >= keyLen {
num = num % keyLen
}
secret = append(secret, s^key[num])
num++
func (c *Cipher) genKey() error {
c.randKey = make([]byte, keySize)
_, err := rand.Read(c.randKey)
if err != nil {
return err
}
c.keyS = genKey(c.randKey, 4)

return secret
return nil
}

// Encrypt 加密
// Encrypt 加密,一次加密对应一次解密
func (c *Cipher) Encrypt(plainText []byte) ([]byte, error) {
keyS := genKey(c.randKey, 4)
tmp := xor(plainText, keyS)
err := c.genKey()
if err != nil {
return nil, err
}
tmp := xor(plainText, c.keyS)
for i, j := 0, len(tmp)-1; i < j; i, j = i+1, j-1 {
tmp[i], tmp[j] = tmp[j], tmp[i]
}
Expand All @@ -138,7 +107,7 @@ func (c *Cipher) Encrypt(plainText []byte) ([]byte, error) {
return text, nil
}

// Decrypt 解密
// Decrypt 解密,一次加密对应一次解密
func (c *Cipher) Decrypt(cipherText []byte) ([]byte, error) {
text := make([]byte, base64.StdEncoding.DecodedLen(len(cipherText)))
n, err := base64.StdEncoding.Decode(text, cipherText)
Expand All @@ -157,13 +126,52 @@ func (c *Cipher) Decrypt(cipherText []byte) ([]byte, error) {
}
randKey := plainText[:keySize]
plainText = plainText[keySize:]
keyS := genKey(c.randKey, 4)
keyL := genKey(randKey, 12)
tmp := xor(plainText, keyL)
for i, j := 0, len(tmp)-1; i < j; i, j = i+1, j-1 {
tmp[i], tmp[j] = tmp[j], tmp[i]
}
plainText = xor(tmp, keyS)
plainText = xor(tmp, c.keyS)

return plainText, nil
}

// 生成key
func genKey(randKey []byte, keyLen int) []byte {
xorKey := make([]byte, 0, keyLen)
length := keyLen * (keyLen - 1)
index := 0
if len(randKey) != 0 {
for i := 0; i < keyLen; i++ {
x := byte(uint8(randKey[i]) + uint8(gKts[index]))
xorKey = append(xorKey, gKts[length]^x)
length -= keyLen
index += keyLen
}
}

return xorKey
}

// 利用key进行异或操作
func xor(src, key []byte) []byte {
secret := make([]byte, 0, len(src))
pad := len(src) % 4
if pad > 0 {
for i := 0; i < pad; i++ {
secret = append(secret, src[i]^key[i])
}
src = src[pad:]
}
keyLen := len(key)
num := 0
for _, s := range src {
if num >= keyLen {
num = num % keyLen
}
secret = append(secret, s^key[num])
num++
}

return secret
}

0 comments on commit 12fc388

Please sign in to comment.