java中應用DES加密解密實例。本站提示廣大學習愛好者:(java中應用DES加密解密實例)文章只能為提供參考,不一定能成為您想要的結果。以下是java中應用DES加密解密實例正文
本文實例講述了Android受權拜訪網頁的完成辦法,即應用Webview顯示OAuth Version 2.a ImplicitGrant方法受權的頁,然則關於挪動終端不建議應用Authorize code grant方法受權。
詳細功效代碼以下所示:
import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.net.http.SslError; import android.os.Bundle; import android.util.Log; import android.webkit.SslErrorHandler; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import com.tencent.weibo.oauthv2.OAuthV2; import com.tencent.weibo.oauthv2.OAuthV2Client; /** * 應用Webview顯示OAuth Version 2.a ImplicitGrant方法受權的頁 * (挪動終端不建議應用Authorize code grant方法受權 * 本類應用辦法 * 挪用本類的處所請添加以下代碼 * //請將OAuthV2Activity改成類的類名 * Intent intent = new Intent(OAuthV2Activity.this, OAuthV2AuthorizeWebView.class); * intent.putExtra("oauth", oAuth); //oAuth為OAuthV2類的實例,寄存受權相干信?? * startActivityForResult(intent, myRrequestCode); //請設置適合的requsetCode * 重寫吸收回調信息的方 * if (requestCode==myRrequestCode) { //對應之前設置的的myRequsetCode * if (resultCode==OAuthV2AuthorizeWebView.RESULT_CODE) { * //獲得前往的OAuthV2類實例oAuth * oAuth=(OAuthV2) data.getExtras().getSerializable("oauth"); * } * } * @see android.app.Activity#onActivityResult(int requestCode, int resultCode, Intent data) */ public class MyWebView extends Activity { public final static int RESULT_CODE = 2; private OAuthV2 oAuth; private final String TAG = "MyWebView"; private WebView mWebView; @SuppressLint("NewApi") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.webview_qq); mWebView = (WebView) findViewById(R.id.qq_mywebview);; mWebView.setVerticalScrollBarEnabled(false); mWebView.setHorizontalScrollBarEnabled(false); Intent intent = this.getIntent(); oAuth = (OAuthV2) intent.getExtras().getSerializable("oauth"); String urlStr = OAuthV2Client.generateImplicitGrantUrl(oAuth); WebSettings webSettings = mWebView.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setSupportZoom(true); mWebView.requestFocus(); mWebView.loadUrl(urlStr); System.out.println(urlStr.toString()); Log.i(TAG, "WebView Starting...."); WebViewClient client = new WebViewClient() { /* 回調辦法,當頁面加載時履行*/ @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { Log.i(TAG, "WebView onPageStarted..."); Log.i(TAG, "URL = " + url); if (url.indexOf("access_token=") != -1) { int start=url.indexOf("access_token="); String responseData=url.substring(start); OAuthV2Client.parseAccessTokenAndOpenId(responseData, oAuth); Intent intent = new Intent(); intent.putExtra("oauth", oAuth); setResult(RESULT_CODE, intent); finish(); } super.onPageStarted(view, url, favicon); Log.i(TAG, "999999999"); } /* TODO Android2.2及以上版本能力應用該辦法,今朝https://open.t.qq.com中存在http資本會惹起sslerror,待網站修改後可去失落該方*/ public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { if ((null != view.getUrl()) && (view.getUrl().startsWith("https://open.t.qq.com"))) { handler.proceed();// 接收證書 } else { handler.cancel(); // 默許的處置方法,WebView釀成空白 } // handleMessage(Message msg); 其他處置 } }; mWebView.setWebViewClient(client); } }�
/**
* 解密以byte[]密文輸出,以byte[]明文輸入
*
* @param bt
* 待解密的字節碼
* @return 解密後的字節碼
*/
private byte[] getDesCode(byte[] bt) {
Cipher cipher;
byte[] byteFina = null;
try {
// 獲得Cipher實例
cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, iv);
byteFina = cipher.doFinal(bt);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
}