HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android信息-摘要校验信息大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

在Android中,常用检验信息.防止信息被篡改。


最简单方式md5:

@H_944_12@public static String md5( String plaintext) { String result = null; try { messageDigest md = messageDigest.geTinstance("MD5"); md.update(plaintext.getBytes()); byte b[] = md.digest(); int i; StringBuffer buf = new StringBuffer(""); for (int offset = 0; offset < b.length; offset++) { i = b[offset]; if (i < 0) i += 256; if (i < 16) buf.append("0"); buf.append(Integer.toHexString(i)); } // result = buf.toString(); //md5 32bit // result = buf.toString().subString(8,24))); //md5 16bit result = buf.toString().subString(8,24); System.out.println("mdt 16bit: " + buf.toString().subString(8,24)); System.out.println("md5 32bit: " + buf.toString()); } catch (NoSuchAlgorithmException E) { e.printStackTrace(); } return result; }

但是这样也不能完全保证传递的信息正确。用户可能同时改变数据和md5值。这样也无法保证数据完全正确。


下面用加密信息摘要校验,更安全。

@H_944_12@import java.io.UnsupportedEncodingException; import java.security.messageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; public class Digestutil { private static String encodingCharset = "UTF-8"; /** * @param aValue * @param aKey * @return */ public static String hmacSign(String aValue,String aKey) { byte k_ipad[] = new byte[64]; byte k_opad[] = new byte[64]; byte keyb[]; byte value[]; try { keyb = aKey.getBytes(encodingCharset); value = aValue.getBytes(encodingCharset); } catch (UnsupportedEncodingException E) { keyb = aKey.getBytes(); value = aValue.getBytes(); } Arrays.fill(k_ipad,keyb.length,64,(bytE) 54); Arrays.fill(k_opad,(bytE) 92); for (int i = 0; i < keyb.length; i++) { k_ipad[i] = (bytE) (keyb[i] ^ 0x36); k_opad[i] = (bytE) (keyb[i] ^ 0x5c); } messageDigest md = null; try { md = messageDigest.geTinstance("MD5"); } catch (NoSuchAlgorithmException E) { return null; } md.update(k_ipad); md.update(value); byte dg[] = md.digest(); md.reset(); md.update(k_opad); md.update(dg,16); dg = md.digest(); return toHex(dg); } public static String toHex(byte input[]) { if (input == null) return null; StringBuffer output = new StringBuffer(input.length * 2); for (int i = 0; i < input.length; i++) { int current = input[i] & 0xff; if (current < 16) output.append("0"); output.append(Integer.toString(current,16)); } return output.toString(); } /** * * @param args * @param key * @return */ public static String getHmac(String[] args,String key) { if (args == null || args.length == 0) { return (null); } StringBuffer str = new StringBuffer(); for (int i = 0; i < args.length; i++) { str.append(args[i]); } return (hmacSign(str.toString(),key)); } /** * @param aValue * @return */ public static String digest(String avalue) { aValue = aValue.trim(); byte value[]; try { value = aValue.getBytes(encodingCharset); } catch (UnsupportedEncodingException E) { value = aValue.getBytes(); } messageDigest md = null; try { md = messageDigest.geTinstance("SHA"); } catch (NoSuchAlgorithmException E) { e.printStackTrace(); return null; } return toHex(md.digest(value)); } } }

调用String den = Digestutil.hmacSign(String,key);只要客户端和服务器端同一个key加密检验信息,就能保证信息完整。

大佬总结

以上是大佬教程为你收集整理的android信息-摘要校验信息全部内容,希望文章能够帮你解决android信息-摘要校验信息所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。