sha256java
packageorg.zhang.sha_256;
importjava.security.MessageDigest;
importjava.security.NoSuchAlgorithmException;
publicclassStringEncrypt{
/**
*对字符串加密,加密算法使用MD5,SHA-1,SHA-256,默认使用SHA-256
*
*@paramstrSrc
*要加密的字符串
*@paramencName
*加密类型
*@return
*/
publicstaticStringEncrypt(StringstrSrc,StringencName){
MessageDigestmd=null;
StringstrDes=null;
byte[]bt=strSrc.getBytes();
try{
if(encName==null||encName.equals("")){
encName="SHA-256";
}
md=MessageDigest.getInstance(encName);
md.update(bt);
strDes=bytes2Hex(md.digest());//toHexString
}catch(NoSuchAlgorithmExceptione){
returnnull;
}
returnstrDes;
}
publicstaticStringbytes2Hex(byte[]bts){
Stringdes="";
Stringtmp=null;
for(inti=0;i<bts.length;i++){
tmp=(Integer.toHexString(bts[i]&0xFF));
if(tmp.length()==1){
des+="0";
}
des+=tmp;
}
returndes;
}
publicstaticvoidmain(Stringargs[]){
Strings=StringEncrypt.Encrypt("qq群","227297422");
System.out.println(s);
}
}
⑵ java sha256 怎么解密
网上有解密网站,能对简单的密码解密出来。通过算法是不可能的。现在的办法是建立对应的数据库查询。也可以通过碰撞实验来解密。。但是很慢
⑶ sha265如何使用java代码进行解密
SHA256类似于MD5都是单向加密的,如果要靠算法实现解密,目前理论上是不可能的。
⑷ 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();
}
}
}
=========
php验证
<?php
echo hash('sha256', 'abcd');
验证无误
⑸ .net中SHA256Managed类的ComputeHash算法用java怎么实现
java.security.MessageDigest 调用已经集成的 Hash 算法
⑹ 用Java实现计算文件的SHA256值
请参考
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class BenchmarkSha256 {
public static void main(String[]args) throws NoSuchAlgorithmException {
int size = 1024 * 1024;
byte[] bytes = new byte[size];
MessageDigest md = MessageDigest.getInstance("SHA-256");
long startTime = System.nanoTime();
for (int i = 0; i < 1024; i++)
md.update(bytes, 0, size);
long endTime = System.nanoTime();
System.out.println(String.format("%14x", new java.math.BigInteger(1, md.digest())));
System.out.println(String.format("%d ms", (endTime - startTime) / 1000000));
}
}
换成文件,就是读那么多字节,计算一次。
⑺ java 怎样实现 64位的md5加密算法
直接引入“commons-codec-1.10.jar”这个java包,然后调用相应方法即可
比如我们可以写一个方法类,把常用的方法都写进去:
publicclassEncryptionUtil{
/**
*Base64encode
**/
(Stringdata){
returnBase64.encodeBase64String(data.getBytes());
}
/**
*Base64decode
*@
**/
(Stringdata){
returnnewString(Base64.decodeBase64(data.getBytes()),"utf-8");
}
/**
*md5
**/
publicstaticStringmd5Hex(Stringdata){
returnDigestUtils.md5Hex(data);
}
/**
*sha1
**/
publicstaticStringsha1Hex(Stringdata){
returnDigestUtils.sha1Hex(data);
}
/**
*sha256
**/
publicstaticStringsha256Hex(Stringdata){
returnDigestUtils.sha256Hex(data);
}
}
(PS:纯手打,望采纳)
⑻ 求ECDSA的Java代码
【方案1】
package ECDSA;
import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;
import java.security.*;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class Ecdsa {
private static String src = "hello berber" ;
public static void main(String []args){
jdkECDSA();
}
public static void jdkECDSA(){
// 1.初始化密钥
try{
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");
keyPairGenerator.initialize(256);
KeyPair keyPair = keyPairGenerator.generateKeyPair() ;
ECPublicKey ecPublicKey = (ECPublicKey)keyPair.getPublic() ;
ECPrivateKey ecPrivateKey = (ECPrivateKey)keyPair.getPrivate() ;
// 执行签名
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(ecPrivateKey.getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance("EC") ;
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec) ;
Signature signature = Signature.getInstance("SHA1withECDSA");
signature.initSign(privateKey);
signature.update(src.getBytes());
byte []arr = signature.sign();
System.out.println("jdk ecdsa sign :"+ HexBin.encode(arr));
// 验证签名
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(ecPublicKey.getEncoded());
keyFactory = KeyFactory.getInstance("EC");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
signature = Signature.getInstance("SHA1withECDSA");
signature.initVerify(publicKey);
signature.update(src.getBytes());
boolean bool = signature.verify(arr);
System.out.println("jdk ecdsa verify:"+bool);
}catch(Exception e){
}
}
}
Java数字签名——ECDSA算法
【方案2】
public class MyTest {
/**
* @param args
*/
public static void main(String[] args) {
new MyTest().getSign();
}
void getSign() {
// Get the instance of the Key Generator with "EC" algorithm
try {
KeyPairGenerator g = KeyPairGenerator.getInstance("EC");
ECGenParameterSpec kpgparams = new ECGenParameterSpec("secp256r1");
g.initialize(kpgparams);
KeyPair pair = g.generateKeyPair();
// Instance of signature class with SHA256withECDSA algorithm
Signature ecdsaSign = Signature.getInstance("SHA256withECDSA");
ecdsaSign.initSign(pair.getPrivate());
System.out.println("Private Keys is::" + pair.getPrivate());
System.out.println("Public Keys is::" + pair.getPublic());
String msg = "text ecdsa with sha256";//getSHA256(msg)
ecdsaSign.update((msg + pair.getPrivate().toString())
.getBytes("UTF-8"));
byte[] signature = ecdsaSign.sign();
System.out.println("Signature is::"
+ new BigInteger(1, signature).toString(16));
// Validation
ecdsaSign.initVerify(pair.getPublic());
ecdsaSign.update(signature);
if (ecdsaSign.verify(signature))
System.out.println("valid");
else
System.out.println("invalid!!!!");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}}
java – 使用secp256r1曲线和SHA256算法生
怎么验证生成的Ecdsa签名是正确的呢,可以看下这篇文章:RSA,ECC,Ecdsa,国密SM2的签名,验签,加密