- 用 <bean class=“com.cyou.web.common.EncryptPropertyPlaceholderConfigurer” p:location=“classpath:/config.properties”></bean>
- 代替 <context:property-placeholder location=“classpath:/config.properties” /> public class DESUtils
-
- {
- private static Key key;
- private static String KEY_STR=“mykey”;
- static{
- try
- {
- KeyGenerator generator = KeyGenerator.getInstance(“DES”);
- SecureRandom secureRandom=SecureRandom.getInstance(“SHA1PRNG”);
- secureRandom.setSeed(KEY_STR.getBytes());
- generator.init(secureRandom);
- key = generator.generateKey();
- generator=null;
- }
- catch (Exception e)
- {
- throw new RuntimeException(e);
- }
- }
- /**
- * 对字符串进行加密,返回BASE64的加密字符串
- * <功能详细描述>
- * @param str
- * @return
- * @see [类、类#方法、类#成员]
- */
- public static String getEncryptString(String str){
- BASE64Encoder base64Encoder = new BASE64Encoder();
- System.out.println(key);
- try
- {
- byte[] strBytes = str.getBytes(“UTF-8”);
- Cipher cipher = Cipher.getInstance(“DES”);
- cipher.init(Cipher.ENCRYPT_MODE, key);
- byte[] encryptStrBytes = cipher.doFinal(strBytes);
- return base64Encoder.encode(encryptStrBytes);
- }
- catch (Exception e)
- {
- throw new RuntimeException(e);
- }
- }
- /**
- * 对BASE64加密字符串进行解密
- * <功能详细描述>
- * @param str
- * @return
- * @see [类、类#方法、类#成员]
- */
- public static String getDecryptString(String str){
- BASE64Decoder base64Decoder = new BASE64Decoder();
- try
- {
- byte[] strBytes = base64Decoder.decodeBuffer(str);
- Cipher cipher = Cipher.getInstance(“DES”);
- cipher.init(Cipher.DECRYPT_MODE, key);
- byte[] encryptStrBytes = cipher.doFinal(strBytes);
- return new String(encryptStrBytes,“UTF-8”);
- }
- catch (Exception e)
- {
- throw new RuntimeException(e);
- }
- }
- public static void main(String[] args)
- {
- String name =“root”;
- String password=“1234”;
- String encryname = getEncryptString(name);
- String encrypassword = getEncryptString(password);
- System.out.println(encryname);
- System.out.println(encrypassword);
- System.out.println(getDecryptString(encryname));
- System.out.println(getDecryptString(encrypassword));
- }
- }
转载请注明来源网站:blog.ytso.com谢谢!
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/14612.html