You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
starcrypto/crypto.go

172 lines
3.2 KiB
Go

4 years ago
package starcrypto
import (
"crypto/rand"
"encoding/binary"
"encoding/hex"
"io"
"os"
)
4 years ago
func String(bstr []byte) string {
return hex.EncodeToString(bstr)
}
func VicqueEncodeV1(srcdata []byte, key string) []byte {
var keys []int
8 months ago
var randCode1, randCode2 uint8
4 years ago
data := make([]byte, len(srcdata))
copy(data, srcdata)
8 months ago
binary.Read(rand.Reader, binary.LittleEndian, &randCode1)
binary.Read(rand.Reader, binary.LittleEndian, &randCode2)
keys = append(keys, len(key)+int(randCode1))
4 years ago
lens := len(data)
for _, v := range key {
8 months ago
keys = append(keys, int(byte(v))+int(randCode1)-int(randCode2))
4 years ago
}
lenkey := len(keys)
for k, v := range data {
if k == lens/2 {
break
}
nv := int(v)
t := 0
if k%2 == 0 {
nv += keys[k%lenkey]
if nv > 255 {
nv -= 256
}
t = int(data[lens-1-k])
t += keys[k%lenkey]
if t > 255 {
t -= 256
}
} else {
nv -= keys[k%lenkey]
if nv < 0 {
nv += 256
}
t = int(data[lens-1-k])
t -= keys[k%lenkey]
if t > 255 {
t += 256
}
}
data[k] = byte(t)
data[lens-1-k] = byte(nv)
}
8 months ago
data = append(data, byte(randCode1), byte(randCode2))
4 years ago
return data
}
func VicqueDecodeV1(srcdata []byte, key string) []byte {
var keys []int
8 months ago
var randCode1, randCode2 int
4 years ago
data := make([]byte, len(srcdata))
copy(data, srcdata)
lens := len(data)
8 months ago
randCode1 = int(data[lens-2])
randCode2 = int(data[lens-1])
keys = append(keys, len(key)+int(randCode1))
4 years ago
for _, v := range key {
8 months ago
keys = append(keys, int(byte(v))+int(randCode1)-int(randCode2))
4 years ago
}
lenkey := len(keys)
lens -= 2
for k, v := range data {
if k == lens/2 {
break
}
nv := int(v)
t := 0
if k%2 == 0 {
nv -= keys[k%lenkey]
if nv < 0 {
nv += 256
}
t = int(data[lens-1-k])
t -= keys[k%lenkey]
if t > 255 {
t += 256
}
} else {
nv += keys[k%lenkey]
if nv > 255 {
nv -= 256
}
t = int(data[lens-1-k])
t += keys[k%lenkey]
if t > 255 {
t -= 256
}
}
data[k] = byte(t)
data[lens-1-k] = byte(nv)
}
return data[:lens]
}
func VicqueEncodeV1File(src, dst, pwd string, shell func(float64)) error {
fpsrc, err := os.Open(src)
if err != nil {
return err
}
defer fpsrc.Close()
stat, _ := os.Stat(src)
filebig := float64(stat.Size())
var sum int64
4 years ago
defer fpsrc.Close()
fpdst, err := os.Create(dst)
if err != nil {
return err
}
defer fpdst.Close()
for {
buf := make([]byte, 1048576)
n, err := fpsrc.Read(buf)
if err != nil {
if err == io.EOF {
break
}
return err
}
sum += int64(n)
4 years ago
go shell(float64(sum) / filebig * 100)
data := VicqueEncodeV1(buf[0:n], pwd)
fpdst.Write(data)
}
return nil
}
func VicqueDecodeV1File(src, dst, pwd string, shell func(float64)) error {
fpsrc, err := os.Open(src)
if err != nil {
return err
}
defer fpsrc.Close()
stat, _ := os.Stat(src)
filebig := float64(stat.Size())
var sum int64
4 years ago
defer fpsrc.Close()
fpdst, err := os.Create(dst)
if err != nil {
return err
}
defer fpdst.Close()
for {
buf := make([]byte, 1048578)
n, err := fpsrc.Read(buf)
if err != nil {
if err == io.EOF {
break
}
return err
}
sum += int64(n)
4 years ago
go shell(float64(sum) / filebig * 100)
data := VicqueDecodeV1(buf[0:n], pwd)
fpdst.Write(data)
}
return nil
}