Spring Cloud Config's handling of special character encryption

I wrote an introduction to the encryption and decryption of configuration content by the configuration center: "Spring Cloud Builds Microservice Architecture: Distributed Configuration Center (Encryption and Decryption)" . In this article, there is a problem: when the encrypted content contains some special characters such as =, +when you use the commands like the one mentioned in the previous article curl localhost:7001/encrypt -dto encrypt and decrypt, you will find that the special characters are lost. .

Such as the following situation:

$ curl localhost:7001/encrypt -d eF34+5edo=
a34c76c4ddab706fbcae0848639a8e0ed9d612b0035030542c98997e084a7427
$ curl localhost:7001/decrypt -d a34c76c4ddab706fbcae0848639a8e0ed9d612b0035030542c98997e084a7427
eF34 5edo

It can be seen that after encryption and decryption, some special characters are lost. Since I have made a little pit here before, I took the time to write it out and share it with friends who encounter the same problem, I hope it will help you.

Problem cause and solution

In fact, the reason for this problem is specified in the official document, and I can only blame myself for being too careless, as follows:

If you are testing like this with curl, then use --data-urlencode (instead of -d) or set an explicit Content-Type: text/plain to make sure curl encodes the data correctly when there are special characters ('+' is particularly tricky).

Therefore, when using curl, the correct posture should be:

$ curl localhost:7001/encrypt -H 'Content-Type:text/plain' --data-urlencode "eF34+5edo="
335e618a02a0ff3dc1377321885f484fb2c19a499423ee7776755b875997b033

$ curl localhost:7001/decrypt -H 'Content-Type:text/plain' --data-urlencode "335e618a02a0ff3dc1377321885f484fb2c19a499423ee7776755b875997b033"
eF34+5edo=

So, what if we write our own tools to encrypt and decrypt? Here OkHttpis an example for reference:

private String encrypt(String value) {
    String url = "http://localhost:7001/encrypt";
    Request request = new Request.Builder()
            .url(url)
            .post(RequestBody.create(MediaType.parse("text/plain"), value.getBytes()))
            .build();

    Call call = okHttpClient.newCall(request);
    Response response = call.execute();
    ResponseBody responseBody = response.body();
    return responseBody.string();
}

private String decrypt(String value) {
    String url = "http://localhost:7001/decrypt";
    Request request = new Request.Builder()
            .url(url)
            .post(RequestBody.create(MediaType.parse("text/plain"), value.getBytes()))
            .build();

    Call call = okHttpClient.newCall(request);
    Response response = call.execute();
    ResponseBody responseBody = response.body();
    return responseBody.string();
}

The following topical tutorials may be of interest to you

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325129310&siteId=291194637
Recommended