golang-module / dongle

A simple, semantic and developer-friendly golang package for encoding&decoding and encryption&decryption
https://pkg.go.dev/github.com/golang-module/dongle
MIT License
875 stars 67 forks source link

openssl_encrypt($str, 'AES-128-ECB', $key, $padding) #14

Closed sfter closed 1 year ago

sfter commented 1 year ago

The following is the php implementation code

<?php

        $padding = OPENSSL_RAW_DATA;
        $str = "483";
        $key = "1234567890";
        $data = openssl_encrypt($str, 'AES-128-ECB', $key, $padding);
        $data = strtolower(bin2hex($data));
        echo $data;
        echo "\r\n";

        $decrypted = openssl_decrypt(hex2bin($data), 'AES-128-ECB', $key, $padding);
        echo $decrypted;
        echo "\r\n";
?>

output: 13863dbecd8078bf2c8d4a795fe203e4 483

The following is the Go implementation code

func TestDecryptAes(t *testing.T) {
    key := "1234567890"
    cipher := dongle.NewCipher()
    cipher.SetMode(dongle.ECB) // CBC、CFB、OFB、CTR、ECB
    //cipher.SetPadding(dongle.Zero) // No、Zero、PKCS5、PKCS7、AnsiX923、ISO97971
    //key = dongle.Encrypt.FromString(key).ByMd5().String()
    cipher.SetKey(key) // key 长度必须是 16、24 或 32 字节
    //cipher.SetIV("")                                  // iv 长度必须是 16 字节,ECB 模式不需要设置 iv

    str := "13863dbecd8078bf2c8d4a795fe203e4"
    str = dongle.Decrypt.FromHexString(str).ByAes(cipher).ToString()
    println(str)
}

output Empty string

But I can't decrypt with go. How can I decrypt with go?

gouguoyin commented 1 year ago

Print the error

hexStr  := "13863dbecd8078bf2c8d4a795fe203e4"
decrypt := dongle.Decrypt.FromHexString(str).ByAes(cipher)
fmt.Println(decrypt.Error)
sfter commented 1 year ago
<?php

        $padding = OPENSSL_RAW_DATA;
        $str = "483";
        $key = "5953kdz0K4pG5fd32nEQfdka23M2fkwa";
        $data = openssl_encrypt($str, 'AES-128-ECB', $key, $padding);
        $data = strtolower(bin2hex($data));
        echo $data;
        echo "\r\n";

        $decrypted = openssl_decrypt(hex2bin($data), 'AES-128-ECB', $key, $padding);
        echo $decrypted;
        echo "\r\n";
?>

php output: cbb287ca4ce7c8d84993044e2b33222a 483

func TestDecryptAes(t *testing.T) {
    key := "5953kdz0K4pG5fd32nEQfdka23M2fkwa"
    cipher := dongle.NewCipher()
    // CBC、CFB、OFB、CTR、ECB
    cipher.SetMode(dongle.ECB)
    // No、Zero、PKCS5、PKCS7、AnsiX923、ISO97971
    cipher.SetPadding(dongle.Zero)
    // key 长度必须是 16、24 或 32 字节
    cipher.SetKey(key)
    // iv 长度必须是 16 字节,ECB 模式不需要设置 iv
    //cipher.SetIV("")

    str := "cbb287ca4ce7c8d84993044e2b33222a"
    //str = dongle.Decode.FromString(str).ByHex().ToString()
    str = dongle.Decrypt.FromHexString(str).ByAes(cipher).ToString()
    println(str)
}

golang output: $״�&��@�0����z

There is no error message, but the result of Golang is inconsistent with that of Php.

gouguoyin commented 1 year ago

Golang use padding with Zero, php should also use padding with OPENSSL_ZERO_PADDING

PHP OPENSSL_RAW_DATA be equal to dongle. No

gouguoyin commented 1 year ago

In addition, NoPadding requires that the encrypted string must be multiple of 16