【问题标题】:Part of initial value of key is the samekey的部分初始值相同
【发布时间】:2013-05-23 01:29:53
【问题描述】:

查看代码的时候,看到有人用了关于密钥生成的策略,IV的前半部分始终相同,后半部分根据机器ID不同(其他人可能很难得到)身份证)。然后用于生成加密密钥,如下例:

         public static final String constant = "1234";

         String key = constant + (machine ID);


         SecretKeySpec sks = new SecretKeySpec(key.getBytes(), "DES");

         String result = sks.toString();

它是一种硬编码密码吗?我不确定它是否安全?如果不是,是不是高风险?

非常感谢。

【问题讨论】:

    标签: security passwords hardcoded


    【解决方案1】:

    这是不安全的,因为您使用的是非随机密钥,并且还使用了不安全的加密算法 (DES)。您需要使用像SecureRandom 这样的安全随机生成函数/类,并且需要选择像AESTwoFish 这样的安全算法

    这是来自JavaDigest 的示例,展示了正确使用class SecureRandom

    package random;
    
    import java.security.SecureRandom;
    
    /**
     * A Simple Example to generate secure random numbers using
     * java.security.SecureRandom class.
     * 
     */
    public class SecureRandomGenerator {
      public static void main(String[] args) {
    
        // Get the instance of SecureRandom class with specified PRNG algorithm
        SecureRandom secureRandom = new SecureRandom();
    
        // You can use the getInstance() of the Secure Random class to create an object of SecureRandam
        // where you would need to specify the algorithm name.
        // SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
    
        // Display the algorithm name
        System.out.println("Used algorithm: " + secureRandom.getAlgorithm());
    
        // You also specify the algorithm provider in the getInstance() method
        // SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG", "SUN");
    
        // Display the Provider
        System.out.println("Provider: " + secureRandom.getProvider());
    
        // A call to the setSeed() method will seed the SecureRandom object.
        // If a call is not made to setSeed(),
        // The first call to nextBytes method will force the SecureRandom object to seed itself.
    
        // Get 10 random numbers
        System.out.println("Random Integers generated using SecureRandom");
        for (int i = 0; i < 10; i++) {
          System.out.println(secureRandom.nextInt());
        }
      }
    }
    

    【讨论】:

    • 那么,最好的做法是使用完全随机的IV来生成密钥?
    • @junzi749 是的,您想使用完全随机的 IV。否则可能会损害密码的完整性。
    猜你喜欢
    • 2018-09-03
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 2016-02-22
    • 2012-11-03
    • 1970-01-01
    相关资源
    最近更新 更多