java中加密的实现方法(MD5,MD2,SHA)
java中加密的实现方法(MD5,MD2,SHA)
实例代码:注释都很清楚,
importjava.security.MessageDigest; importjavax.xml.bind.annotation.adapters.HexBinaryAdapter; publicclassMain{ staticStringsrc="Hello,sahadev!"; publicstaticvoidmain(String[]args){ MD5(); MD2(); SHA(); } /** *MD5加密 */ publicstaticvoidMD5(){ try{ //获取MD5加密工具 MessageDigestmd=MessageDigest.getInstance("MD5"); //加密 byte[]digest=md.digest(src.getBytes()); //获取二进制十六进制互转工具 HexBinaryAdapterhexBinaryAdapter=newHexBinaryAdapter(); //将二进制数组转换为十六进制字符串 Stringmarshal=hexBinaryAdapter.marshal(digest); //输出结果 System.out.println(marshal); }catch(Exceptione){ e.printStackTrace(); } } /** *MD2加密 */ publicstaticvoidMD2(){ try{ //获取MD2加密工具 MessageDigestmd=MessageDigest.getInstance("MD2"); //加密 byte[]digest=md.digest(src.getBytes()); //获取二进制十六进制互转工具 HexBinaryAdapterhexBinaryAdapter=newHexBinaryAdapter(); //将二进制数组转换为十六进制字符串 Stringmarshal=hexBinaryAdapter.marshal(digest); //输出结果 System.out.println(marshal); }catch(Exceptione){ e.printStackTrace(); } } publicstaticvoidSHA(){ try{ //获取MD2加密工具 MessageDigestmd=MessageDigest.getInstance("SHA"); //加密 byte[]digest=md.digest(src.getBytes()); //获取二进制十六进制互转工具 HexBinaryAdapterhexBinaryAdapter=newHexBinaryAdapter(); //将二进制数组转换为十六进制字符串 Stringmarshal=hexBinaryAdapter.marshal(digest); //输出结果 System.out.println(marshal); }catch(Exceptione){ e.printStackTrace(); } } }
输出结果:
MD5:8FC69C57ACC2258C7B4A9D39DC4C940B MD2:9EAF08289903A29C3C58D2AAA25BD83D SHA:1B1330BFC6257FC9F4B4ED5CB605FA4109608CCA
以上就是java加密的实例,如疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!