cfb_encrypt

    0

    1

    toolcrypto

    cfb crypto of golang

    Shortcut: cfb_crypto

    Filename pattern: *.go

    
    // https://bruinsslot.jp/post/golang-crypto/
    var bytes = []byte{35, 46, 57, 24, 85, 35, 24, 74, 87, 35, 88, 98, 66, 32, 14, 05}
    
    const DefaultSecret string = "abc&1*~#^2^#s0^=)^^7%b34"
    
    func encode(b []byte) string {
      return base64.StdEncoding.EncodeToString(b)
    }
    func decode(s string) []byte {
      data, err := base64.StdEncoding.DecodeString(s)
      if err != nil {
      panic(err)
      }
      return data
    }
    
    // Encrypt method is to encrypt or hide any classified text
    func Encrypt(text, secret string) (string, error) {
      block, err := aes.NewCipher([]byte(secret))
      if err != nil {
      return "", err
      }
      plainText := []byte(text)
      cfb := cipher.NewCFBEncrypter(block, bytes)
      cipherText := make([]byte, len(plainText))
      cfb.XORKeyStream(cipherText, plainText)
      return encode(cipherText), nil
    }
    
    // Decrypt method is to extract back the encrypted text
    func Decrypt(text, secret string) (string, error) {
      block, err := aes.NewCipher([]byte(secret))
      if err != nil {
      return "", err
      }
      cipherText := decode(text)
      cfb := cipher.NewCFBDecrypter(block, bytes)
      plainText := make([]byte, len(cipherText))
      cfb.XORKeyStream(plainText, cipherText)
      return string(plainText), nil
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.