【问题标题】:PHP crypt($pw,$salt) Blowfish equivalent for Android (Java)PHP crypt($pw,$salt) 适用于 Android (Java) 的 Blowfish
【发布时间】:2014-12-16 15:27:00
【问题描述】:

我使用 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 从密码字段发送数据时,这真的很不安全。

那么有没有办法在 ANDROID 上生成 Blowfish PW?我尝试了许多其他教程,例如
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


那我做错了什么?!

【问题讨论】:

  • 字节数组不是字符串,Java 不是 php。
  • 那么如何解决我的问题呢?

标签: java php android encryption


【解决方案1】:

要获得正确的加密密码,请不要使用 return new

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

相反

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

【讨论】:

    猜你喜欢
    • 2014-06-27
    • 2018-09-05
    • 1970-01-01
    • 2015-11-14
    • 2011-01-12
    • 1970-01-01
    • 2013-05-05
    • 2014-10-06
    • 2014-09-18
    相关资源
    最近更新 更多