将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

@@ -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
}