當前位置:首頁 » 編程語言 » sha256java

sha256java

發布時間: 2023-02-18 01:14:36

⑴ 請問有sha256加密java代碼嗎

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

熱點內容
電腦配置都有哪些問題 發布:2024-11-08 11:15:29 瀏覽:727
新浪微博敏感詞資料庫 發布:2024-11-08 11:03:22 瀏覽:472
linux的終端軟體 發布:2024-11-08 11:01:46 瀏覽:204
主機如何把密碼關掉 發布:2024-11-08 10:36:25 瀏覽:720
安卓軟體如何鎖定 發布:2024-11-08 10:30:27 瀏覽:709
sql定時執行語句 發布:2024-11-08 10:29:36 瀏覽:673
邁銳寶xl值得入手哪個配置 發布:2024-11-08 10:14:13 瀏覽:634
尋歡加密 發布:2024-11-08 10:02:57 瀏覽:353
拼單源碼 發布:2024-11-08 09:58:24 瀏覽:143
linux伺服器存儲 發布:2024-11-08 09:51:40 瀏覽:473