B
B
bsbak2020-05-11 15:50:52
go
bsbak, 2020-05-11 15:50:52

What is the correct way to write from PHP hex2bin and openssl_decode to Go?

There is such an implementation in php:

<?php

    $token = 'c01d7c2768ba633990d458a78e0e4ce50627c20cffe54c8c';
    $key = 'j0klBHYlVQOkc1LCE6ECZwBf1Siyvt5l';
    $result = openssl_decrypt(hex2bin($token), 'BF-ECB', $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING);
    print_r($result);

    // результат: {"t":1,"u":"1341/957"}
    ?>


You can try on some php fiddle phpfiddle.org/lite

Tell me how to implement this on Go? I tried like this, but even on hex2bin there is already an error https://play.golang.org/p/gKiMMkT7CxR

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
falconandy, 2020-05-11
@bsbak

package main

import (
  "encoding/hex"
  "fmt"

  "golang.org/x/crypto/blowfish"
)

func main() {
  encryptedText, _ := hex.DecodeString("c01d7c2768ba633990d458a78e0e4ce50627c20cffe54c8c")
  key := []byte("j0klBHYlVQOkc1LCE6ECZwBf1Siyvt5l")

  cipher, _ := blowfish.NewCipher(key)
  plainText := make([]byte, len(encryptedText))
  size := blowfish.BlockSize

  for bs, be := 0, size; bs < len(encryptedText); bs, be = bs+size, be+size {
    cipher.Decrypt(plainText[bs:be], encryptedText[bs:be])
  }
  fmt.Println(string(plainText))
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question