The basic implementation of AES encryption, there is a problem is the security of code. We know that Java layer code is very easy to decompile, it is possible to leak our encryption and key content, then what should we do? We can implement encryption in C/s + +, compiled into the form of so libraries, which can be called by the Java implementation, which greatly enhances the program security, because the so decompile result is arm instruction, not smali in Java so easy to understand. The use of the full-C + + implementation may be troublesome, in fact, we can also simplify a part, only the key using JNI implementation, the other is implemented in Java, this will be simpler, the following is the specific operation;
(1) New project AES
Add the native interface to the Java class, and note that the native interface and System.loadlibrary () are written well. The code is as follows:
Public synchronized Static native String Getfromnativeiv (); Public synchronized Static native String getstringfromnative ();
(2) generate the corresponding. h header file According to the class file, execute the following command
javah-d JNI-classpath c:\Users\sodinochen\AppData\Local\Android\sdk\platforms\android-16\android.jar; \.. \build\intermediates\classes\debug com.aes.jniaes.MainActivity
3) Next, set the library file name (the resulting so file name) in the Build.gradle in the app module directory. Find the Defaultconfig for the Gradle file, and add the following to it:
defaultconfig { "Com.aes.jniaes " 1 "1.0 " ndk { "aesjni" // generated so name abifilters "Armeabi", "armeabi-v7a "," x86 " // outputs specify the so library under three ABI architectures. Currently dispensable. } }
(4) Finally, add the static initialization LoadLibrary code, add the following:
Static { system.loadlibrary ("Checkapp-jni"); // the name of the so file }
(5) encryption and decryption class Aesutil:
Public classAesutils {//Encrypt Public StaticString Encrypt (String sSrc, String SKey, String sIv)throwsException {if(SKey = =NULL) {System.out.print ("Key is null NULL"); return NULL; } //determine if key is 16-bit if(Skey.length ()! = 16) {System.out.print ("Key length is not 16-bit"); return NULL; } byte[] Raw =skey.getbytes (); Secretkeyspec Skeyspec=NewSecretkeyspec (Raw, "AES"); Cipher Cipher= Cipher.getinstance ("aes/cbc/pkcs5padding");//"algorithm/Mode/complement Method"Ivparameterspec IV =NewIvparameterspec (Siv.getbytes ());//using CBC mode, a vector IV is required to increase the strength of the encryption algorithmCipher.init (Cipher.encrypt_mode, Skeyspec, iv); byte[] encrypted =cipher.dofinal (Ssrc.getbytes ()); String mm=NewString (Base64.encode (encrypted, base64.default)); returnmm//The BASE64 is used here to do transcoding functions, and can play 2 times encryption function. } //decryption Public StaticString Decrypt (String sSrc, String SKey, String sIv)throwsException {Try { //determine if key is correct if(SKey = =NULL) {System.out.print ("Key is null NULL"); return NULL; } //determine if key is 16-bit if(Skey.length ()! = 16) {System.out.print ("Key length is not 16-bit"); return NULL; } byte[] raw = Skey.getbytes ("UTF-8"); Secretkeyspec Skeyspec=NewSecretkeyspec (Raw, "AES"); Cipher Cipher= Cipher.getinstance ("aes/cbc/pkcs5padding"); Ivparameterspec IV=NewIvparameterspec (Siv.getbytes ()); Cipher.init (Cipher.decrypt_mode, Skeyspec, iv); byte[] encrypted1 =Base64.decode (SSRC, Base64.default); Try { byte[] Original =cipher.dofinal (encrypted1); String originalstring=NewString (original); returnoriginalstring; } Catch(Exception e) {System.out.println (e.tostring ()); return NULL; } } Catch(Exception ex) {System.out.println (ex.tostring ()); return NULL; } }}
Implementation of AES's CBC mode encryption and decryption via JNI