注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

你想快速掌握安卓编程吗

 
 
 

日志

 
 

Android示例加密 - MD5和SHA-1(内联java代码)  

2017-10-02 21:41:49|  分类: B.文件校验 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

1楼帖子附件代码如下

#Region  Project Attributes 
#ApplicationLabel: securityUtil
#VersionCode: 1
#VersionName: 
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes 
#FullScreen: False
#IncludeTitle: True
#End Region

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.

End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim nativeMe As JavaObject

End Sub

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("main")
nativeMe.InitializeContext
Dim stringToConvert As String
stringToConvert = "This is a Security Util"
Dim logstring As String

logstring = nativeMe.RunMethod("getMD5",Array(stringToConvert))
Log(logstring)
logstring = nativeMe.RunMethod("getSHA1",Array(stringToConvert))
Log(logstring)

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

#If Java

import java.security.MessageDigest;
import java.util.Locale;

import android.text.TextUtils;

public static String getMD5(String text) {
String md5 = null;
if (TextUtils.isEmpty(text)) {
return md5;
}
MessageDigest md5Digest = null;
try {
md5Digest = MessageDigest.getInstance("MD5");
} catch (Exception e) {
e.printStackTrace();
return md5;
}
char[] charArray = text.toCharArray();
byte[] byteArray = new byte[charArray.length];

for (int index = 0; index < charArray.length; index++) {
byteArray[index] = (byte) charArray[index];
}

byte[] md5Bytes = md5Digest.digest(byteArray);
StringBuffer hexValue = new StringBuffer();

for (int index = 0; index < md5Bytes.length; index++) {
int val = ((int) md5Bytes[index]) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}

md5 = hexValue.toString();
return md5;
}

public static String bytes2Hex(byte[] bytes) {
String hs = "";
String stmp = "";
for (int n = 0; n < bytes.length; n++) {
stmp = (Integer.toHexString(bytes[n] & 0XFF));
if (stmp.length() == 1) {
hs += "0" + stmp;
} else {
hs += stmp;
}
}
return hs.toLowerCase(Locale.ENGLISH);
}

public static String getSHA1(String text) {
String sha1 = null;
if (TextUtils.isEmpty(text)) {
return sha1;
}
MessageDigest sha1Digest = null;
try {
sha1Digest = MessageDigest.getInstance("SHA-1");
} catch (Exception e) {
return sha1;
}
byte[] textBytes = text.getBytes();
sha1Digest.update(textBytes, 0, text.length());
byte[] sha1hash = sha1Digest.digest();
return bytes2Hex(sha1hash);
}


#End If


  评论这张
 
阅读(13)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017