在自己的MIS系统使用简单的加密功能加密基本数据

package skydev.modules.encryption;

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

public class Encryption {
  public Encryption() {}

  /**
   * 简单的加密系统
   * @param infoToBeEncrypted 需要加密的数据
   * @return 返回被加密的数据,数据长度20位
   */
  public byte[] getEncryptedData(String infoToBeEncrypted) {
    byte[] digest = null; //new byte[20];
    try {
      //SHA算法返回20位长度,MD5算法返回16位长度
      //系统设计数据库使用20位长度,所以这里使用SHA算法
      MessageDigest md = MessageDigest.getInstance("SHA"); //"SHA-1"
      md.update(infoToBeEncrypted.getBytes("UTF-8"));
      digest = md.digest();
    }
    catch (NoSuchAlgorithmException ex) {
    }
    finally {
      return digest;
    }
  }

  public String byte2hex(byte[] b) { //二进制转字符串
    String hs = "";
    String stmp = "";
    for (int n = 0; n < b.length; n++) {
      stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
      if (stmp.length() == 1) {
        hs = hs + "0" + stmp;
      }
      else {
        hs = hs + stmp;
      }
    }
    return hs.toUpperCase();
  }
}

 

 

package skydev.modules.encryption;

import junit.framework.*;

public class TestEncryption_getEncryptedData
    extends TestCase {
  private Encryption encryption = null;

  protected void setUp() throws Exception {
    super.setUp();
    /**@todo verify the constructors*/
    encryption = new Encryption();
  }

  protected void tearDown() throws Exception {
    encryption = null;
    super.tearDown();
  }

  public void testGetEncryptedData() {
    String infoToBeEncrypted = "b";
    byte[] expectedReturn = null;
    byte[] actualReturn = encryption.getEncryptedData(infoToBeEncrypted);
    assertEquals("return value", expectedReturn, actualReturn);
    /**@todo fill in the test code*/
  }

  public static void main(String[] args) {
    Encryption enc = new Encryption();
    System.out.println(enc.byte2hex(enc.getEncryptedData("abcdef")));
    System.out.println(enc.byte2hex(enc.getEncryptedData("ab")));
    System.out.println(enc.byte2hex(enc.getEncryptedData("ab")));

  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值