加密 - Android中的MD5哈希
我有一个简单的Android客户端,需要与简单的C#HTTP侦听器“对话”。 我想通过在POST请求中传递用户名/密码来提供基本级别的身份验证。
MD5哈希在C#中是微不足道的,并且为我的需求提供了足够的安全性,但我似乎无法在android端找到如何做到这一点。
编辑:只是为了解决有关MD5弱点的问题 - C#服务器运行在我的Android客户端用户的PC上。 在许多情况下,他们将使用自己的局域网上的Wi-Fi访问服务器,但他们可能会自己冒险从互联网上访问它。 此外,服务器上的服务需要使用MD5的传递到我无法控制的第三方应用程序。
12个解决方案
199 votes
这是您可以使用的实现(更新为使用更新的Java约定 - for:each循环,StringBuilder而不是StringBuffer):
public static final String md5(final String s) {
final String MD5 = "MD5";
try {
// Create MD5 Hash
MessageDigest digest = java.security.MessageDigest
.getInstance(MD5);
digest.update(s.getBytes());
byte messageDigest[] = digest.digest();
// Create Hex String
StringBuilder hexString = new StringBuilder();
for (byte aMessageDigest : messageDigest) {
String h = Integer.toHexString(0xFF & aMessageDigest);
while (h.length() < 2)
h = "0" + h;
hexString.append(h);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
虽然不推荐用于甚至涉及基本安全级别的系统(MD5被认为已损坏且易于利用),但对于基本任务而言,它有时已足够。
Den Delimarsky answered 2019-04-24T04:15:37Z
39 votes
在Android 2.2中,接受的答案对我不起作用。 我不知道为什么,但它正在“吃掉”我的一些零(0)。Apache commons也无法在Android 2.2上运行,因为它使用仅从Android 2.3.x开始支持的方法。 另外,如果你想只是MD5一个字符串,那么Apache公共文件太复杂了。 为什么要让整个库只使用一个小函数...
最后,我在这里找到了以下代码片段,它对我来说非常有用。 我希望它对某些人有用......
public String MD5(String md5) {
try {
java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
byte[] array = md.digest(md5.getBytes("UTF-8"));
StringBuffer sb = new StringBuffer();
for (int i = 0; i < array.length; ++i) {
sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
}
return sb.toString();
} catch (java.security.NoSuchAlgorithmException e) {
} catch(UnsupportedEncodingException ex){
}
return null;
}
Andranik answered 2019-04-24T04:16:10Z
24 votes
androidsnippets.com代码不能可靠地工作,因为0似乎被从结果哈希中删除。
这里有更好的实施方案。
public static String MD5_Hash(String s) {
MessageDigest m = null;
try {
m = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
m.update(s.getBytes(),0,s.length());
String hash = new BigInteger(1, m.digest()).toString(16);
return hash;
}
Christian answered 2019-04-24T04:16:50Z
16 votes
如果使用Apache Commons Codec是一个选项,那么这将是一个更短的实现:
String md5Hex = new String(Hex.encodeHex(DigestUtils.md5(data)));
或SHA:
String shaHex= new String(Hex.encodeHex(DigestUtils.sha("textToHash")));
以上来源。
请点击链接并提出他的解决方案,以奖励正确的人。
Maven回购链接:[https://mvnrepository.com/artifact/commons-codec/commons-codec]
目前的Maven依赖(截至2016年7月6日):
commons-codec
commons-codec
1.10
tbraun answered 2019-04-24T04:17:52Z
11 votes
使用DigestUtils的上述解决方案对我不起作用。 在我的Apache commons版本(2013年的最新版本)中没有这样的类。
我在一个博客中找到了另一个解决方案。 它完美无缺,不需要Apache公共资源。 它看起来比上面接受的答案中的代码短一些。
public static String getMd5Hash(String input) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] messageDigest = md.digest(input.getBytes());
BigInteger number = new BigInteger(1, messageDigest);
String md5 = number.toString(16);
while (md5.length() < 32)
md5 = "0" + md5;
return md5;
} catch (NoSuchAlgorithmException e) {
Log.e("MD5", e.getLocalizedMessage());
return null;
}
}
您将需要这些导入:
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
wzbozon answered 2019-04-24T04:18:32Z
7 votes
这是Andranik和Den Delimarsky在上面的答案的略微变化,但它更简洁,不需要任何按位逻辑。 相反,它使用内置的%02X方法将字节转换为两个字符的十六进制字符串(不剥离0)。 通常我会评论他们的答案,但我没有这样做的声誉。
public static String md5(String input) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
StringBuilder hexString = new StringBuilder();
for (byte digestByte : md.digest(input.getBytes()))
hexString.append(String.format("%02X", digestByte));
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
如果您想要返回小写字符串,则只需将%02X更改为%02x。
编辑:使用BigInteger和wzbozon的答案一样,你可以使答案更简洁:
public static String md5(String input) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
BigInteger md5Data = new BigInteger(1, md.digest(input.getBytes()));
return String.Format("%032X", md5Data);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
rsimp answered 2019-04-24T04:19:13Z
3 votes
我在Kotlin做了一个简单的图书馆。
在Root build.gradle中添加
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
在App build.gradle
implementation 'com.github.1AboveAll:Hasher:-SNAPSHOT'
用法
在科特林
val ob = Hasher()
然后使用hash()方法
ob.hash("String_You_Want_To_Encode",Hasher.MD5)
ob.hash("String_You_Want_To_Encode",Hasher.SHA_1)
它将分别返回MD5和SHA-1。
更多关于图书馆
[https://github.com/1AboveAll/Hasher]
Himanshu Rawat answered 2019-04-24T04:20:32Z
1 votes
在我们的MVC应用程序中,我们生成了长参数
using System.Security.Cryptography;
using System.Text;
...
public static string getMD5(long id)
{
// convert
string result = (id ^ long.MaxValue).ToString("X") + "-ANY-TEXT";
using (MD5 md5Hash = MD5.Create())
{
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(result));
// Create a new Stringbuilder to collect the bytes and create a string.
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
sBuilder.Append(data[i].ToString("x2"));
// Return the hexadecimal string.
result = sBuilder.ToString().ToUpper();
}
return result;
}
和Android应用程序相同(然后帮助Andranik)
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
...
public String getIdHash(long id){
String hash = null;
long intId = id ^ Long.MAX_VALUE;
String md5 = String.format("%X-ANY-TEXT", intId);
try {
MessageDigest md = java.security.MessageDigest.getInstance("MD5");
byte[] arr = md.digest(md5.getBytes());
StringBuffer sb = new StringBuffer();
for (int i = 0; i < arr.length; ++i)
sb.append(Integer.toHexString((arr[i] & 0xFF) | 0x100).substring(1,3));
hash = sb.toString();
} catch (NoSuchAlgorithmException e) {
Log.e("MD5", e.getMessage());
}
return hash.toUpperCase();
}
Oleg Grabets answered 2019-04-24T04:21:10Z
0 votes
MD5有点旧,SHA-1是一个更好的算法,这里有一个例子。
(正如他们在那篇文章中指出的那样,Java自己处理这个问题,没有特定于Android的代码。)
Adam answered 2019-04-24T04:21:45Z
0 votes
我使用下面的方法通过传递你想要获得md5的字符串给我md5
public static String getMd5Key(String password) {
// String password = "12131123984335";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(password.getBytes());
byte byteData[] = md.digest();
//convert the byte to hex format method 1
StringBuffer sb = new StringBuffer();
for (int i = 0; i < byteData.length; i++) {
sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
}
System.out.println("Digest(in hex format):: " + sb.toString());
//convert the byte to hex format method 2
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < byteData.length; i++) {
String hex = Integer.toHexString(0xff & byteData[i]);
if (hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
System.out.println("Digest(in hex format):: " + hexString.toString());
return hexString.toString();
} catch (Exception e) {
// TODO: handle exception
}
return "";
}
Ghanshyam Patidar answered 2019-04-24T04:22:11Z
0 votes
实际上,在其他建议中,过于浪费于Hex()转换。
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
public static String md5string(String s) {
return toHex(md5plain(s));
}
public static byte[] md5plain(String s) {
final String MD5 = "MD5";
try {
// Create MD5 Hash
MessageDigest digest = java.security.MessageDigest.getInstance(MD5);
digest.update(s.getBytes());
return digest.digest();
} catch (NoSuchAlgorithmException e) {
// never happens
e.printStackTrace();
return null;
}
}
public static String toHex(byte[] buf) {
char[] hexChars = new char[buf.length * 2];
int v;
for (int i = 0; i < buf.length; i++) {
v = buf[i] & 0xFF;
hexChars[i * 2] = HEX_ARRAY[v >>> 4];
hexChars[i * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars);
}
Gena Batsyan answered 2019-04-24T04:22:39Z
-1 votes
为Scala语言提供的解决方案(稍短):
def getMd5(content: Array[Byte]) =
try {
val md = MessageDigest.getInstance("MD5")
val bytes = md.digest(content)
bytes.map(b => Integer.toHexString((b + 0x100) % 0x100)).mkString
} catch {
case ex: Throwable => null
}
david.perez answered 2019-04-24T04:23:07Z