将XOR加密用C语言重写
This commit is contained in:
parent
da0e77d74c
commit
67a00e7902
|
|
@ -58,17 +58,17 @@ func randMillionSecond() time.Duration {
|
||||||
|
|
||||||
func attack() {
|
func attack() {
|
||||||
c := make(chan int)
|
c := make(chan int)
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 20; i++ {
|
||||||
time.Sleep(time.Millisecond * randMillionSecond())
|
time.Sleep(time.Millisecond * randMillionSecond())
|
||||||
go doHttp(c)
|
go doHttp(c)
|
||||||
}
|
}
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 20; i++ {
|
||||||
<-c
|
<-c
|
||||||
}
|
}
|
||||||
log.Println("Finish")
|
log.Println("Finish")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main1() {
|
func main() {
|
||||||
cfg := profile.Config{
|
cfg := profile.Config{
|
||||||
MemProfile: true,
|
MemProfile: true,
|
||||||
ProfilePath: "./profile", // store profiles in current directory
|
ProfilePath: "./profile", // store profiles in current directory
|
||||||
|
|
|
||||||
4
main.go
4
main.go
|
|
@ -25,11 +25,11 @@ func tunnel() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main1() {
|
||||||
// defer profile.Start(profile.CPUProfile).Stop()
|
// defer profile.Start(profile.CPUProfile).Stop()
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
fmt.Println("Hello World!")
|
fmt.Println("Hello World!")
|
||||||
log.LogTo(cli.LogTo, "ERROR")
|
log.LogTo(cli.LogTo, "INFO")
|
||||||
if cli.ProfilePort != 0 {
|
if cli.ProfilePort != 0 {
|
||||||
go func() {
|
go func() {
|
||||||
http.ListenAndServe("0.0.0.0:"+strconv.Itoa(cli.ProfilePort), nil)
|
http.ListenAndServe("0.0.0.0:"+strconv.Itoa(cli.ProfilePort), nil)
|
||||||
|
|
|
||||||
|
|
@ -75,17 +75,22 @@ func (this *Tunnel) Run() { //单向的,从src发送到dest
|
||||||
}()
|
}()
|
||||||
cache := make([]byte, 1024*4) //4kB
|
cache := make([]byte, 1024*4) //4kB
|
||||||
//构建Carrier
|
//构建Carrier
|
||||||
revCarrier := tscipher.NewCarrier(src, tscipher.NewCipher("XOR"), cache, this.id)
|
revCarrier := tscipher.NewCarrier(src, tscipher.NewCipher("XOR"), cache, id)
|
||||||
sendCarrier := tscipher.NewCarrier(dest, tscipher.NewCipher("XOR"), cache, this.id)
|
sendCarrier := tscipher.NewCarrier(dest, tscipher.NewCipher("XOR"), cache, id)
|
||||||
|
//timer
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
srTimer := time.Now() //send receive timer
|
||||||
var nByte int
|
var nByte int
|
||||||
var err error
|
var err error
|
||||||
|
rTimer := time.Now() //receive timer
|
||||||
if cipherDirection != RECEIVE {
|
if cipherDirection != RECEIVE {
|
||||||
revCarrier.Cipher = nil
|
revCarrier.Cipher = nil
|
||||||
nByte, err = tscipher.RowReceiveData(revCarrier)
|
nByte, err = tscipher.RowReceiveData(revCarrier)
|
||||||
} else {
|
} else {
|
||||||
nByte, err = tscipher.ReceiveData(revCarrier)
|
nByte, err = tscipher.ReceiveData(revCarrier)
|
||||||
}
|
}
|
||||||
|
log.Info("id %s time to receive %d", id, time.Since(rTimer)/1000)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic("Read panic. Tunnel id: %s. Remote Add: %s Local: %s. Err:%s", id, src.RemoteAddr().String(), src.LocalAddr().String(), err.Error())
|
log.Panic("Read panic. Tunnel id: %s. Remote Add: %s Local: %s. Err:%s", id, src.RemoteAddr().String(), src.LocalAddr().String(), err.Error())
|
||||||
}
|
}
|
||||||
|
|
@ -93,11 +98,14 @@ func (this *Tunnel) Run() { //单向的,从src发送到dest
|
||||||
if cipherDirection != SEND {
|
if cipherDirection != SEND {
|
||||||
sendCarrier.Cipher = nil
|
sendCarrier.Cipher = nil
|
||||||
}
|
}
|
||||||
|
sTimer := time.Now() //send timer
|
||||||
n, err := tscipher.SendData(sendCarrier, nByte)
|
n, err := tscipher.SendData(sendCarrier, nByte)
|
||||||
|
log.Info("id %s time to sned %d", id, time.Since(sTimer)/1000)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic("Write panic. ID: %s, Err: %s, Remote Add: %s", id, err, dest.RemoteAddr().String())
|
log.Panic("Write panic. ID: %s, Err: %s, Remote Add: %s", id, err, dest.RemoteAddr().String())
|
||||||
}
|
}
|
||||||
log.Debug("Write %d bytes from %s to %s. Tunnel: %s . 18 bytes %x", n, dest.LocalAddr(), dest.RemoteAddr().String(), id, sendCarrier.Cache[:18])
|
log.Debug("Write %d bytes from %s to %s. Tunnel: %s . 18 bytes %x", n, dest.LocalAddr(), dest.RemoteAddr().String(), id, sendCarrier.Cache[:18])
|
||||||
|
log.Info("id %s send %d /receive %d duration %d ms", n, nByte, id, time.Since(srTimer)/1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,24 @@
|
||||||
package tscipher
|
package tscipher
|
||||||
|
|
||||||
import ()
|
/*
|
||||||
|
#cgo CFLAGS: -O3
|
||||||
|
#include <stdlib.h>
|
||||||
|
char* decrypt(char* inBuff,char* buff,int buffLen,char* key,int keyLen){
|
||||||
|
for(int i=0;i<buffLen;i++){
|
||||||
|
inBuff[i]=buff[buffLen-i-1]^key[i%keyLen];
|
||||||
|
}
|
||||||
|
return inBuff;
|
||||||
|
}
|
||||||
|
char* encrypt(char* inBuff,char* buff,int buffLen,char* key,int keyLen){
|
||||||
|
for(int i=0;i<buffLen;i++){
|
||||||
|
inBuff[buffLen-i-1]=buff[i]^key[i%keyLen];
|
||||||
|
}
|
||||||
|
return inBuff;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
import "unsafe"
|
||||||
|
|
||||||
type XOR struct {
|
type XOR struct {
|
||||||
key []byte
|
key []byte
|
||||||
|
|
@ -11,10 +29,12 @@ func (this *XOR) Decrypt(data []byte) (decrypted []byte, err error) {
|
||||||
// err = nil
|
// err = nil
|
||||||
// return
|
// return
|
||||||
decrypted = make([]byte, len(data))
|
decrypted = make([]byte, len(data))
|
||||||
for i := 0; i < len(data); i++ {
|
// for i := 0; i < len(data); i++ {
|
||||||
decrypted[i] = data[len(data)-i-1] ^ this.key[i%len(this.key)]
|
// decrypted[i] = data[len(data)-i-1] ^ this.key[i%len(this.key)]
|
||||||
// decrypted[i] = data[i]
|
// // decrypted[i] = data[i]
|
||||||
}
|
// }
|
||||||
|
key := this.key
|
||||||
|
C.decrypt((*C.char)(unsafe.Pointer(&decrypted[0])), (*C.char)(unsafe.Pointer(&data[0])), C.int(len(data)), (*C.char)(unsafe.Pointer(&key[0])), C.int(len(key)))
|
||||||
err = nil
|
err = nil
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -24,10 +44,12 @@ func (this *XOR) Encrypt(data []byte) (encryped []byte, err error) {
|
||||||
// err = nil
|
// err = nil
|
||||||
// return
|
// return
|
||||||
encryped = make([]byte, len(data))
|
encryped = make([]byte, len(data))
|
||||||
for i := 0; i < len(data); i++ {
|
// for i := 0; i < len(data); i++ {
|
||||||
encryped[len(data)-i-1] = data[i] ^ this.key[i%len(this.key)]
|
// encryped[len(data)-i-1] = data[i] ^ this.key[i%len(this.key)]
|
||||||
// encryped[i] = data[i]
|
// // encryped[i] = data[i]
|
||||||
}
|
// }
|
||||||
|
key := this.key
|
||||||
|
C.encrypt((*C.char)(unsafe.Pointer(&encryped[0])), (*C.char)(unsafe.Pointer(&data[0])), C.int(len(data)), (*C.char)(unsafe.Pointer(&key[0])), C.int(len(key)))
|
||||||
err = nil
|
err = nil
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue