1、SHA-256加密算法:
package com.arithmetic.encryption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
//使用java.security.MessageDigest类来进行SHA-256摘要的计算。
//通过getInstance("SHA-256")方法获取SHA-256摘要的实例。
//将要进行摘要计算的密码作为字节数组传递给digest()方法来计算消息摘要。
//将摘要转换为十六进制字符串输出。
//实际应用中,还需要考虑密码的安全存储、密码强度要求等。
public class SHA256ExampleDemo {public static void main(String[] args) {String password = "mypassword";try {// 创建SHA-256摘要MessageDigest md = MessageDigest.getInstance("SHA-256");// 计算消息摘要byte[] hash = md.digest(password.getBytes());// 将摘要转换为十六进制字符串StringBuilder sb = new StringBuilder();for (byte b : hash) {sb.append(String.format("%02x", b));}String hashHex = sb.toString();System.out.println("SHA-256摘要值:" + hashHex);} catch (NoSuchAlgorithmException e) {e.printStackTrace();}}
}
2、加强型SHA-256加密算法:
package com.arithmetic.encryption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
//使用SHA-256算法对密码进行加密,并添加了随机盐值和迭代计算的步骤。
//运行代码,将在控制台打印出经过加密处理的密码。
//实际使用中需要根据具体需求进行适当的修改和补充。
public class SHA256PasswordHashingExampleAdd {public static void main(String[] args) {String password = "1297+mypasswo!rdvdhajifr";byte[] salt = generateSalt();byte[] hashedPassword = hashPassword(password, salt);System.out.println("Hashed Password: " + bytesToHex(hashedPassword));}//迭代计算摘要:将密码进行多次迭代计算摘要,这样可以增加密码破解的时间和计算成本。private static byte[] generateSalt() {SecureRandom random = new SecureRandom();byte[] salt = new byte[16];random.nextBytes(salt);return salt;}private static byte[] hashPassword(String password, byte[] salt) {try {MessageDigest md = MessageDigest.getInstance("SHA-256");byte[] saltedPassword = new byte[password.getBytes().length + salt.length];System.arraycopy(password.getBytes(), 0, saltedPassword, 0, password.getBytes().length);System.arraycopy(salt, 0, saltedPassword, password.getBytes().length, salt.length);md.update(saltedPassword);byte[] hash = md.digest();for (int i = 0; i < 10000; i++) {md.reset();hash = md.digest(hash);}return hash;} catch (NoSuchAlgorithmException e) {e.printStackTrace();}return null;}private static String bytesToHex(byte[] bytes) {StringBuilder hexString = new StringBuilder();for (byte b : bytes) {String hex = Integer.toHexString(0xFF & b);if (hex.length() == 1) {hexString.append('0');}hexString.append(hex);}return hexString.toString();}
}
3、SHA-512加密算法:
package com.arithmetic.encryption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
//定义一个input变量来保存要进行SHA-512哈希的输入,然后调用getSHA512方法来获取SHA-512哈希结果,并打印输出。
//在getSHA512方法中,使用MessageDigest类来获取SHA-512算法的实例,然后调用digest方法计算输入的哈希值。
//最后,将哈希值转换为16进制字符串并返回。如果算法不支持或者发生异常,将返回null。
public class SHA512ExampleDemo {public static void main(String[] args) {String input = "Hello World";String sha512 = getSHA512(input);System.out.println("SHA-512 hash: " + sha512);}private static String getSHA512(String input) {try {MessageDigest md = MessageDigest.getInstance("SHA-512");byte[] messageDigest = md.digest(input.getBytes());StringBuilder sb = new StringBuilder();for (byte b : messageDigest) {sb.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));}return sb.toString();} catch (NoSuchAlgorithmException e) {e.printStackTrace();}return null;}
}
4、加强型SHA-512加密算法:
package com.arithmetic.encryption;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
//在实际应用中,存储密码或敏感数据时应该使用更安全的方法,例如使用安全的哈希函数库或加密算法库,且需要注意密码的传输和存储的安全性。
//请根据实际需求进行适当的修改和改进。//要加强SHA-512算法的安全性,可以采取以下措施://使用随机盐值:在计算SHA-512哈希值之前,先生成一个随机的盐值。
//将盐值与输入字符串进行组合,然后再计算哈希值。这样做可以增加哈希的强度和安全性,防止彩虹表等攻击。
//迭代计算:多次迭代计算哈希值,每次将前一次的哈希值与输入字符串进行组合。
//这样做可以增加计算哈希值的时间和资源成本,防止暴力破解攻击。
//使用加密密钥:使用一个加密密钥对输入字符串进行加密,然后再计算哈希值。
//只有在使用正确的密钥解密后,才能得到正确的输入字符串。这样可以防止对哈希值进行重放攻击。
//使用更长的输出长度:SHA-512算法默认的输出长度是512位,可以使用更长的输出长度来增加哈希值的长度,提高安全性。
//定期更新哈希算法:定期更新SHA-512算法,使用更新版本的算法库或升级到更安全的哈希算法。
//这可以确保使用的哈希算法是最新、最安全的。
//这些措施并不能完全保证SHA-512算法的安全性,它们只能增加攻击者破解哈希值的难度。
//实际应用中,根据具体的安全需求和架构选择合适的加固措施,并且持续关注最新的密码学发展和安全威胁。public class SHA512EnhancedAddDemo {public static void main(String[] args) throws NoSuchAlgorithmException {// 生成随机的盐值SecureRandom random = new SecureRandom();byte[] salt = new byte[16]; // 根据需要设置盐值的长度random.nextBytes(salt);// 将盐值与输入字符串进行组合String input = "yourInputString";byte[] inputBytes = input.getBytes(StandardCharsets.UTF_8);byte[] saltedInputBytes = new byte[salt.length + inputBytes.length];System.arraycopy(salt, 0, saltedInputBytes, 0, salt.length);System.arraycopy(inputBytes, 0, saltedInputBytes, salt.length, inputBytes.length);// 计算SHA-512哈希值MessageDigest digest = MessageDigest.getInstance("SHA-512");byte[] hash = digest.digest(saltedInputBytes);// 打印盐值和哈希值System.out.println("Salt: " + bytesToHex(salt));System.out.println("Hash: " + bytesToHex(hash));// 使用迭代计算int iterations = 10000; // 设置迭代次数byte[] hashedBytes = saltedInputBytes;for (int i = 0; i < iterations; i++) {hashedBytes = digest.digest(hashedBytes);}// 打印迭代计算后的哈希值System.out.println("Iterated Hash: " + bytesToHex(hashedBytes));// 使用加密密钥String keyString = "yourEncryptionKey";byte[] keyBytes = keyString.getBytes(StandardCharsets.UTF_8);byte[] validKeyBytes = Arrays.copyOf(keyBytes, 16); // 将密钥长度调整为16字节SecretKeySpec keySpec = new SecretKeySpec(validKeyBytes, "AES");Cipher cipher;try {cipher = Cipher.getInstance("AES");cipher.init(Cipher.ENCRYPT_MODE, keySpec);byte[] encryptedBytes = cipher.doFinal(inputBytes);// 计算哈希值hash = digest.digest(encryptedBytes);// 打印加密后的哈希值System.out.println("Encrypted Hash: " + bytesToHex(hash));} catch (Exception e) {e.printStackTrace();}}// 将字节数组转换为十六进制表示的字符串private static String bytesToHex(byte[] bytes) {StringBuilder result = new StringBuilder();for (byte b : bytes) {result.append(String.format("%02x", b));}return result.toString();}}