【问题标题】:Android Apache Codecs Base64 ErrorAndroid Apache 编解码器 Base64 错误
【发布时间】:2018-07-04 13:16:38
【问题描述】:

我正在尝试实现一个更高级的密码散列算法 (PBKDF2),它使用 java util 库中的 Base64 类,但由于此类已过时,我需要获取支持更新的 Apache Codecs 库Base64 类。令人惊奇的是,在普通的 java 类上它可以完美地工作,但是当我在 android 活动中使用同一段代码时,它给了我一个错误,说我试图从 Base64 调用的方法不存在!

我认为这里的问题是,在活动中,Base64 是从具有过时版本的 Base64 的 util 库中调用的。 下面是代码示例。

import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.security.SecureRandom;
import org.apache.commons.codec.binary.Base64;

public class Password {
    // The higher the number of iterations the more 
    // expensive computing the hash is for us and
    // also for an attacker.
    private final int iterations = 20 * 1000;
    private final int saltLen = 32;
    private final int desiredKeyLen = 256;

    /**
     * Computes a salted PBKDF2 hash of given plaintext password
     * suitable for storing in a database.
     * Empty passwords are not supported.
     */
    public String getSaltedHash(String password) throws Exception {
        byte[] salt = SecureRandom.getInstance("SHA1PRNG").generateSeed(saltLen);
        // store the salt with the password
        return Base64.encodeBase64String(salt) + "$" + hash(password, salt);
    }

    /**
     * Checks whether given plaintext password corresponds
     * to a stored salted hash of the password.
     */
    public boolean check(String password, String stored) throws Exception {
        String[] saltAndPass = stored.split("\\$");
        if (saltAndPass.length != 2) {
            throw new IllegalStateException(
                    "The stored password have the form 'salt$hash'");
        }
        String hashOfInput = hash(password, Base64.decodeBase64(saltAndPass[0]));
        return hashOfInput.equals(saltAndPass[1]);
    }

    // using PBKDF2 from Sun, an alternative is https://github.com/wg/scrypt
    // cf. http://www.unlimitednovelty.com/2012/03/dont-use-bcrypt.html
    private String hash(String password, byte[] salt) throws Exception {
        if (password == null || password.length() == 0)
            throw new IllegalArgumentException("Empty passwords are not supported.");
        SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        SecretKey key = f.generateSecret(new PBEKeySpec(
                password.toCharArray(), salt, iterations, desiredKeyLen)
        );
        return Base64.encodeBase64String(key.getEncoded());
    }

    public static void main(String[] args) throws Exception {

        Password passwordHash = new Password();
        String password = passwordHash.getSaltedHash("password");
        String password2 = passwordHash.getSaltedHash("password");
        System.out.println("P1-HASH: " + password);
        System.out.println("P2-HASH: " + password2);

        System.out.println(passwordHash.check("password", password2));
    }

}

【问题讨论】:

  • 您的代码中没有任何地方使用 java Base64 类,因此您的描述没有意义。另外,您在这里似乎没有任何问题。
  • Base64 类在此代码中使用了 3 次。问题在于“encodeBase64String”方法,我猜也是解码方法。这个问题可以根据上面的解释来详细说明。
  • java util 类在 java.util 包中。它不会在您的代码中的任何地方使用。

标签: java encryption base64 apache-commons


【解决方案1】:

该方法似乎被称为encodeToString。我真的不知道您在哪里找到了您的特定方法,但没有记录在案;我想你只是指错了类。

【讨论】:

  • Apache 非常糟糕。到目前为止,Java 有它自己的 Base64 类,当然还没有被弃用。不过,我不确定是否支持 Android。
  • 这里的问题在于 encodeToString,在 java.util.Base64 中这个方法根本不存在。但是在“org.apache.commons.codec.binary.BaseNCodec”库中存在,所以理论上我应该能够从Apache库中调用Base64中找到的方法,但实际上我得到了一个方法不存在异常:)
  • 我不知道你在说什么。你列出的方法不存在,我列出的方法确实存在。 “从 Apache 库中调用 Base64 中的方法是什么意思?请告诉我您正在使用现代 IDE 进行开发。
  • 我用的是最新的android studio。这是同一个类Base64(java.util.Base64和apache.commons.codec.Base64)的两个库之间的冲突
  • java.util.Base64 在默认类路径中不是;因此,除非您明确导入它,否则您应该没有任何麻烦。如果你以某种方式需要两者,那么你应该不导入或只导入一个,然后使用完整的类名 包括 包。这样就不可能发生冲突。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-18
  • 1970-01-01
  • 2019-01-18
  • 2015-01-13
  • 2020-10-08
相关资源
最近更新 更多