50b2be1b575ad309509ea2f5af7bb1ba9b118a9b.svn-base 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package eVVM.apk.helper;
  2. public class Encrypt {
  3. //密钥 加解密的密钥必须相同 可自定义
  4. private static final String secretKey ="6w3lacP9gmjbMgor";
  5. /**
  6. * 字符串加密
  7. * @param plainText 要加密的字符串
  8. * @return 加密后的字符串
  9. */
  10. public static String encrypt(String plainText){
  11. String encryption = "";
  12. try {
  13. plainText = new String(plainText.getBytes("UTF-8"),"iso-8859-1");
  14. } catch (Exception e) {
  15. }
  16. char[] cipher=new char[plainText.length()];
  17. for(int i=0,j=0;i<plainText.length();i++,j++){
  18. if(j==secretKey.length())
  19. j=0;
  20. cipher[i]=(char) (plainText.charAt(i)^secretKey.charAt(j));
  21. String strCipher= Integer.toHexString(cipher[i]);
  22. if(strCipher.length() == 1){
  23. encryption+="0"+strCipher;
  24. }else{
  25. encryption+=strCipher;
  26. }
  27. }
  28. return encryption;
  29. }
  30. /**
  31. * 解密字符串
  32. * @param encryption 要解密的字符串
  33. * @return 解密后的字符串
  34. */
  35. public static String decrypt(String encryption) {
  36. char[] decryption=new char[encryption.length()/2];
  37. for(int i=0,j=0;i<encryption.length()/2;i++,j++){
  38. if(j==secretKey.length())
  39. j=0;
  40. char n=(char)(int) Integer.valueOf(encryption.substring(i*2,i*2+2),16);
  41. decryption[i]=(char)(n^secretKey.charAt(j));
  42. }
  43. String decoding="";
  44. try {
  45. decoding = new String(String.valueOf(decryption).getBytes("iso-8859-1"),"UTF-8");
  46. } catch (Exception e) {
  47. }
  48. return decoding;
  49. }
  50. /**
  51. * @param args
  52. */
  53. public static void main(String[] args) {
  54. String name="13621372753";
  55. String tem=Encrypt.encrypt(name);
  56. System.out.println(tem);
  57. System.out.println(Encrypt.decrypt(tem));
  58. }
  59. }