【问题标题】:How do generated keys work?生成的密钥如何工作?
【发布时间】:2013-12-27 12:52:42
【问题描述】:

我生成一个密钥并使用密码类中的doFinal() 来加密密码/用户名,现在,当用户想要登录时,他输入 UN 和 PW 然后我告诉他们我需要这样做的过程是什么我将输入与保存加密数据的数据库进行比较?

写这个问题我觉得很愚蠢,但事实是我对此真的很陌生,我的信息可能与右边相距甚远,所以请继续解释并传递你在说什么部分。

现在是我使用的代码:

 public class Safety {
    public static Users encryptUser(Users user){
        Users usera=user;
        try {
            KeyGenerator kg = KeyGenerator.getInstance("AES/CBC/PKCS5Padding");
            Key key=kg.generateKey();
            Cipher cipher=Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            String fNE=new String(cipher.doFinal(user.getFirstname().getBytes()),"UTF-8");
            String lNE=new String(cipher.doFinal(user.getLastname().getBytes()) , "UTF-8");
            String userNameE= new String(cipher.doFinal(user.getUsername().getBytes()),"UTF-8");
            String passWordE= new String(cipher.doFinal(user.getPassword().getBytes()),"UTF-8");
            String eME= new String(cipher.doFinal(user.getEmail().getBytes()),"UTF-8");
            String sQE= new String(cipher.doFinal(user.getsQ().getBytes()),"UTF-8");
            String sAE= new String(cipher.doFinal(user.getsA().getBytes()),"UTF-8");
            Users usere=new Users(fNE, lNE, userNameE, passWordE, eME, sQE, sAE, user.getUserID());
            return usere;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        catch(Exception e){
            e.printStackTrace();                
        }

        return usera;
    }

    public static String decryptuser(Users user){
       //what should I do here exactly? 
    }
}

经过一些研究和工作,这就是我想出的:

         public class Safety {
public static final String algorithm = "PBKDF2WithHmacSHA1";
public static final int saltbytesize = 24;
public static final int hashbytesize = 24;
public static final int iterations = 1000;
public static final int iIndex = 0;
public static final int sIndex = 1;
public static final int pbkIndex = 2;
    public static Users passwordHash(Users user) throws NoSuchAlgorithmException, InvalidKeySpecException{
        SecureRandom sR=new SecureRandom();
        byte[] pws=new byte[saltbytesize];
        sR.nextBytes(pws);
        byte[] pwh=pbkdf2(user.getPassword().toCharArray(),pws,iterations,hashbytesize);
        user.setPassword(toHex(pwh));
        byte[] sas=new byte[saltbytesize];
        sR.nextBytes(sas);
        byte[] sah=pbkdf2(user.getsA().toCharArray(),sas,iterations,hashbytesize);
        user.setsA(toHex(sah));
        user.setUserhash(pws);
        user.setSahash(sas);
        return user;
    }

    public static boolean hashpassword(String username,String password,Users user) throws NoSuchAlgorithmException, InvalidKeySpecException{
        byte[] pws=user.getUserhash();
        byte[] pwh=pbkdf2(password.toCharArray(),pws,iterations,hashbytesize);
        String searcher=toHex(pwh)+username;
        String searched=user.getPassword()+user.getUsername();
        if(searcher.equals(searched)){
            return true;
        }
        return false;
     }
    private static byte[] pbkdf2(char[] password, byte[] salt, int iterations, int bytes)
            throws NoSuchAlgorithmException, InvalidKeySpecException
        {
            PBEKeySpec spec = new PBEKeySpec(password, salt, iterations, bytes * 8);
            SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
            return skf.generateSecret(spec).getEncoded();
        }
    private static String toHex(byte[] array)
    {
        BigInteger bi = new BigInteger(1, array);
        String hex = bi.toString(16);
        int paddingLength = (array.length * 2) - hex.length();
        if(paddingLength > 0)
            return String.format("%0" + paddingLength + "d", 0) + hex;
        else
            return hex;
    }



     }

现在这很好,我想如何让它与 SHA512 一起工作,我该怎么做?

【问题讨论】:

  • @sanket 好吧,但是那个人解密了它,就像他用同样的方法加密它一样,所以他称之为 myKey 的密钥仍然在内存中,这是一种方法......但在我的情况下,这些是两种各自独立工作的方法。有时应用程序可能会关闭并重新启动,那么在这种情况下您的链接将如何帮助我?
  • 感谢您的编辑先生 :)
  • 完全保留我的密钥..怎么样? @sanket
  • 把它放在一个文件里....这里的例子stackoverflow.com/questions/1925104/…

标签: java aes key-generator


【解决方案1】:

你不应该加密密码,你应该用用户名和盐来散列它。

Why should I hash passwords?

【讨论】:

  • 是的。但我认为用户只是学习使用加密和解密是java
  • 好吧,所以我把它们放在同一个字符串中并添加盐?你能放一些代码来告诉我它是如何工作的吗?我真的很感激。
  • @sanket 我实际上正在学习如何将用户安全地保存在数据库中,所以所有建议都很棒。
  • 你的帖子是这个网站:crackstation.net/hashing-security.htm,虽然它不是用于 java 的,但它仍然很有帮助,谢谢你,这正是我一直在寻找的,但是盐哈希是最好的方法吗?
  • 是的。使用哈希意味着您不存储甚至不知道密码,因此如果您的数据库被黑客入侵,黑客将无法提取密码。盐应该是唯一的,例如您的网站名称,以防止字典攻击,其中坏人有一个常用密码及其哈希的数据库。
猜你喜欢
  • 2014-10-22
  • 2022-11-01
  • 2021-12-10
  • 2016-07-17
  • 2020-12-09
  • 1970-01-01
  • 1970-01-01
  • 2021-02-08
  • 2020-03-24
相关资源
最近更新 更多