前言:
对于MD5加密,在博文 MD5 加密 已经有了详细说明,这边博文将其用Android 实现。
MD5 的使用跟之前介绍几种块加密(或称分组加密)AES加密、DES加密、3DES加密有些不同。
之前几种分组加密使用的java 接口为Cipher类,而这边以及后期介绍的其他几种单向加密(如 SHA-1、SHA-224、SHA-512等)使用的java 接口为MessageDigest 类,如果对此感兴趣,可以研读下source code,这里不做过多的介绍。
实例:
参考之前分组加密的coding 形式,对于这几种单向加密也同样使用一个Helper 类:
public class MessageDigestHelper {
private static byte[] getDigest(String alg, String strSource) {
if (strSource == null) {
strSource = "";
}
try {
MessageDigest md = MessageDigest.getInstance(alg);
return md.digest(strSource.getBytes());
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private static byte[] getDigest(String alg, File file) {
if (!FileOperation.isFileExists(file))
return null;
FileInputStream fileIs = null;
byte buffer[] = new byte[4096];
int len;
MessageDigest md;
try {
md = MessageDigest.getInstance(alg);
fileIs = new FileInputStream(file);
while ((len = fileIs.read(buffer)) != -1) {
md.update(buffer, 0, len);
}
return md.digest();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fileIs != null) {
try {
fileIs.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
private static String digest2String(byte[] digest) {
String result = "";
if (digest == null)
return result;
for (byte b : digest) {
String temp = Integer.toHexString(b & 0xff);
if (temp.length() == 1) {
temp = "0" + temp;
}
result += temp;
}
return result;
}
public static String getStringMD(String alg, String strSource) {
return digest2String(getDigest(alg, strSource));
}
public static String getFileMD(String alg, File file) {
return digest2String(getDigest(alg, file));
}
public static String getFileMD(String alg, String filePath) {
File file = new File(filePath);
if (!FileOperation.isFileExists(file))
return "";
return digest2String(getDigest(alg, file));
}
}
具体算法通过参数传进来,这边Helper 支持String 的MD 、file 的MD。
参考之前的分组加密,具体的Encryption 类也做了一个公共类:
public class MessageDigestEncryption {
private String mAlgorithm;
public MessageDigestEncryption(String alg) {
mAlgorithm = alg;
}
public void setAlgorithm(String alg) {
mAlgorithm = alg;
}
public String getStringMD(String string) {
return MessageDigestHelper.getStringMD(mAlgorithm, string);
}
public String getFileMD(String filePath) {
return MessageDigestHelper.getFileMD(mAlgorithm, filePath);
}
public String getFileMD(File file) {
return MessageDigestHelper.getFileMD(mAlgorithm, file);
}
}
这样如果想要实现MD5 就只需要将参数设为“MD5”,如果想要实现SHA-1,参数可以设为“SHA-1”。
本文这边重新多加了一个类MD5Encryption:
public class MD5Encryption extends MessageDigestEncryption {
private static final String ALGORITHM = "MD5";
public MD5Encryption() {
this(ALGORITHM);
}
public MD5Encryption(String alg) {
super(alg);
}
}
Activity 的button 部分如下:
private void testEncryptionMD5() {
Button md5 = (Button) findViewById(R.id.encrypt_md5);
md5.setOnClickListener(this);
}
private void md5Encryption() {
mOperationTitle.setText(getString(R.string.encrypt_md5));
{
String str1 = "";
String result = "";
MD5Encryption encryption = new MD5Encryption();
result = encryption.getStringMD(str1);
Log.d(TAG, "==== md5: " + result);
String strSource = "hliuhiufhliuhsd;jfijso;goshgosjogijsgo;j";
mBeforeOperation.setText(getString(R.string.before_operation, strSource));
result = encryption.getStringMD(strSource);
mAfterOperation.setText(getString(R.string.after_operation, result));
}
// if (mFileEncryption != null) {
// Log.d(TAG, "==== test file, encryption with md5 ...");
// String filePath = "/storage/emulated/0/hehe.png";
// mFileEncryption.setAlgorithm(FileEncryption.ALG_MODE_MD5);
// mFileEncryption.start(FileEncryption.MODE_ENCRYPT, "", filePath);
// Log.d(TAG, "==== test file, encryption end");
// }
}
做了两手准备验证,一个是通过log 打印出空字符串对应的md5值,另一个是非空的字符串md5值显示到屏幕上。
具体UI 上的逻辑可以根据实际需要实现,不过多的做解释。
结果:
12-12 06:16:43.783 3643 3643 D TestEncryptionActivity: ==== md5: d41d8cd98f00b204e9800998ecf8427e
这两个结论可以通过PC 上一些工具验证,结果是完全一样的。
更多的加密可以看: