本文目录一览:
java怎么实现对密码用SHA-256加密
import java.security.MessageDigest;
public class Test{
public static void main(String[] args) {
String t= "abcd";
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(t.getBytes("GBK"));
for(byte b:md.digest())
System.out.format("%02X",b);
} catch (Exception e) {
e.printStackTrace();
}
}
}
=========
88D4266FD4E6338D13B845FCF289579D209C897823B9217DA3E161936F031589
PHP验证
?php
echo hash('sha256', 'abcd');
88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589
验证无误
php 如何实现 java的sha1加密
function
encryptTokey($data){
$apikey
=
'testapikey111';
$ps1
=
sha1($apikey
.
strtolower($data));
$ps1
=
strtoupper($ps1);
$s1
=
implode(str_split($ps1,
2),
'-');
$ps2
=
md5($s1
.
$apikey);
$ps2
=
strtoupper($ps2);
$token
=
implode(str_split($ps2,
2),
'-');
return
$token;
}
echo
encryptTokey('testdata');
运行结果:
68-10-98-74-4C-82-74-4B-CC-49-31-98-46-02-EE-8E
详细你可以去后盾人看看,这些都是后盾人里面的,哪里有详细的视频教学都是高质量,我自己就是在里面学的。
如何使用java进行sha1加密
使用下面的语句即可:
digestutils.shahex(要加密的字符);加密参数最好用字节数组,毕竟sha1算法是使用字节为单位进行运算的,字符串转字节还与字符编码有关。
java加密的几种方式
基本的单向加密算法:
BASE64 严格地说,属于编码格式,而非加密算法
MD5(Message Digest algorithm 5,信息摘要算法)
SHA(Secure Hash Algorithm,安全散列算法)
HMAC(Hash Message Authentication Code,散列消息鉴别码)
复杂的对称加密(DES、PBE)、非对称加密算法:
DES(Data Encryption Standard,数据加密算法)
PBE(Password-based encryption,基于密码验证)
RSA(算法的名字以发明者的名字命名:Ron Rivest, AdiShamir 和Leonard Adleman)
DH(Diffie-Hellman算法,密钥一致协议)
DSA(Digital Signature Algorithm,数字签名)
ECC(Elliptic Curves Cryptography,椭圆曲线密码编码学)
代码参考:
/**
* BASE64加密
*
* @param key
* @return
* @throws Exception
*/
public static String encryptBASE64(byte[] key) throws Exception {
return (new BASE64Encoder()).encodeBuffer(key);
}
/**
* MD5加密
*
* @param data
* @return
* @throws Exception
*/
public static byte[] encryptMD5(byte[] data) throws Exception {
MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
md5.update(data);
return md5.digest();
}
/**
* SHA加密
*
* @param data
* @return
* @throws Exception
*/
public static byte[] encryptSHA(byte[] data) throws Exception {
MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
sha.update(data);
return sha.digest();
}
}
/**
* 初始化HMAC密钥
*
* @return
* @throws Exception
*/
public static String initMacKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);
SecretKey secretKey = keyGenerator.generateKey();
return encryptBASE64(secretKey.getEncoded());
}
/**
* HMAC加密
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] encryptHMAC(byte[] data, String key) throws Exception {
SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
return mac.doFinal(data);
}