简体   繁体   English

通过URL后不保留以Base64编码的加密字符串吗?

[英]Encrypted String encoded in Base64 not preserved after going through URL?

A "data" String is encoded in Base64 (URL safe version) then crypted into a byte array . “数据” StringBase64 (URL安全版本)编码,然后加密为byte array This byte array is turned into a String and sent to a REST web service. byte array变成一个String ,并发送到REST Web服务。 When it arrives on the web service side, the length of the String has changed. 当它到达Web服务端时, String的长度已更改。 I am not a specialist, what do I do wrong? 我不是专家,我该怎么办?

The code: 编码:

//CLIENT SIDE
import org.apache.commons.codec.binary.Base64;    

String data = "My message to be encrypted";        
PublicKey pubKey = readPublicKeyFromFile();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] encryptedData = cipher.doFinal(Base64.encodeBase64URLSafe(data.getBytes()));

System.out.println("length of original data string: " + data.length());
System.out.println("length of original data string turned into byte array: " + data.getBytes().length);
System.out.println("length of data string encrypted into byte array: " + encryptedData.length);

String encryptedDataToString = new String(encrypedData,"UTF-8");
System.out.println("length encrypted String into byte array, converted back to String for url: " + encryptedDataToString.length);

httpclient = new DefaultHttpClient();
builder = new URIBuilder();
builder.setScheme("http").setHost(xxx + "webresources/GetData/" + path)
                    .setParameter("data", encryptedDataToString);
            uri = builder.build();



//SERVER SIDE
import org.apache.commons.codec.binary.Base64;

@GET
@Path("path")
@Produces("text/plain")
public String getToken(@QueryParam("data") String data) {

System.out.println("length of data: " + data.length());
System.out.println("length of data to byte array wthout decoding: " + data.getBytes().length);
System.out.println("length of data to byte decoded: " + Base64.decodeBase64(data).length);


//CONSOLE

//CLIENT SIDE
length of original data string: 37
length of original data string turned into byte array: 37
length of data string encrypted into byte array: 256
length encrypted String into byte array, converted back to String for url: 256

//SERVER SIDE
INFO: length of data: 237
INFO: length of data to byte wthout decoding: 444
INFO: length of data to byte decoded: 4

The data String as it is appearing in the URL, client side : T%23%EF%BF%BD%0C%EF%BF%BD%EF%BF%BD7%EF%BF%BD%EF%BF%BDk%EF%BF%BDA%EF%B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%EF%BF%BD%EF%BF%BD%7B%07%EF%BF%BDO%EF%BF%BD%EF%BF%BD%11%CB%9C%EF%BF%BD%1Dk%EF%BF%BDkL8%EF%BF%BD*%EF%BF%BD%1D%EF%BF%BDl%EF%BF%BD%EF%BF%BD%EF%BF%BD%02%EF%BF%BD D +%EF%BF%BD%CF%86R%EF%BF%BDW%EF%BF%BDU%EF%BF%BDxo%EF%BF%BDd%EF%BF%BD%08-%3F%EF%BF %BD%2F%EF%BF%BD%5Bg%EF%BF%BD2%EF%BF%BD%EF%BF%BDk%5E%EF%BF%BDm%EF%BF%BD%EF%BF%BD %25%EF%BF%BD%EF%BF%BD *%07%5E%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BDU%EF%BF%BD% 7F%EF%BF%BDP%EF%BF%BD%EF%BF%BD%EF%BF%BD%7B%07%EF%BF%BDO%EF%BF%BD%EF%BF%BD%11% CB%9C%EF%BF%BD%1Dk%EF%BF%BDkL8%EF%BF%BD *%EF%BF%BD%1D%EF%BF%BD1%EF%BF%BD%EF%BF%BD %EF%BF%BD%02%EF%BF%BD

The data String as it is received, server side : 服务器端接收到的数据String
T# 7 k A L ^"Be gmE n = ܀ IM eM 5 & } NAQ lT VXR i 3U ep áb6 εy9x |! 0 ˏ} &A -ލ t Tezs , ݔ % t Q } F n φR W U xo d-? / [g 2 k^ m % ^ U P { O ˜ k kL8 l T# 7 k A L ^^''Be gmE n = ܀ IM eM 5 & } NAQ.lT VXR i 3U ep áb6 εy9x |! 0 ˏ} &A -ލ t Tezs , ݔ %t tQ}} FF n φR W U xo d-?/ [g 2k ^ m%% ^^ U P { O kkL8 l

(not sure these are the characters actually received, or just badly printed on the console?) (不确定这些字符是实际收到的字符,还是在控制台上打印得不好的字符?)

You have to encrypt it first and then encode it with base64 and send it via net. 您必须先对其进行加密,然后使用base64对其进行编码,然后通过net发送。 Otherwise the encryption creates (pseuro)random data and some parts of that might not be properly encoded for transfer via web and will inevitably get corrupted. 否则,加密会创建(伪)随机数据,其中某些部分可能未正确编码以通过Web传输,因此不可避免地会遭到破坏。

The same applies if you want to compress your data. 如果要压缩数据,则同样适用。 You do the compression before encrypting them because after the encryption it will be much harder for the algorithm to compress seemingly random data. 您在进行加密之前先进行压缩,因为在加密之后,算法将很难压缩看似随机的数据。
Generally the thing you send to a web service should be base64 encoded as a last step/alteration. 通常,您发送到Web服务的内容应使用base64编码,作为最后一步/更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM