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的簽名,驗簽,加密