需求是会员从C#迁移到了JAVA,但是旧的密码是BASE64(HMACSHA512(value))加密的,用户体验不变,所以java端也要实现这个,可我生成的这么都不对。c#:PBKDF2C#生成的类似:zYKcuNTKMZzvmCptYkXbbfCXkwUiQKf/6qZi/XRNNxSf08pMRcrBO34cj9DMyoQCFOLnbQgDz9aalz8yjbQwng==而我生成的:YjA4OTc2ZDJkNmNhYTk4OGJmODI0M2M1OWExZTJjYjMwMmMzNzFmYzA0ZTA2ZTE0ZjkyNGQyNWNiNGI2MTA1ZDc1YTUzNTJjZjI1YjY4ODlhY2JhMTk2M2MwNzcyODE4MjQzZmY3YzczZjMzY2I2OWEyMGIyM2ZkYmNiOTAwOGY=哪有问题?下面是代码importjavax.crypto.spec.SecretKeySpec;importjavax.crypto.Mac;importjava.util.Base64;publicclassHMAC{/***定义加密方式*MAC算法可选以下多种算法**HmacMD5*HmacSHA1*HmacSHA256*HmacSHA384*HmacSHA512**/privatestaticfinalStringHMAC_SHA1="HmacSHA512";/***生成签名数据_HmacSHA1加密**@paramdata*待加密的数据*@paramkey*加密使用的key*/publicstaticStringgetSignature(Stringdata,Stringkey)throwsException{byte[]keyBytes=key.getBytes();//根据给定的字节数组构造一个密钥。SecretKeySpecsigningKey=newSecretKeySpec(keyBytes,HMAC_SHA1);Macmac=Mac.getInstance(HMAC_SHA1);mac.init(signingKey);byte[]rawHmac=mac.doFinal(data.getBytes());StringhexBytes=byte2hex(rawHmac);returnhexBytes;}privatestaticStringbyte2hex(finalbyte[]b){Stringhs="";Stringstmp="";for(intn=0;n//以十六进制(基数16)无符号整数形式返回一个整数参数的字符串表示形式。 stmp=(java.lang.Integer.toHexString(b[n]&0xFF));if(stmp.length()==1){hs=hs+"0"+stmp;}else{hs=hs+stmp;}}returnhs;}/***@paramargs*/publicstaticvoidmain(String[]args){try{Stringsignature=getSignature("3e043a92a204412cb7098b67c017b1cf","ants@xpress");Stringbase64String=Base64.getEncoder().encodeToString(signature.getBytes("UTF-8"));System.out.println(base64String);}catch(Exceptione){e.printStackTrace();}}}
慕妹3242003
杨魅力
相关分类