Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
sfter opened this issue Dec 16, 2022 · 4 comments
Closed

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

sfter opened this issue Dec 16, 2022 · 4 comments

Comments

@sfter
Copy link

sfter commented Dec 16, 2022

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
Copy link
Collaborator

gouguoyin commented Dec 16, 2022

Print the error

hexStr  := "13863dbecd8078bf2c8d4a795fe203e4"
decrypt := dongle.Decrypt.FromHexString(str).ByAes(cipher)
fmt.Println(decrypt.Error)

@sfter
Copy link
Author

sfter commented Dec 16, 2022

<?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
Copy link
Collaborator

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

PHP OPENSSL_RAW_DATA be equal to dongle. No

@gouguoyin
Copy link
Collaborator

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

@dromara dromara deleted a comment from sfter Dec 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants