SoFunction
Updated on 2025-03-08

Example of parameter encryption code in Android data transmission

In Android development, it is inevitable to encounter problems that require encrypting and decrypting some data content and saving it to local files, or transmitting it to other servers and devices over the network, but it is not absolutely safe to use encryption. If the encryption function is used incorrectly, the encrypted data is easily subject to reverse cracking attacks. There are also many problems with encryption algorithms that developers do not realize.

1. Data transmission

1) There are two most commonly used methods in http requests: get and post; generally post requests are suitable for submissions, while get requests are suitable for requesting data.

2) There are about three commonly used data encryption: AES, DES, Base64

2. Base64 encryption

The aes encryption used here, and then the string is encoded using Base64. There is an increase in vector to improve the difficulty of encryption cracking. The method of encrypting a paragraph of parameter is as follows:

/**
    * Encrypt post request data
    * @param params
    * @return
    * @throws Throwable
    */
public static byte[] encryptParams(HashMap<String, String> params) throws Throwable{
	if (params == null){
		return null;
	}
	StringBuilder stringBuilder = new StringBuilder();
	Iterator<<String, String>> iterator = ().iterator();
	while (()){
		<String,String> entry = ();
		String key = ();
		String value = ();
		if (() > 0){
			("&");
		}
		(key).append("=").append((value));
	}
	byte[] buff = ().getBytes("utf-8");
	byte[] iv = new byte[16];
	Random random = new Random();
	(iv);
	byte[] data = (buff,PASSWORD,iv);
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	(iv,0,);
	(data,0,);
	byte[] out = ();
	try {
		();
	}
	catch (Throwable e){
		();
	}
	return out;
}

3. Encountering problems

During the process of using Base64, I encountered some problems, as follows:

1) The requested string is truncated. After grabbing the link, a space string is obtained. After analysis, this is actually a new line. . . . .

The solution is to process the output string as follows when requesting:

Will  
.(input, ) 
Change to 
.(input, Base64.NO_WRAP); 

2) In addition to the above, there are actually spaces, at this time, you can use substitution, as follows:

// Encryption:byte[] bodyBytes = (hashMap); 
// Use base64encode to do the final encryptionString result = new BASE64Encoder().encode(bodyBytes); 
String ans_url = headUrl + ("\n",""); 

4. Summary

The base64encode encoding will truncate the string after 76 bits. If a Chinese string is contained, the plus sign will be replaced with a space.

The above is the entire content of this article about parameter encryption code examples in Android data transmission. I hope it will be helpful to everyone. Interested friends can continue to refer to this site:

Introduction to the method of implementing file association of Android development

Detailed explanation of Android subcontracting MultiDex strategy

If there are any shortcomings, please leave a message to point it out.