日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內(nèi)最全IT社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當前位置:首頁 > php開源 > 綜合技術 > Android Studio JNI開發(fā)基礎篇

Android Studio JNI開發(fā)基礎篇

來源:程序員人生   發(fā)布時間:2016-09-27 09:20:52 閱讀次數(shù):2670次

      • 前言
      • 環(huán)境搭建
      • 創(chuàng)建Native代碼
      • 使用

前言

開發(fā)進程中,為了數(shù)據(jù)交互安全,決定對數(shù)據(jù)進行des加密,然落后行前后交互;但是,如果密鑰放置在android代碼里面,就算是混淆,反編譯也很容易讓人拿到密鑰,數(shù)據(jù)加密的安全度不高,因此斟酌通過jni來返回1個密鑰對數(shù)據(jù)進行加解密。從而到達數(shù)據(jù)的安全性。

環(huán)境搭建

  • 下載NDK
    通過android studio去下載NDK插件;打開File–>Project Structure–>SDK Location–>Android NDK Location,以下圖:

    如果是第1次,沒有下載NDK插件,在紅色箭頭的地方有個按鈕用于下載安裝,點擊等待下載便可,使用AS下載的NDK會默許反正你的sdk的目錄路徑下;亦或選中藍色剪頭的按鈕選擇已下載好的NDK;點擊“OK”,配置成功以后,會在local.properties文件下看見相應的路徑指向,以下圖:

  • 配置NDK環(huán)境變量

    • 創(chuàng)建NDK_HOME
      右鍵我的電腦–>屬性–>高級系統(tǒng)設置–>環(huán)境變量–>系統(tǒng)變量–>新建–>創(chuàng)建1個“NDK_HOME”,NDK的路徑就是上1步中配置的路徑,以下圖:

    • 添加path
      系統(tǒng)變量中找到Path–>編輯,將;%NDK_HOME%添加至path中,以下圖:

    • 驗證配置是不是成功
      在cmd下輸入“ndk-build”指令,如果出現(xiàn)下圖結果,即配置成功:

創(chuàng)建Native代碼

  • 在java目錄下創(chuàng)建1個帶有native方法的java文件

    /** * native */ public class SmartCardJniUtil { /** * 加載so庫 */ static { System.loadLibrary("SCJniUtil"); } /** * 獲得密鑰 * @return 密鑰 */ public native String getKey(); }
  • 通過Build–>Make Project項目
    成功以后,在app–>intermediates–>classes–>debug–>個人項目路徑下找到上面新建的native類的class文件,以下圖:

  • 生成頭文件

    • File—settings—plugins下勾選Termainal

    • 指令生成頭文件
      javah -d jni -classpath ;….\build\intermediates\classes\debug 類的包名。
      按著以上說明,首先通過cd app\src\main跳轉(zhuǎn)到main目錄下,然后輸入以下指令:
      javah -d jni -classpath E:**\sdk\platforms\android⑵0\android.jar;….\build\intermediates\classes\debug com.a.b.c.SmartCardJniUtil
      以下圖:

      成功以后會在main目錄下多1個jni文件,并包括了1個.h的頭文件,以下圖:

    • 創(chuàng)建.c文件
      在jni目錄下右鍵–>New–>C/C++ Source File創(chuàng)建1個SmartCardJniUtil.c文件,然后引入頭文件,返回密鑰,以下代碼:

      #include "com_a_b_c_SmartCardJniUtil.h" JNIEXPORT jstring JNICALL Java_com_a_b_c_SmartCardJniUtil_getKey (JNIEnv *env, jobject jobject1) { return (*env)->NewStringUTF(env, "1122334455667788"); };
  • 在build.gradle文件下添加配置,代碼以下:

    buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' ndk { moduleName "SCJniUtil" //生成的so名字 abiFilters "armeabi", "armeabi-v7a", "x86" //輸出指定3種abi體系結構下的so庫。 } } debug { ndk { moduleName "SCJniUtil" //生成的so名字 abiFilters "armeabi", "armeabi-v7a", "x86" //輸出指定3種abi體系結構下的so庫。 } } } sourceSets { main { jniLibs.srcDirs = ['libs'] } }
  • 清算并重新make項目,生成so庫,以下圖:

  • 關于Android.mk文件說明
    通過eclipse開發(fā)jni的時候,需要進行1個Android.mk文件的配置,但是Android Studio中并沒有做,沒有做不代表沒有,AS自動幫我們生成了,不需要我們自己去創(chuàng)建并配置,以下圖:

  • 混淆文件中添加不混淆native的配置

    -keepclasseswithmembernames class * { native <methods>; }

使用

  • 通過jni獲得密鑰
    當以上步驟完成以后,實例化native對象,調(diào)用相應的方法便可獲得到相應的結果,代碼以下:

    //實例化對象 SmartCardJniUtil jniUtil = new SmartCardJniUtil(); //獲得密鑰 jniUtil.getKey()
  • des加解密工具

    public class DesUtil { /** * 數(shù)據(jù)加密,算法(DES) * * @param data 要進行加密的數(shù)據(jù) * @return 加密后的數(shù)據(jù) */ public static String encryptBasedDes(String data, String keyStr) { keyStr = StringUtilsSimple.leftPad(keyStr, 16, "0"); byte[] DES_KEY = ByteUtil.hexStr2Byte(keyStr); String encryptedData = null; try { // DES算法要求有1個可信任的隨機數(shù)源 SecureRandom sr = new SecureRandom(); DESKeySpec deskey = new DESKeySpec(DES_KEY); // 創(chuàng)建1個密匙工廠,然后用它把DESKeySpec轉(zhuǎn)換成1個SecretKey對象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(deskey); // 加密對象 Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key, sr); // 加密,并把字節(jié)數(shù)組編碼成字符串 encryptedData = ByteUtil.hexToStr(cipher.doFinal(data.getBytes())); } catch (Exception e) { // log.error("加密毛病,毛病信息:", e); throw new RuntimeException("加密毛病,毛病信息:", e); } return encryptedData; } /** * 數(shù)據(jù)解密,算法(DES) * * @param cryptData 加密數(shù)據(jù) * @return 解密后的數(shù)據(jù) */ public static String decryptBasedDes(String cryptData, String keyStr) { keyStr = StringUtilsSimple.leftPad(keyStr, 16, "0"); byte[] DES_KEY = ByteUtil.hexStr2Byte(keyStr); String decryptedData = null; try { // DES算法要求有1個可信任的隨機數(shù)源 SecureRandom sr = new SecureRandom(); DESKeySpec deskey = new DESKeySpec(DES_KEY); // 創(chuàng)建1個密匙工廠,然后用它把DESKeySpec轉(zhuǎn)換成1個SecretKey對象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(deskey); // 解密對象 Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, key, sr); // 把字符串解碼為字節(jié)數(shù)組,并解密 decryptedData = new String(cipher.doFinal(ByteUtil.hexStr2Byte(cryptData))); } catch (Exception e) { // log.error("解密毛病,毛病信息:", e); throw new RuntimeException("解密毛病,毛病信息:", e); } return decryptedData; } public static void main(String[] args) { // TODO Auto-generated method stub String str = "123456789"; // DES數(shù)據(jù)加密 long time = System.currentTimeMillis(); String s1 = encryptBasedDes(str, "1122334411223344"); System.out.println("time1:" + (System.currentTimeMillis() - time)); System.out.println(s1); // DES數(shù)據(jù)解密 long time2 = System.currentTimeMillis(); String s2 = decryptBasedDes(s1, "1122334411223344"); System.out.println("time2:" + (System.currentTimeMillis() - time2)); System.err.println(s2); } }
  • 文本左右補位的工具

    public class StringUtilsSimple { /** * A String for a space character. * * @since 3.2 */ public static final String SPACE = " "; /** * <p>The maximum size to which the padding constant(s) can expand.</p> */ private static final int PAD_LIMIT = 8192; // Empty checks // ----------------------------------------------------------------------- /** * <p> * Checks if a CharSequence is empty ("") or null. * </p> * * <pre> * StringUtils.isEmpty(null) = true * StringUtils.isEmpty("") = true * StringUtils.isEmpty(" ") = false * StringUtils.isEmpty("bob") = false * StringUtils.isEmpty(" bob ") = false * </pre> * * <p> * NOTE: This method changed in Lang version 2.0. It no longer trims the * CharSequence. That functionality is available in isBlank(). * </p> * * @param cs * the CharSequence to check, may be null * @return {@code true} if the CharSequence is empty or null * @since 3.0 Changed signature from isEmpty(String) to * isEmpty(CharSequence) */ public static boolean isEmpty(final CharSequence cs) { return cs == null || cs.length() == 0; } /** * <p> * Left pad a String with a specified String. * </p> * * <p> * Pad to a size of {@code size}. * </p> * * <pre> * StringUtils.leftPad(null, *, *) = null * StringUtils.leftPad("", 3, "z") = "zzz" * StringUtils.leftPad("bat", 3, "yz") = "bat" * StringUtils.leftPad("bat", 5, "yz") = "yzbat" * StringUtils.leftPad("bat", 8, "yz") = "yzyzybat" * StringUtils.leftPad("bat", 1, "yz") = "bat" * StringUtils.leftPad("bat", -1, "yz") = "bat" * StringUtils.leftPad("bat", 5, null) = " bat" * StringUtils.leftPad("bat", 5, "") = " bat" * </pre> * * @param str * the String to pad out, may be null * @param size * the size to pad to * @param padStr * the String to pad with, null or empty treated as single space * @return left padded String or original String if no padding is necessary, * {@code null} if null String input */ public static String leftPad(final String str, final int size, String padStr) { if (str == null) { return null; } if (isEmpty(padStr)) { padStr = SPACE; } final int padLen = padStr.length(); final int strLen = str.length(); final int pads = size - strLen; if (pads <= 0) { return str; // returns original String when possible } if (padLen == 1 && pads <= PAD_LIMIT) { return leftPad(str, size, padStr.charAt(0)); } if (pads == padLen) { return padStr.concat(str); } else if (pads < padLen) { return padStr.substring(0, pads).concat(str); } else { final char[] padding = new char[pads]; final char[] padChars = padStr.toCharArray(); for (int i = 0; i < pads; i++) { padding[i] = padChars[i % padLen]; } return new String(padding).concat(str); } } /** * <p>Left pad a String with a specified character.</p> * * <p>Pad to a size of {@code size}.</p> * * <pre> * StringUtils.leftPad(null, *, *) = null * StringUtils.leftPad("", 3, 'z') = "zzz" * StringUtils.leftPad("bat", 3, 'z') = "bat" * StringUtils.leftPad("bat", 5, 'z') = "zzbat" * StringUtils.leftPad("bat", 1, 'z') = "bat" * StringUtils.leftPad("bat", -1, 'z') = "bat" * </pre> * * @param str the String to pad out, may be null * @param size the size to pad to * @param padChar the character to pad with * @return left padded String or original String if no padding is necessary, * {@code null} if null String input * @since 2.0 */ public static String leftPad(final String str, final int size, final char padChar) { if (str == null) { return null; } final int pads = size - str.length(); if (pads <= 0) { return str; // returns original String when possible } if (pads > PAD_LIMIT) { return leftPad(str, size, String.valueOf(padChar)); } return repeat(padChar, pads).concat(str); } /** * <p>Returns padding using the specified delimiter repeated * to a given length.</p> * * <pre> * StringUtils.repeat('e', 0) = "" * StringUtils.repeat('e', 3) = "eee" * StringUtils.repeat('e', -2) = "" * </pre> * * <p>Note: this method doesn't not support padding with * <a href="http://www.unicode.org/glossary/#supplementary_character">Unicode Supplementary Characters</a> * as they require a pair of {@code char}s to be represented. * If you are needing to support full I18N of your applications * consider using {@link #repeat(String, int)} instead. * </p> * * @param ch character to repeat * @param repeat number of times to repeat char, negative treated as zero * @return String with repeated character * @see #repeat(String, int) */ public static String repeat(final char ch, final int repeat) { final char[] buf = new char[repeat]; for (int i = repeat - 1; i >= 0; i--) { buf[i] = ch; } return new String(buf); } /** * <p>Right pad a String with a specified String.</p> * * <p>The String is padded to the size of {@code size}.</p> * * <pre> * StringUtils.rightPad(null, *, *) = null * StringUtils.rightPad("", 3, "z") = "zzz" * StringUtils.rightPad("bat", 3, "yz") = "bat" * StringUtils.rightPad("bat", 5, "yz") = "batyz" * StringUtils.rightPad("bat", 8, "yz") = "batyzyzy" * StringUtils.rightPad("bat", 1, "yz") = "bat" * StringUtils.rightPad("bat", -1, "yz") = "bat" * StringUtils.rightPad("bat", 5, null) = "bat " * StringUtils.rightPad("bat", 5, "") = "bat " * </pre> * * @param str the String to pad out, may be null * @param size the size to pad to * @param padStr the String to pad with, null or empty treated as single space * @return right padded String or original String if no padding is necessary, * {@code null} if null String input */ public static String rightPad(final String str, final int size, String padStr) { if (str == null) { return null; } if (isEmpty(padStr)) { padStr = SPACE; } final int padLen = padStr.length(); final int strLen = str.length(); final int pads = size - strLen; if (pads <= 0) { return str; // returns original String when possible } if (padLen == 1 && pads <= PAD_LIMIT) { return rightPad(str, size, padStr.charAt(0)); } if (pads == padLen) { return str.concat(padStr); } else if (pads < padLen) { return str.concat(padStr.substring(0, pads)); } else { final char[] padding = new char[pads]; final char[] padChars = padStr.toCharArray(); for (int i = 0; i < pads; i++) { padding[i] = padChars[i % padLen]; } return str.concat(new String(padding)); } } /** * <p>Right pad a String with a specified character.</p> * * <p>The String is padded to the size of {@code size}.</p> * * <pre> * StringUtils.rightPad(null, *, *) = null * StringUtils.rightPad("", 3, 'z') = "zzz" * StringUtils.rightPad("bat", 3, 'z') = "bat" * StringUtils.rightPad("bat", 5, 'z') = "batzz" * StringUtils.rightPad("bat", 1, 'z') = "bat" * StringUtils.rightPad("bat", -1, 'z') = "bat" * </pre> * * @param str the String to pad out, may be null * @param size the size to pad to * @param padChar the character to pad with * @return right padded String or original String if no padding is necessary, * {@code null} if null String input * @since 2.0 */ public static String rightPad(final String str, final int size, final char padChar) { if (str == null) { return null; } final int pads = size - str.length(); if (pads <= 0) { return str; // returns original String when possible } if (pads > PAD_LIMIT) { return rightPad(str, size, String.valueOf(padChar)); } return str.concat(repeat(padChar, pads)); } }
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 少妇视频一区 | 国产一级黄色影片 | 亚洲一二三四区 | 在线免费观看毛片 | 成人黄色免费网址 | 成人免费高清 | 精品91久久 | 欧美日韩中文国产一区 | 国产视频在线看 | 亚洲精品在线电影 | 五月婷婷在线观看 | 一区二区高清在线 | 国产区视频在线观看 | 三级网站免费看 | 黄色片网站免费在线观看 | 日韩视频区 | 欧美日一区二区三区 | 污视频免费看 | 色片免费观看 | 国产福利电影网 | 亚洲日本中文字幕 | 国产伦精品一区二区三区照片 | 亚洲九九九 | 成人福利视频网站 | 亚洲综合在线视频 | 欧洲av不卡 | 中文字幕国 | 精品国产乱码一区二区三区 | 亚洲永久| 日韩精品久久久久久久电影99爱 | 亚洲欧洲精品一区二区 | 国产精品成人免费视频 | 精品在线视频一区 | 日韩精品视频在线 | aaa成人 | 国产精品一区二区三区av | 久久久91精品国产一区二区三区 | 免费黄色电影在线观看 | 超碰999 | 成人精品鲁一区一区二区 | 一级黄色小视频 |