PHP crypt($ pw,$ salt)Blowfish等效于Android(Java)

时间:2014-10-21 11:57:30

标签: java php android encryption

我使用Blowfish使用随机生成的盐加密密码,该密码也存储在数据库中。

$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发送密码字段中的数据而没有加密时,这实际上是不安全的。

那么有没有办法生成Blowfish PW ON ANDROID?我已经尝试了许多其他教程,比如
Encrypt And Decrypt Using Blowfish In Java
Encrypt Using Blowflish And Java
还发现了这个:PHP crypt(pass, salt) alternative in Java - Blowfish algorithm但是没有解决我的问题

来自第一个链接:

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; }
}

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


那么我做错了什么?!

1 个答案:

答案 0 :(得分:0)

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

  

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

相反

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