start
1.SHA-256算法(单向、验证完整性/一致性,暂时安全)
1 import java.nio.charset.StandardCharsets; 2 import java.security.MessageDigest; 3 import java.security.NoSuchAlgorithmException; 4 5 public class SHA256Example { 6 7 public static String hashWithSHA256(String text) { 8 9 try { 10 MessageDigest digest = MessageDigest.getInstance("SHA-256"); 11 byte[] hash = digest.digest(text.getBytes(StandardCharsets.UTF_8)); 12 return bytesToHexString(hash); 13 } catch (NoSuchAlgorithmException e) { 14 throw new RuntimeException("Error: Unable to find SHA-256 algorithm.", e); 15 } 16 17 } 18 19 public static String bytesToHexString(byte[] bytes) { 20 StringBuilder hexString = new StringBuilder(); 21 for (byte b : bytes) { 22 String hex = Integer.toHexString(0xff & b); 23 if (hex.length() == 1) { 24 hexString.append("0"); 25 } 26 hexString.append(hex); 27 } 28 return hexString.toString(); 29 } 30 31 public static void main(String[] args) { 32 String text = "Hello,World!"; 33 String sha256hash = hashWithSHA256(text); 34 System.out.println("Original:" + text); 35 System.out.println("SHA-256:" + sha256hash); 36 } 37 38 }
2.MD5算法(单向、验证完整性、一致性,有安全漏洞)
import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MD5Example { public static String getMD5Hash(String text) { try { MessageDigest mDigest = MessageDigest.getInstance("MD5"); byte[] hashBytes = mDigest.digest(text.getBytes(StandardCharsets.UTF_8)); StringBuilder sBuilder = new StringBuilder(); for (byte b : hashBytes) { sBuilder.append(String.format("%02x", b)); } return sBuilder.toString(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("MD5 algorithm not found", e); } } public static void main(String[] args) { String text = "Hello,world!"; String md5Hash = getMD5Hash(text); System.out.println("Original:" + text); System.out.println("MD5:" + md5Hash); } }
3.SHA3-256算法()
import org.bouncycastle.jcajce.provider.digest.SHA3; import org.bouncycastle.util.encoders.Hex; import java.nio.charset.StandardCharsets; public class SHA3_256Example { public static void main(String[] args) { String input = "Hello, World!"; String sha3_256Hash = getSHA3_256Hash(input); System.out.println("SHA3-256 Hash: " + sha3_256Hash); } public static String getSHA3_256Hash(String input) { // 创建一个SHA3.Digest256实例 SHA3.Digest256 digest = new SHA3.Digest256(); // 对输入的字符串进行散列 byte[] hashBytes = digest.digest(input.getBytes(StandardCharsets.UTF_8)); // 将散列后的字节数组转换为十六进制字符串 return new String(Hex.encode(hashBytes)); } }
Java8以前需要第三方库支持。
<dependencies> <!-- Other dependencies --> <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.70</version> </dependency> </dependencies>
Java9开始,JDK中已经支持SHA3-256的算法。无需第三方。
3.AES算法(对称加密算法)
import java.nio.charset.StandardCharsets; import java.util.Base64; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; public class AESExample { public AESExample() { String input = "Hello,World!"; try { KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(256); SecretKey secretKey = keyGenerator.generateKey(); // 加密 Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] encryptedData = cipher.doFinal(input.getBytes(StandardCharsets.UTF_8)); String encryptedDataString = Base64.getEncoder().encodeToString(encryptedData); System.out.println("Encrypted data:" + encryptedDataString); // 解密 cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] decryptedData = cipher.doFinal(encryptedData); String decryptedDataString = new String(decryptedData, StandardCharsets.UTF_8); System.out.println("Decrypted data:" + decryptedDataString); } catch (Exception e) { throw new RuntimeException("Error:", e); } } public static void main(String[] args) { new AESExample(); } }
4.RSA算法(非对称加密算法)
import java.nio.charset.StandardCharsets; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; import java.util.Base64; import javax.crypto.Cipher; public class RSAExample { public static void main(String[] args) { String input = "Hello,world!"; try { // 生成SA密钥 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(2048); KeyPair keyPair = keyPairGenerator.generateKeyPair(); // 公钥 PublicKey publicKey = keyPair.getPublic(); System.out.println("公钥:"+publicKey.toString()); // 私钥 PrivateKey privateKey = keyPair.getPrivate(); System.out.println("私钥:"+privateKey.toString()); // 使用公钥加密数据 Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] encryptedData = cipher.doFinal(input.getBytes(StandardCharsets.UTF_8)); String encryptedDataString = Base64.getEncoder().encodeToString(encryptedData); System.out.println("Encryted Data:" + encryptedDataString); // 使用私钥解密数据 cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] decrytedData = cipher.doFinal(encryptedData); String decrytedDataString = new String(decrytedData, StandardCharsets.UTF_8); System.out.println("Decryted Data:" + decrytedDataString); } catch (Exception e) { throw new RuntimeException("Error:", e); } } }
end
标签:java,String,public,算法,new,StandardCharsets,import,JAVA From: https://www.cnblogs.com/lnsylt/p/17746649.html