示例#1
0
文件: encrypt0.go 项目: optimuse/spig
func main() {
	var help = flag.Bool("h", false, "help")
	var ip = flag.String("i", "127.0.0.1", "ip address")
	var port = flag.String("p", "8002", "port")
	flag.Parse()
	if *help || flag.NArg() != 1 {
		fmt.Printf("USAGE: encrypt0 <string>\n")
		flag.PrintDefaults()
		return
	}

	utils.Version()

	debug := utils.NewDebug(utils.USER, "encrypt0")

	debug.Printf("Alg/Mode/Padding = %s", AMP)

	key, e := aeskey.Key()
	if e != nil {
		fmt.Printf("AES key error: %v\n", e)
		os.Exit(1)
	}
	debug.PrintBuffer(key, "Key = ")

	iv, e := aeskey.Iv()
	if e != nil {
		fmt.Printf("AES IV error: %v\n", e)
		return
	}
	debug.PrintBuffer(iv, "IV = ")

	laddr := "" + *ip + ":" + *port

	addr, e := net.ResolveTCPAddr("tcp", laddr)
	if e != nil {
		fmt.Printf("Cannot resolve address %s\n", laddr)
		os.Exit(1)
	}
	conn, e := net.DialTCP("tcp", nil, addr)
	if e != nil {
		fmt.Printf("Dialed failed on address %s\n", laddr)
		os.Exit(2)
	}

	defer func() {
		conn.Close()
	}()

	fmt.Printf("Connected to remote address %s\n", conn.RemoteAddr())
	fmt.Printf("Connected from local address %s\n", conn.LocalAddr())

	pbuff := utils.MakeByteOEncoding(2048)

	e = pbuff.WriteString(flag.Arg(0))
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		os.Exit(1)
	}

	e = pbuff.WriteBinary([]byte(flag.Arg(0)))
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		os.Exit(1)
	}

	plaintext, e := pbuff.GetBuffer()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		os.Exit(1)
	}

	debug.PrintBuffer(plaintext, "Plaintext encoding for T1,B =")

	ciphertext, e := utils.Encrypt(AMP, iv, key[0:], plaintext)
	if e != nil {
		fmt.Printf("Encryption error: %v\n", e)
	}
	debug.PrintBuffer(ciphertext, "Ciphertext of encoding for T1,B =")

	obuff := utils.MakeTcpOEncoding(conn)
	debug.Printf("Sending ciphertext as binary encoding")
	e = obuff.WriteBinary(ciphertext)
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		os.Exit(1)
	}

	ibuff := utils.MakeTcpIEncoding(conn)
	debug.Printf("Reading ciphertext as binary encoding")
	ciphertext, e = ibuff.ReadBinary()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		os.Exit(1)
	}
	debug.PrintBuffer(ciphertext, "Ciphertext = ")

	plaintext, e = utils.Decrypt(AMP, iv, key[0:], ciphertext)
	if e != nil {
		fmt.Printf("Decryption error: %v\n", e)
	}
	debug.PrintBuffer(plaintext, "Plaintext encoding of B,T2 = ")

	cbuff := utils.MakeByteIEncoding(plaintext)

	debug.Printf("Reading buffer B")
	b, e := cbuff.ReadBinary()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		os.Exit(1)
	}
	debug.PrintBuffer(b, "B = ")

	debug.Printf("Reading string T2")
	s, e := cbuff.ReadString()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		os.Exit(1)
	}
	debug.Printf("T2 = %s", s)

	fmt.Printf("String received = %s\n", s)
	fmt.Printf("Binary data received contained %d bytes\n", len(b))

}
示例#2
0
func worker(name string, conn *net.TCPConn) {

	debug := utils.NewDebug(utils.SYSTEM, name)

	defer func() {
		fmt.Printf("... %s worker finished.\n", name)
		conn.Close()
	}()

	key, e := aeskey.Key()
	if e != nil {
		fmt.Printf("%s AES key error: %v\n", name, e)
		return
	}
	iv, e := aeskey.Iv()
	if e != nil {
		fmt.Printf("%s AES IV error: %v\n", name, e)
		return
	}

	fmt.Printf("%s worker connected to remote address %s\n", name, conn.RemoteAddr())
	ibuff := utils.MakeTcpIEncoding(conn)

	debug.Printf("Reading ciphertext as binary encoding")
	ciphertext, e := ibuff.ReadBinary()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(ciphertext, "Ciphertext = ")

	plaintext, e := utils.Decrypt(AMP, iv, key[0:], ciphertext)
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(plaintext, "Plaintext encoding of T1,B = ")

	cbuff := utils.MakeByteIEncoding(plaintext)

	debug.Printf("Reading string T1")
	s, e := cbuff.ReadString()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.Printf("T1 = %s", s)

	debug.Printf("Reading buffer B")
	b, e := cbuff.ReadBinary()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(b, "B = ")

	fmt.Printf("You sent the string \"%s\"\n", s)
	fmt.Printf("and the binary data of length %d\n", len(b))

	obuff := utils.MakeTcpOEncoding(conn)

	pbuff := utils.MakeByteOEncoding(2048)
	e = pbuff.WriteBinary(b)
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}

	e = pbuff.WriteString("God is alive. He just doesn't want to get involved.")
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}

	plaintext, e = pbuff.GetBuffer()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(plaintext, "plaintext encoding of B,T2 = ")

	ciphertext, e = utils.Encrypt(AMP, iv, key[0:], plaintext)
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}

	debug.PrintBuffer(ciphertext, "Sending binary encoding of ciphertext =")
	e = obuff.WriteBinary(ciphertext)
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}

}
示例#3
0
func worker(name string, conn *net.TCPConn) {
	defer func() {
		fmt.Printf("... %s worker finished.\n", name)
		conn.Close()
	}()

	debug := utils.NewDebug(utils.USER, name)

	key, e := aeskey.Key()
	if e != nil {
		fmt.Printf("%s AES key error: %v\n", name, e)
		return
	}
	iv, e := aeskey.Iv()
	if e != nil {
		fmt.Printf("%s AES IV error: %v\n", name, e)
		return
	}

	fmt.Printf("%s worker connected to remote address %s\n", name, conn.RemoteAddr())
	ibuff := utils.MakeTcpIEncoding(conn)

	debug.Printf("Reading ciphertext as binary encoding")
	ciphertext, e := ibuff.ReadBinary()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(ciphertext, "Ciphertext = ")

	plaintext, e := utils.Decrypt(AMP, iv, key[0:], ciphertext)
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(plaintext, "Plaintext encoding of {T1,B} = ")

	sbuff := utils.MakeByteIEncoding(plaintext)
	debug.Printf("Reading structured data {T1,B}")
	body, e := sbuff.ReadStructured()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(body, "Encoding for T1,B = ")

	cbuff := utils.MakeByteIEncoding(body)

	s, e := cbuff.ReadString()
	debug.Printf("Reading string T1")
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.Printf("T1 = %s", s)

	debug.Printf("Reading buffer B")
	b, e := cbuff.ReadBinary()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(b, "B = ")

	fmt.Printf("You sent the string \"%s\"\n", s)
	fmt.Printf("and the binary data of length %d\n", len(b))

	obuff := utils.MakeTcpOEncoding(conn)

	tbuff := utils.MakeByteOEncoding(2048)
	e = tbuff.WriteBinary(b)
	if e != nil {
		fmt.Printf("%s Error: %v\n", e)
		return
	}

	e = tbuff.WriteString("Along come the scientists and make the words of our fathers into folklore.")
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}

	body, e = tbuff.GetBuffer()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}

	pbuff := utils.MakeByteOEncoding(2048)
	e = pbuff.WriteStructured(body)
	if e != nil {
		fmt.Printf("%s Error: %v\n", e)
		return
	}

	plaintext, e = pbuff.GetBuffer()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(plaintext, "plaintext encoding of {B,T2} = ")

	ciphertext, e = utils.Encrypt(AMP, iv, key[0:], plaintext)
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}

	debug.PrintBuffer(ciphertext, "Sending binary encoding of ciphertext =")
	e = obuff.WriteBinary(ciphertext)
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}

}