There is such an application scenario. When we put some important files in the asset folder, we can directly get the file by decompressing the .apk. We don’t want some files involving important information to be decompiled. At this time, we need to encrypt the file first, and then put it in the resource directory in Android, and then decrypt it when used. In modern cryptography, the security of encryption systems is based on keys rather than algorithms. Now I will introduce a complete set of encryption, decryption and application processes. I think this encryption process is very reliable in terms of practicality and security. It is also a commonly used practice on the market. The core logic is actually quite simple. After all, the most difficult part of the encryption and decryption algorithm implementation is ready-made. Part of our company has also used this process, which is of course more complicated than what I am talking about. 1. Introduction It mainly involves the application of the following algorithms: RSA, AES, and Base64 encoding. The basic idea is to use [AES algorithm + AES key] to encrypt files. In order to ensure the security of the key, the AES key will be encrypted using [RSA algorithm + RSA private key]. If you are not familiar with these algorithms, you can take a look at the article "Common Encryption Methods and Application Scenarios" by our boss. It is enough to know the general principles and usage methods, because the algorithms are ready-made in Java and can be used directly. After arranging the process, it is the above flowchart, which is divided into three parts:
One thing to add is the public and private keys of the RSA algorithm. From the third block, we can see that the RSA public and private keys are not placed in the resource file. In fact, if you think about it, you will know that if the encrypted file, the AES key for encryption and decryption, and the RSA key used to encrypt the AES key are all placed in the folder, there will be no security (Note: the encryption and decryption algorithm can be modified to be unique to your company, which is what our company does). Therefore, in order to ensure security, our RSA public and private keys are dynamically obtained through the code in the application signature (.keystore signature file). If you are interested, you can read this article: [Extract private keys and certificates from Java Keystore files]. 2. Block 1: Encryption tool for encryption The Java interface development of the tool is implemented through the Java swing package. Those who are interested in swing can refer to this article Introduction to Java Swing Graphical Interface Development, which explains it in great detail. At the beginning, there is no AES key, so we need to generate a secure key, so generate a random AES key and save it. The operation page interface of the encryption tool is: 2.1. Generate a random key Generating a random key is mainly divided into several steps:
Because the generated key is byte[], it is displayed on the interface through Base64 encoding.
SignKey.getSignKeyPair() is used to obtain the public and private keys required for the RSA algorithm. It comes from our application signature. Everyone should be familiar with it. Application packaging and uploading requires signature packaging. Java provides an API to obtain the private key and certificate of the testkey.keystore file (generate one yourself using studio), and put the testkey.keystore file in the directory:
The parameters required to get the testkey.keystore are the same as those required for signing the packaged application, and are obtained through the keystore class provided by java. Then the AES key is encrypted with the testkey.keystore private key just obtained, and then converted into a string through Base64 for display. The encoding is converted only for displaying the key. 2.2. Exporting Keys Export the key to a file and import it directly next time to decrypt the file. To export the key, you need to convert the Base64 key string in the text box into Byte[] using Base64 before saving it.
2.3 Encrypted Files The key is already available, and the AES algorithm is ready-made. Just call the API to encrypt:
Select the file and encrypt it using the AES algorithm and AES key. The final result is as follows. If there is no key that can decrypt it, I lose. 3. Block 2: Decryption by decryption tool There is actually no need to explain the decryption process, because the decryption process is the reverse process of the encryption process. This decryption is not used in the application, but to facilitate us to update the encrypted file. The file must be decrypted before modifying it. 3.1. Import AES key This key is the key we generated earlier. After importing it, use the RSA public key signed by the application to decrypt the AES key:
3.2 Decrypting Files After getting the pure version of the AES key, you can directly call the AES algorithm to decrypt the file:
Compared with the AES encryption process, it can be found that it is just switching the AES algorithm mode. 3. Block 3: Decrypting files in Android applications To decrypt the file, you need to add the encrypted AES key to the resource folder, which is the key derived above, and the encrypted file. The premise for correct decryption is that your application signature is the same as the signature used to encrypt the file. 3.1. Decrypting the AES key The main difference between decrypting files in Android applications and decrypting files in Java tools lies in the acquisition of RSA keys. In Java tools, the application signature testkey.keystore is owned by the developer, and all the information in it can be obtained. In Android, applications are released to the application market, and anyone can download our package. The application signature can only obtain its public key through the API provided by Android.
After obtaining the public key of the application signature testkey.keystore, the process is basically the same as the operation in the Java tool, using the RSA public key to decrypt the AES key.
Finally, use the decrypted AES key to decrypt the file. 3.2. AES key decryption file Get the file stream of the encrypted file through the resource manager, and use the AES algorithm to decrypt the file stream using the AES key.
After getting the encrypted file stream, the purpose is achieved, which can be parsed into a string and displayed:
The example effect diagram is as follows. Please pay attention to the content in the red box. Because I am too lazy to create a new project, I tested it with the original project: Currently, the tool uses the encryption and decryption algorithms that are common on the market. You can change the algorithm, such as DES or other symmetric and asymmetric algorithms, or even your own modified algorithms. If you want to run the example demonstration: Just run the java file and you can open the encryption and decryption tool. The encryption and decryption tool interface is a small part extracted from our toolkit. After all, writing the interface is very annoying. Thanks to our great god who wrote such a tool many years ago. |
<<: Android 9.0 is now available! Xiaomi, OnePlus, and OV are the first to release it
>>: Google announces Android version market share in May: More people are eating "Oreo"
Recently, Chukong Technology and Microsoft jointl...
Believe that the copy you write is actually prett...
References: [1] Liu Xiliang, Liu Yingliang. Appli...
Double 11, Double 12, Christmas Eve and other fes...
Apple is promoting safer and more compliant iPhon...
Recently, many areas in Anhui have also reported n...
With the rise of short video platforms, the trend...
Recently, the Logistics Support Department of the...
Recently, Hurun Research Institute released the &...
Produced by: Science Popularization China Produce...
Author|Ctrip's front-end framework team prov...
Jokes are actually a way that people write texts n...
Preface I believe that many developers have more ...
Daily Specials is a platform provided by Taobao w...
A few days ago, Apple’s official support website ...