Unicenta密码加密

时间:2013-09-11 11:35:41

标签: java android encryption hash passwords

我正在尝试开发新版本的Unicenta opos _它是一个Openbravo开源项目_在Android平台上工作,我几乎完成了,但我还是有一些密码部分的问题。

我的问题是: Unicenta opos使用Sha1加密将密码保存在mysql数据库中,所以当我尝试在我的Android应用程序中执行相同操作时,它不会给我相同的加密密码,然后我做了什么;我找了Unicenta代码源,我拿起了加密代码,我把它放在我的应用程序中;

来自unicenta代码源的加密功能:

public static String hashString(String sPassword) {

      if (sPassword == null || sPassword.equals("")) {
          return "empty:";
      } else {
          try {
              MessageDigest md = MessageDigest.getInstance("SHA-1");
              md.update(sPassword.getBytes("UTF-8"));
              byte[] res = md.digest();
              return "sha1:" + StringUtils.byte2hex(res);
          } catch (NoSuchAlgorithmException e) {
              return "plain:" + sPassword;
          } catch (UnsupportedEncodingException e) {
              return "plain:" + sPassword;
          }
      }
    }

所以它很清楚,这个函数将String作为参数(密码)并返回(密码加密)

但问题是当我尝试使用unicenta opos加密密码( 0000 )时,它会让我:( sha1:B6589FC6AB0DC82CF12099D1C2D40AB994E8410C)并使用我的Android应用程序使用unicenta使用的相同方法,它给了我另一个哈希( sha1:39dfa55283318d31afe5a3ff4a0e3253e2045e43

抱歉很久,我希望如果有人有兴趣!

谢谢你

2 个答案:

答案 0 :(得分:1)

Unicenta不直接加密/哈希密码。查看byte2hex和JEditorText.java,了解它在加密之前如何操作输入密码。所以9802变成了' Wt a '没有引用然后散列。

第一个哈希函数:

MessageDigest md = MessageDigest.getInstance("SHA-1");                   
                    md.update(sPassword.getBytes("UTF-8"));
                    byte[] res = md.digest();                    
                    return "sha1:" + StringUtils.byte2hex(res);

<强> byte2hex

<强> StringUtils.java

public static String byte2hex(byte[] binput) {

        StringBuilder sb = new StringBuilder(binput.length * 2);
        for (int i = 0; i < binput.length; i++) {
            int high = ((binput[i] & 0xF0) >> 4);
            int low = (binput[i] & 0x0F);
            sb.append(hexchars[high]);
            sb.append(hexchars[low]);
        }
//sb : 56E048B73E9B3EFF42C509B772AF6941FF0B5BA5
        return sb.toString();
    }  

最后看看JEditorText.java:

    private static final char[] CHAR_abc1_1 = {'.', '?', '!', ',', '1', ';', ':'};
    private static final char[] CHAR_abc1_2 = {'a', 'b', 'c', '2', '\u00a0'};
    private static final char[] CHAR_abc1_3 = {'d', 'e', 'f', '3', '\u201a'};
    private static final char[] CHAR_abc1_4 = {'g', 'h', 'i', '4', '\u00a1'};
    private static final char[] CHAR_abc1_5 = {'j', 'k', 'l', '5'};
    private static final char[] CHAR_abc1_6 = {'m', 'n', 'o', '6', '\u00a4', '\u00a2'};
    private static final char[] CHAR_abc1_7 = {'p', 'q', 'r', 's', '7'};
    private static final char[] CHAR_abc1_8 = {'t', 'u', 'v', '8', '\u00a3', '\ufffd'};
    private static final char[] CHAR_abc1_9 = {'w', 'x', 'y', 'z', '9'};
    private static final char[] CHAR_abc1_0 = {' ', '0'};

    private static final char[] CHAR_ABC1_1 = {'.', '?', '!', ',', '1', ';', ':'};
    private static final char[] CHAR_ABC1_2 = {'A', 'B', 'C', '2', '\u00b5'};
    private static final char[] CHAR_ABC1_3 = {'D', 'E', 'F', '3', '\u00c9'};
    private static final char[] CHAR_ABC1_4 = {'G', 'H', 'I', '4', '\u00cd'};
    private static final char[] CHAR_ABC1_5 = {'J', 'K', 'L', '5'};
    private static final char[] CHAR_ABC1_6 = {'M', 'N', 'O', '6', '\u00d1', '\u00d3'};
    private static final char[] CHAR_ABC1_7 = {'P', 'Q', 'R', 'S', '7'};
    private static final char[] CHAR_ABC1_8 = {'T', 'U', 'V', '8', '\u00da', '\u00dc'};
    private static final char[] CHAR_ABC1_9 = {'W', 'X', 'Y', 'Z', '9'};
    private static final char[] CHAR_ABC1_0 = {' ', '0'};

答案 1 :(得分:0)

试试这可能会对你有所帮助

private String hashString(String s) {
        try {
            // Create SHA-1 Hash
            MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1");
            digest.update(s.getBytes("UTF-8"));
            byte messageDigest[] = digest.digest();

            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < messageDigest.length; i++)
                hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }