PHP crypt($pw,$salt) Blowfish equivalent for Android (Java)


PHP crypt($pw,$salt) Blowfish equivalent for Android (Java)

我使用Blowfish用随机生成的salt加密密码,该salt也存储在DB中。

$password = "someString";
$salt= "$2a$07$0dade6ad90a6cc2639b236876538b5fe$" // "$2a$07$". md5(mt_rand()) . "$";
$pw = crypt($password,$salt); // $2a -> Blowfish
// $pw would be $2a$07$0dade6ad90a6cc2639b23uwYL2QMyqG3piDr3N/D0oGvdD4NF7CIy

所以,我的实际问题是:我正在编写一个需要登录的android应用程序。因此,当我在没有加密的情况下通过POST发送密码字段中的数据时,这真的很不安全。

那么有没有办法在安德身上生成河豚PW?我尝试过许多其他教程,如
在Java中使用Blowfish加密和解密
使用Blowfish和Java加密
还发现:Java中的PHP密码(pass,salt)替代方案-Blowfish算法,但没有解决我的问题


这来自第一个链接:

public static String encryptBlowfish(String to_encrypt, String strkey) {
    try {
        SecretKeySpec key = new SecretKeySpec(strkey.getBytes(), "Blowfish");
        Cipher cipher = Cipher.getInstance("Blowfish");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return new String(cipher.doFinal(to_encrypt.getBytes()).toString()); // Added here .toString() because otherwise I get some hardcoded text
    } catch (Exception e) { return null; }
}

但当我用salt加密密码时,我只得到一个长度为11的字符串…
所以我调用encryptBlowfish("someString","0dade6ad90a6cc2639b236876538b5fe");,返回的String是[B@42ab9778


那么我做错了什么

要获得正确的加密密码,请不要使用返回新的

return new String(cipher.doFinal(to_encrypt.getBytes()).toString());

取而代之的是

Base64.encodeToString(Data,Base64.DEFAULT); //Data is the result cointainer of cipher.doFinal.