Chinaunix首页 | 论坛 | 博客
  • 博客访问: 533458
  • 博文数量: 135
  • 博客积分: 3568
  • 博客等级: 中校
  • 技术积分: 1942
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-19 17:52
文章分类

全部博文(135)

文章存档

2012年(29)

2011年(41)

2010年(26)

2009年(12)

2008年(9)

2007年(12)

2006年(6)

分类: 网络与安全

2012-01-31 11:46:13

  1. package me.test.jasypt;

  2. import java.math.BigDecimal;

  3. import org.apache.commons.codec.binary.Base64;
  4. import org.jasypt.util.binary.BasicBinaryEncryptor;
  5. import org.jasypt.util.digest.Digester;
  6. import org.jasypt.util.numeric.BasicDecimalNumberEncryptor;
  7. import org.jasypt.util.password.ConfigurablePasswordEncryptor;
  8. import org.jasypt.util.text.BasicTextEncryptor;

  9. /**
  10.  * Jasypt
  11.  *
  12.  *
  13.  * 在应用 WebLogic、学习 Maven 的时候都碰到过设置密码的情况,且发现他们对同样密码的
  14.  * 2次加密结果还都不一致,但却能验证成功,看过了 Maven 使用的加密的源码。但是作为
  15.  * 可独立使用类库就有点牵强了。于是,就用 google 百度了一下,发现了类似组件,先按照
  16.  * 官网上的例子给写出个 sample 吧。
  17.  *
  18.  * 注意:虽然该组件可以提高加密数据被破解的几率,但是对于系统密码的保护仍需要重视。
  19.  * 系统密码被泄露后,更新系统密码引发的加密数据需要使用原密码解密、再使用新密码加密
  20.  * 也是一个问题(即,加密密码的版本管理问题)。
  21.  *
  22.  * @author btpka3@163.com
  23.  */

  24. /* 附上一份结果吧:
  25. ========= test general digesting
  26. input=[zhang3], output=[5LMF/mIwdfqhesdCUWujzFYtwng=]
  27. input=[zhang3], output=[5LMF/mIwdfqhesdCUWujzFYtwng=]

  28. ========= test password encryption (digesting).
  29. userPassword=[123456], encryptedPassword=[XC4QDvKS72xtYPmTB51Z1/Qy1mVK5+wcSQ2dAg==], inputPassword=[123456], matched=[true]
  30. userPassword=[123456], encryptedPassword=[thOIZMc7odNXhH1vZ93dKfBLwo+5xdnAxN56FA==], inputPassword=[123456], matched=[true]

  31. ========= test text encryption.
  32. password=[123456], text=[zhang3], encryptedText=[UE37aP0KvFWWL9ZkCFBW3g==], plainText=[zhang3]
  33. password=[123456], text=[zhang3], encryptedText=[aWNQza1NdQxjRdy3DJQr8g==], plainText=[zhang3]

  34. ========= test number encryption.
  35. password=[123456], number=[123456789.1229999959468841552734375], encryptedNumber=[-367267622180946584689892873201427575447347.0297720356016226605989864], plainNumber=[123456789.1229999959468841552734375]
  36. password=[123456], number=[123456789.1229999959468841552734375], encryptedNumber=[177858823843249227001381950717244348460500.9116956255239482869022744], plainNumber=[123456789.1229999959468841552734375]

  37. ========= test binary encryption.
  38. password=[123456], binaryStr=[zhang3], encryptedBinaryBase64=[3SA3+eH3mpX+8vnePCLaxw==], plainBinaryStr=[zhang3]
  39. password=[123456], binaryStr=[zhang3], encryptedBinaryBase64=[EOOCLZaVESaG5lYj6E/lBg==], plainBinaryStr=[zhang3]
  40.  */

  41. public class Test {

  42.     public static void main(String[] args) {
  43.         testGeneralDigest();
  44.         testPasswordEncryption();
  45.         testTextEncryption();
  46.         testNumberEncryption();
  47.         testBinaryEncryption();
  48.     }

  49.     /**
  50.      * 进行正常的摘要。称不上为加密,只能用于检测是否数据被改窜。
  51.      */
  52.     public static void testGeneralDigest() {
  53.         System.out.println("========= test general digesting");

  54.         Digester digester = new Digester();
  55.         digester.setAlgorithm("SHA-1");

  56.         // 第一次
  57.         String msg = "zhang3";
  58.         byte[] digest = digester.digest(msg.getBytes());
  59.         String digestBase64 = Base64.encodeBase64String(digest).trim();
  60.         System.out.printf("msg=[%s], digest=[%s]\n", msg, digestBase64);

  61.         // 第二次
  62.         msg = "zhang3";
  63.         digest = digester.digest(msg.getBytes());
  64.         digestBase64 = Base64.encodeBase64String(digest).trim();
  65.         System.out.printf("msg=[%s], digest=[%s]\n", msg, digestBase64);

  66.         System.out.println();
  67.     }

  68.     /**
  69.      * 对密码进行加密。一般都使用单向加密(其实也摘要的一种)。
  70.      */
  71.     public static void testPasswordEncryption() {
  72.         System.out.println("========= test password encryption (digesting).");
  73.         ConfigurablePasswordEncryptor encryptor = new ConfigurablePasswordEncryptor();
  74.         encryptor.setAlgorithm("SHA-1");
  75.         encryptor.setPlainDigest(false);

  76.         // 第一次
  77.         String userPassword = "123456";
  78.         String encryptedPassword = encryptor.encryptPassword(userPassword);
  79.         String inputPassword = userPassword;
  80.         boolean matched = encryptor.checkPassword(inputPassword,
  81.                 encryptedPassword);
  82.         System.out.printf("userPassword=[%s], encryptedPassword=[%s], "
  83.                 + "inputPassword=[%s], matched=[%s]\n", userPassword,
  84.                 encryptedPassword, inputPassword, matched);

  85.         // 第二次
  86.         userPassword = "123456";
  87.         encryptedPassword = encryptor.encryptPassword(userPassword);
  88.         inputPassword = userPassword;
  89.         matched = encryptor.checkPassword(inputPassword, encryptedPassword);
  90.         System.out.printf("userPassword=[%s], encryptedPassword=[%s], "
  91.                 + "inputPassword=[%s], matched=[%s]\n", userPassword,
  92.                 encryptedPassword, inputPassword, matched);

  93.         System.out.println();
  94.     }

  95.     /**
  96.      * 对文本进行加密。可对大量数据进行加密。
  97.      */
  98.     public static void testTextEncryption() {
  99.         System.out.println("========= test text encryption.");

  100.         BasicTextEncryptor encryptor = new BasicTextEncryptor();
  101.         String password = "123456";
  102.         encryptor.setPassword(password);

  103.         // 第一次
  104.         String text = "zhang3";
  105.         String encryptedText = encryptor.encrypt(text);
  106.         String plainText = encryptor.decrypt(encryptedText);
  107.         System.out.printf("password=[%s], text=[%s], encryptedText=[%s], "
  108.                 + "plainText=[%s]\n", password, text, encryptedText, plainText);

  109.         // 第二次
  110.         text = "zhang3";
  111.         encryptedText = encryptor.encrypt(text);
  112.         plainText = encryptor.decrypt(encryptedText);
  113.         System.out.printf("password=[%s], text=[%s], encryptedText=[%s], "
  114.                 + "plainText=[%s]\n", password, text, encryptedText, plainText);

  115.         System.out.println();
  116.     }

  117.     /**
  118.      * 对数值型 BasicDecimal 进行加密。加密前后均为 BasicDecimal 类型。
  119.      */
  120.     public static void testNumberEncryption() {
  121.         System.out.println("========= test number encryption.");

  122.         BasicDecimalNumberEncryptor encryptor = new BasicDecimalNumberEncryptor();

  123.         String password = "123456";
  124.         encryptor.setPassword(password);

  125.         // 第一次
  126.         BigDecimal number = new BigDecimal(123456789.123D);
  127.         BigDecimal encryptedNumber = encryptor.encrypt(number);
  128.         BigDecimal plainNumber = encryptor.decrypt(encryptedNumber);
  129.         System.out.printf("password=[%s], number=[%s], encryptedNumber=[%s], "
  130.                 + "plainNumber=[%s]\n", password, number, encryptedNumber,
  131.                 plainNumber);

  132.         // 第二次
  133.         number = new BigDecimal(123456789.123D);
  134.         encryptedNumber = encryptor.encrypt(number);
  135.         plainNumber = encryptor.decrypt(encryptedNumber);
  136.         System.out.printf("password=[%s], number=[%s], encryptedNumber=[%s], "
  137.                 + "plainNumber=[%s]\n", password, number, encryptedNumber,
  138.                 plainNumber);

  139.         System.out.println();
  140.     }

  141.     /**
  142.      * 对二进制进行加密。
  143.      */
  144.     public static void testBinaryEncryption() {
  145.         System.out.println("========= test binary encryption.");

  146.         BasicBinaryEncryptor encryptor = new BasicBinaryEncryptor();

  147.         String password = "123456";
  148.         encryptor.setPassword(password);

  149.         // 第一次
  150.         String binaryStr = "zhang3";
  151.         byte[] encryptedBinary = encryptor.encrypt(binaryStr.getBytes());
  152.         String encryptedBinaryBase64 = Base64.encodeBase64String(
  153.                 encryptedBinary).trim();
  154.         byte[] plainBinary = encryptor.decrypt(encryptedBinary);
  155.         String plainBinaryStr = new String(plainBinary);
  156.         System.out.printf("password=[%s], binaryStr=[%s], "
  157.                 + "encryptedBinaryBase64=[%s], plainBinaryStr=[%s]\n",
  158.                 password, binaryStr, encryptedBinaryBase64, plainBinaryStr);

  159.         // 第二次
  160.         binaryStr = "zhang3";
  161.         encryptedBinary = encryptor.encrypt(binaryStr.getBytes());
  162.         encryptedBinaryBase64 = Base64.encodeBase64String(encryptedBinary)
  163.                 .trim();
  164.         plainBinary = encryptor.decrypt(encryptedBinary);
  165.         plainBinaryStr = new String(plainBinary);
  166.         System.out.printf("password=[%s], binaryStr=[%s], "
  167.                 + "encryptedBinaryBase64=[%s], plainBinaryStr=[%s]\n",
  168.                 password, binaryStr, encryptedBinaryBase64, plainBinaryStr);

  169.         System.out.println();
  170.     }
  171. }
阅读(1887) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~