Android md5加密与php md5加密一致详解
Androidmd5加密与phpmd5加密一致详解
在Android开发过程中加密密码常常采用md5加密方式,然而如果服务器端采用PHP开发(php采用md5加密很简单,直接md5($str)),很可能与Java的md5加密不一致。以下方法是md5加密与php一致的源码:
importjava.math.BigInteger;
importjava.security.MessageDigest;
importjava.security.NoSuchAlgorithmException;
publicclassMD5{
//密码加密与php加密一致
publicstaticStringmd5(Stringinput)throwsNoSuchAlgorithmException{
Stringresult=input;
if(input!=null){
MessageDigestmd=MessageDigest.getInstance("MD5");//or"SHA-1"
md.update(input.getBytes());
BigIntegerhash=newBigInteger(1,md.digest());
result=hash.toString(16);
while(result.length()<32){//31位string
result="0"+result;
}
}
returnresult;
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!