将XOR加密用C语言重写

This commit is contained in:
dmy@lab 2015-11-21 16:54:04 +08:00
parent da0e77d74c
commit 67a00e7902
4 changed files with 46 additions and 16 deletions

View File

@ -58,17 +58,17 @@ func randMillionSecond() time.Duration {
func attack() {
c := make(chan int)
for i := 0; i < 10; i++ {
for i := 0; i < 20; i++ {
time.Sleep(time.Millisecond * randMillionSecond())
go doHttp(c)
}
for i := 0; i < 10; i++ {
for i := 0; i < 20; i++ {
<-c
}
log.Println("Finish")
}
func main1() {
func main() {
cfg := profile.Config{
MemProfile: true,
ProfilePath: "./profile", // store profiles in current directory

View File

@ -25,11 +25,11 @@ func tunnel() {
}
func main() {
func main1() {
// defer profile.Start(profile.CPUProfile).Stop()
flag.Parse()
fmt.Println("Hello World!")
log.LogTo(cli.LogTo, "ERROR")
log.LogTo(cli.LogTo, "INFO")
if cli.ProfilePort != 0 {
go func() {
http.ListenAndServe("0.0.0.0:"+strconv.Itoa(cli.ProfilePort), nil)

View File

@ -75,17 +75,22 @@ func (this *Tunnel) Run() { //单向的从src发送到dest
}()
cache := make([]byte, 1024*4) //4kB
//构建Carrier
revCarrier := tscipher.NewCarrier(src, tscipher.NewCipher("XOR"), cache, this.id)
sendCarrier := tscipher.NewCarrier(dest, tscipher.NewCipher("XOR"), cache, this.id)
revCarrier := tscipher.NewCarrier(src, tscipher.NewCipher("XOR"), cache, id)
sendCarrier := tscipher.NewCarrier(dest, tscipher.NewCipher("XOR"), cache, id)
//timer
for {
srTimer := time.Now() //send receive timer
var nByte int
var err error
rTimer := time.Now() //receive timer
if cipherDirection != RECEIVE {
revCarrier.Cipher = nil
nByte, err = tscipher.RowReceiveData(revCarrier)
} else {
nByte, err = tscipher.ReceiveData(revCarrier)
}
log.Info("id %s time to receive %d", id, time.Since(rTimer)/1000)
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())
}
@ -93,11 +98,14 @@ func (this *Tunnel) Run() { //单向的从src发送到dest
if cipherDirection != SEND {
sendCarrier.Cipher = nil
}
sTimer := time.Now() //send timer
n, err := tscipher.SendData(sendCarrier, nByte)
log.Info("id %s time to sned %d", id, time.Since(sTimer)/1000)
if err != nil {
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.Info("id %s send %d /receive %d duration %d ms", n, nByte, id, time.Since(srTimer)/1000)
}
}

View File

@ -1,6 +1,24 @@
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 {
key []byte
@ -11,10 +29,12 @@ func (this *XOR) Decrypt(data []byte) (decrypted []byte, err error) {
// err = nil
// return
decrypted = make([]byte, len(data))
for i := 0; i < len(data); i++ {
decrypted[i] = data[len(data)-i-1] ^ this.key[i%len(this.key)]
// decrypted[i] = 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[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
return
}
@ -24,10 +44,12 @@ func (this *XOR) Encrypt(data []byte) (encryped []byte, err error) {
// err = nil
// return
encryped = make([]byte, len(data))
for i := 0; i < len(data); i++ {
encryped[len(data)-i-1] = data[i] ^ this.key[i%len(this.key)]
// encryped[i] = data[i]
}
// for i := 0; i < len(data); i++ {
// encryped[len(data)-i-1] = data[i] ^ this.key[i%len(this.key)]
// // 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
return
}