Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "crypto/rand"
  7. "fmt"
  8. "io"
  9. "net"
  10. )
  11.  
  12. func main() {
  13. key := []byte("example key 1234")
  14.  
  15. conn, err := net.Dial("tcp", "127.0.0.1:9080")
  16.  
  17. if err != nil {
  18. panic(err)
  19. }
  20.  
  21. defer func() {
  22. fmt.Println("Bye")
  23.  
  24. conn.Close()
  25. }()
  26.  
  27. block, cipherErr := aes.NewCipher(key)
  28.  
  29. if cipherErr != nil {
  30. fmt.Errorf("Can't create cipher:", cipherErr)
  31.  
  32. return
  33. }
  34.  
  35. iv := make([]byte, aes.BlockSize)
  36.  
  37. if _, randReadErr := io.ReadFull(rand.Reader, iv); randReadErr != nil {
  38. fmt.Errorf("Can't build random iv", randReadErr)
  39.  
  40. return
  41. }
  42.  
  43. _, ivWriteErr := conn.Write(iv)
  44.  
  45. if ivWriteErr != nil {
  46. fmt.Errorf("Can't send IV:", ivWriteErr)
  47.  
  48. return
  49. } else {
  50. fmt.Println("IV Sent:", iv)
  51. }
  52.  
  53. stream := cipher.NewCFBEncrypter(block, iv)
  54.  
  55. data := [][]byte{
  56. []byte("Test one"),
  57. []byte("Hello crypto"),
  58. []byte("Hello word"),
  59. []byte("Hello excel"),
  60. []byte("Hello powerpoint"),
  61. }
  62.  
  63. for _, d := range data {
  64. encrypted := make([]byte, len(d))
  65.  
  66. stream.XORKeyStream(encrypted, d)
  67.  
  68. writeLen, writeErr := conn.Write(encrypted)
  69.  
  70. if writeErr != nil {
  71. fmt.Errorf("Write failed:", writeErr)
  72.  
  73. return
  74. }
  75.  
  76. fmt.Println("Encrypted Data Written:", string(d), encrypted, writeLen)
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement