Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "crypto/rand"
  7. "encoding/base32"
  8. "encoding/json"
  9. "fmt"
  10. "io"
  11. "os"
  12. )
  13. func encrypt(key, text []byte) string {
  14. block, err := aes.NewCipher(key)
  15. if err != nil {
  16. return ""
  17. }
  18. cipherText := make([]byte, aes.BlockSize+len(text))
  19. iv := cipherText[:aes.BlockSize]
  20. if _, err = io.ReadFull(rand.Reader, iv); err != nil {
  21. return ""
  22. }
  23. stream := cipher.NewCFBEncrypter(block, iv)
  24. stream.XORKeyStream(cipherText[aes.BlockSize:], text)
  25. return base32.StdEncoding.EncodeToString(cipherText)
  26. }
  27.  
  28.  
  29. func main(){
  30. key := []byte("1234567890123456")
  31. var login,psw,host,port string
  32. fmt.Print("Enter login: ")
  33. fmt.Fscan(os.Stdin, &login)
  34. fmt.Print("Enter password: ")
  35. fmt.Fscan(os.Stdin, &psw)
  36. fmt.Print("Enter host: ")
  37. fmt.Fscan(os.Stdin, &host)
  38. fmt.Print("Enter port: ")
  39. fmt.Fscan(os.Stdin, &port)
  40.  
  41. map1 := map[string]string{
  42. "login": login,
  43. "port": port,
  44. "password": encrypt(key, []byte(psw)),
  45. "host": host,
  46. }
  47. map2, _ := json.Marshal(map1)
  48. file,_ := os.Create("config.json")
  49. file.Write(map2)
  50. defer file.Close()
  51. file, err := os.Open("config.json")
  52. if err != nil {
  53. fmt.Println(err)
  54. os.Exit(1)
  55. }
  56. defer file.Close()
  57. dat := make([]byte,1000)
  58. n, err := file.Read(dat)
  59. var data map[string]string
  60. if err := json.Unmarshal([]byte(dat[:n]), &data); err != nil {
  61. panic(err)
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement