MD5算法加密

package com.nml.roims.common;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * <p>Title: </p>
 * <p>Description: A password encryption helper.</p>
 *
 *  A cryptographically secure message digest takes arbitrary-sized input
 *  (a byte array), and generates a fixed-size output, called a digest or hash.
 *  A digest has two properties:
 *  - It should be computationally infeasible to find two messages that hashed
 *    to the same value.
 *  - The digest should not reveal anything about the input that was used to
 *    generate it.
 *  provided by "JavaTM Cryptography Architecture API Specification & Reference"
 * <p>Copyright:      Copyright (c) 2007</p>
 * <p>Company:        </p>
 * @author            nick  yu
 * @version           $Id: PasswordEncryptionHelper.java,v 1.2 2007/06/08 06:24:56 his_jack Exp $
 */

public class PasswordEncryptionHelper {
  public PasswordEncryptionHelper() {
  }

  public static String createHashCode(char[] passwordArr) throws
      NoSuchProviderException, NoSuchAlgorithmException {
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < passwordArr.length; i++) {
      buf.append(passwordArr[i]);
      passwordArr[i] = 0;
    }
    String encryptedPassword = createHashCode(buf.toString());
    for (int i = 0; i < passwordArr.length; i++) {
      buf.setCharAt(i, ' ');
    }
    return encryptedPassword;
  }

  public static String createHashCode(String password) throws
      NoSuchAlgorithmException, NoSuchProviderException {
    if (password != null) {
      if (!password.equals("")) {
        byte[] input = password.getBytes();
        byte[] digestedInput;
        StringBuffer buf = new StringBuffer();
        MessageDigest md = MessageDigest.getInstance("MD5", "SUN");
        digestedInput = md.digest(input);
        for (int i = 0; i < digestedInput.length; i++) {
          buf.append(Integer.toHexString(0x0100 + (digestedInput[i] & 0x00FF)).
                     substring(1));
//        buf.append(Integer.toHexString(digestedInput[i]));
        }
        return buf.toString();
      }
      else {
        // to work around the database constraint (NOT NULL)
        // there at least need to have a space.
        return " ";
      }
    }
    else { // password is null
      throw new NullPointerException("Password cannot be NULL.");
    }
  }

  public static void main(String[] args) {
    try {
      String text = args[0];
      String digest = createHashCode(text);
      System.out.println("Original Text: /"" + text + "/"");
      System.out.println("Digested Text: /"" + digest + "/"");
    }
    catch (Exception e) {
      e.printStackTrace();
      System.out.println("Usage: PasswordEncryptionHelper plain_text");
    }
  }

}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值