欢迎来到天天文库
浏览记录
ID:36005300
大小:353.35 KB
页数:13页
时间:2019-04-29
《java加密与解密》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、摘要: java加密与解密,java秘钥体系摘要算法摘要算法主要包含三大类型:MD算法,SHA算法,已经基于KEY的MAC算法MD算法MD算法主要有MD2,MD3,MD4以及最常用的MD5。SHA算法主要有SHA-1,SHA-224,SHA-256,SHA-384,SHA-512。MAC算法是基于哈西散列算法(MD系列或者SHA系列等),在HASH散列进行KEY加密JAVA密码体系对主流摘要算法的支持MD5算法的调用示例packagecom.rcl.platform.demo;importjava.security.InvalidKeyException;importjava.security
2、.MessageDigest;importjava.security.NoSuchAlgorithmException;importjava.security.NoSuchProviderException;importjava.security.Provider;importjava.security.Security;importjavax.crypto.Mac;importjavax.crypto.SecretKey;importjavax.crypto.spec.SecretKeySpec;importorg.bouncycastle.jce.provider.BouncyCastle
3、Provider;publicclassProviderTest{publicstaticvoidmain(String[]args)throwsNoSuchAlgorithmException,NoSuchProviderException,InvalidKeyException{MessageDigestmessageDigest=MessageDigest.getInstance("MD5");messageDigest.update("我是MD".getBytes());byte[]md5=messageDigest.digest();System.out.println(byte2H
4、exStr(md5));Security.addProvider(newBouncyCastleProvider());//注册BouncyCastleProviderProviderprovider=Security.getProvider("BC");messageDigest=MessageDigest.getInstance("MD4",provider);messageDigest.update("我是MD".getBytes());byte[]md4=messageDigest.digest();System.out.println(byte2HexStr(md4));messag
5、eDigest=MessageDigest.getInstance("SHA512");//调用SHA算法messageDigest.update("我是SHA".getBytes());byte[]sha512=messageDigest.digest();System.out.println(byte2HexStr(sha512));SecretKeysecretKey=newSecretKeySpec("woshikey".getBytes(),"HmacSHA512");//HMAC算法调用Macmac=Mac.getInstance(secretKey.getAlgorithm())
6、;mac.init(secretKey);byte[]hmacSHA224=mac.doFinal("我是SHA".getBytes());System.out.println(byte2HexStr(hmacSHA224));}publicstaticStringbyte2HexStr(byte[]b){Stringstmp="";StringBuildersb=newStringBuilder("");for(intn=0;n7、+stmp:stmp);sb.append("");}returnsb.toString().toLowerCase().trim();}}执行结果f610c6a17a31eec9d36c5ef646149ea6d40694ae1650fdb9bfe807833a3b6c89e7046e9687398575eadf7eadf5854103b9a98fbedc1333cd553bc288b3518f
7、+stmp:stmp);sb.append("");}returnsb.toString().toLowerCase().trim();}}执行结果f610c6a17a31eec9d36c5ef646149ea6d40694ae1650fdb9bfe807833a3b6c89e7046e9687398575eadf7eadf5854103b9a98fbedc1333cd553bc288b3518f
此文档下载收益归作者所有