func main() {
	msg := []byte("This is 18+ chars!")
	fmt.Printf("msg ==    %s\n", msg)

	// Encrypt
	encBlock, err := aes.NewCipher(PASSPHRASE)
	fun.MaybeFatalAt("aes.NewCipher", err)

	// See https://github.com/thecloakproject/utils/blob/master/crypt/aes.go
	cipher, err := crypt.AESEncryptBytes(encBlock, msg)
	fun.MaybeFatalAt("AESEncryptBytes", err)

	fmt.Printf("cipher == %v\n", cipher)

	// Decrypt
	decBlock, err := aes.NewCipher(PASSPHRASE)
	fun.MaybeFatalAt("aes.NewCipher", err)

	// See https://github.com/thecloakproject/utils/blob/master/crypt/aes.go
	plain, err := crypt.AESDecryptBytes(decBlock, cipher)
	fun.MaybeFatalAt("AESDecryptBytes", err)

	fmt.Printf("plain ==  %s\n", plain)
	msgPadded := utils.PadBytes(msg, decBlock.BlockSize())

	// Check for equality
	fmt.Printf("\nThey match? %v!\n", bytes.Equal(msgPadded, plain))

	// Check for equality in other ways
	msgUnpadded := strings.TrimSpace(string(msgPadded))
	match := (msgUnpadded == string(plain))
	fmt.Printf("\nDo their trimmed versions match? %v!\n", match)
	if match {
		fmt.Printf("They both equal '%s'\n", msgUnpadded)
	}

	// Here's how to remove those ugly trailing nulls
	fmt.Printf("Cleanest-looking version: '%s'\n",
		strings.TrimRight(string(plain), "\x00"))
}
Exemplo n.º 2
0
// Continuously read local user input (from telnet) and write it to all remote
// connections
func LocalConnHandler(conn net.Conn) {
	// New user connected; add their connection to routing table
	connList.AddLocal <- conn

	// Close conn and remove it from the routing table when we're done
	// here
	defer func() {
		connList.DeleteLocal <- conn
		conn.Close()
	}()

	// Create new cipher.Block
	if DEBUG {
		log.Printf("Using shared secret '%s'\n", SharedSecret)
	}
	encBlock, err := aes.NewCipher([]byte(SharedSecret))
	if err != nil {
		log.Fatalf("Error creating AES cipher for encryption: %v\n", err)
	}
	var text []byte

	r := bufio.NewReader(conn)
	for {
		if DEBUG {
			log.Printf("Listening for new message...\n")
		}

		plaintext := []byte{}
		isPrefix := true

		for isPrefix {
			text, isPrefix, err = r.ReadLine()
			if DEBUG {
				fmt.Printf("isPrefix == %v\n", isPrefix)
			}
			if err != nil {
				if err == io.EOF {
					log.Printf("* Disconnected: %s\n", conn.RemoteAddr())
					return
				}
				log.Printf("Error reading message from local conn %s: %v\n",
					conn.RemoteAddr(), err)
				break
			}
			if DEBUG {
				fmt.Printf("text == %s\n", text)
			}
			plaintext = append(plaintext, text...)
		}
		// Print user input to screen
		now := time.Now().Format(time.Kitchen)
		fmt.Printf("[%s] %s: %s\n", now, conn.RemoteAddr(), plaintext)

		// Encrypt plaintext coming from local user over telnet
		ciphertext, err := crypt.AESEncryptBytes(encBlock, plaintext)
		if err != nil {
			log.Printf("Error encrypting '%s': %v\n", plaintext, err)
			continue
		}
		// Asynchronously write encrypted message to all remote
		// connections
		go func() {
			connList.WriteToRemotes <- &types.Cipherstore{conn, ciphertext}
		}()
	}
}