crypt.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "github.com/tal-tech/go-zero/core/codec"
  6. )
  7. const (
  8. pubKey = `-----BEGIN PUBLIC KEY-----
  9. MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD7bq4FLG0ctccbEFEsUBuRxkjE
  10. eJ5U+0CAEjJk20V9/u2Fu76i1oKoShCs7GXtAFbDb5A/ImIXkPY62nAaxTGK4KVH
  11. miYbRgh5Fy6336KepLCtCmV/r0PKZeCyJH9uYLs7EuE1z9Hgm5UUjmpHDhJtkAwR
  12. my47YlhspwszKdRP+wIDAQAB
  13. -----END PUBLIC KEY-----`
  14. body = "hello"
  15. )
  16. var key = []byte("q4t7w!z%C*F-JaNdRgUjXn2r5u8x/A?D")
  17. func main() {
  18. encrypter, err := codec.NewRsaEncrypter([]byte(pubKey))
  19. if err != nil {
  20. log.Fatal(err)
  21. }
  22. decrypter, err := codec.NewRsaDecrypter("private.pem")
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. output, err := encrypter.Encrypt([]byte(body))
  27. if err != nil {
  28. log.Fatal(err)
  29. }
  30. actual, err := decrypter.Decrypt(output)
  31. if err != nil {
  32. log.Fatal(err)
  33. }
  34. fmt.Println(actual)
  35. out, err := codec.EcbEncrypt(key, []byte(body))
  36. if err != nil {
  37. log.Fatal(err)
  38. }
  39. ret, err := codec.EcbDecrypt(key, out)
  40. if err != nil {
  41. log.Fatal(err)
  42. }
  43. fmt.Println(string(ret))
  44. }