用Java编写的密码算法类

package data;

import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;

/**
 * Security 提供了一个安全算法类,其中包括对称密码算法和散列算法
 */
public final class Security
{
 /**
  * 对称加密方法
  * @param byteSource 需要加密的数据
  * @return 经过加密的数据
  * @throws Exception
  */
 public static byte[] symmetricEncrypto(byte[] byteSource) throws Exception
 {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  try
  {
   int mode = Cipher.ENCRYPT_MODE;
   SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
   byte[] keyData = {1, 9, 8, 2, 0, 8, 2, 1};
   DESKeySpec keySpec = new DESKeySpec(keyData);
   Key key = keyFactory.generateSecret(keySpec);
   Cipher cipher = Cipher.getInstance("DES");
   cipher.init(mode, key);
   int blockSize = cipher.getBlockSize();
   int position = 0;
   int length = byteSource.length;
   boolean more = true;
   while(more)
   {
    if(position + blockSize <= length)
    {
     baos.write(cipher.update(byteSource, position, blockSize));
     position += blockSize;
    }
    else
    {
     more = false;
    }
   }
   if(position < length)
   {
    baos.write(cipher.doFinal(byteSource, position, length - position));
   }
   else
   {
    baos.write(cipher.doFinal());
   }
   return baos.toByteArray();
  }
  catch(Exception e)
  {
   throw e;
  }
  finally
  {
   baos.close();
  }
 }
 /**
  * 对称解密方法
  * @param byteSource 需要解密的数据
  * @return 经过解密的数据
  * @throws Exception
  */
 public static byte[] symmetricDecrypto(byte[] byteSource) throws Exception
 {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  try
  {
   int mode = Cipher.DECRYPT_MODE;
   SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
   byte[] keyData = {1, 9, 8, 2, 0, 8, 2, 1};
   DESKeySpec keySpec = new DESKeySpec(keyData);
   Key key = keyFactory.generateSecret(keySpec);
   Cipher cipher = Cipher.getInstance("DES");
   cipher.init(mode, key);
   int blockSize = cipher.getBlockSize();
   int position = 0;
   int length = byteSource.length;
   boolean more = true;
   while(more)
   {
    if(position + blockSize <= length)
    {
     baos.write(cipher.update(byteSource, position, blockSize));
     position += blockSize;
    }
    else
    {
     more = false;
    }
   }
   if(position < length)
   {
    baos.write(cipher.doFinal(byteSource, position, length - position));
   }
   else
   {
    baos.write(cipher.doFinal());
   }
   return baos.toByteArray();
  }
  catch(Exception e)
  {
   throw e;
  }
  finally
  {
   baos.close();
  }
 }
 /**
  * 散列算法
  * @param byteSource 需要散列计算的数据
  * @return 经过散列计算的数据
  * @throws Exception
  */
 public static byte[] hashMethod(byte[] byteSource) throws Exception
 {
  try
  {
   MessageDigest currentAlgorithm = MessageDigest.getInstance("SHA-1");
   currentAlgorithm.reset();
   currentAlgorithm.update(byteSource);
   return currentAlgorithm.digest();
  }
  catch(Exception e)
  {
   throw e;
  }
 }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SHA (Secure Hash Algorithm) 是一种常用的密码散列函数,可以将任意长度的二进制数据映射为固定长度的较小二进制数据。以下是用 Java 编写 SHA 算法的示例代码: ```java import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; public class SHAExample { public static void main(String[] args) throws NoSuchAlgorithmException { String passwordToHash = "password123"; byte[] salt = getSalt(); String securePassword = getSHA512SecurePassword(passwordToHash, salt); System.out.println("Secure password: " + securePassword); } private static String getSHA512SecurePassword(String passwordToHash, byte[] salt) throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance("SHA-512"); md.update(salt); byte[] bytes = md.digest(passwordToHash.getBytes()); StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1)); } return sb.toString(); } private static byte[] getSalt() throws NoSuchAlgorithmException { SecureRandom sr = SecureRandom.getInstanceStrong(); byte[] salt = new byte[16]; sr.nextBytes(salt); return salt; } } ``` 在上面的代码中,我们使用了 SHA-512 算法来生成密码的散列值。首先我们调用 `getSalt()` 方法获取一个随机的盐值,然后将密码和盐值传入 `getSHA512SecurePassword()` 方法来生成散列值。最后,我们将生成的散列值输出到控制台。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值