Does anybody know what encrypting technique is JDeveloper/SQL Developer using to persist credentials?

后端 未结 11 2107
广开言路
广开言路 2020-12-22 18:46

I\'d be more than interesting for me to understand which technique is being used here to persist sensible data since I\'m needing to implement a similar solution. Here\'s a

11条回答
  •  一生所求
    2020-12-22 19:31

    For the curious, what you're actually seeing is the secret key concatenated with the encrypted password. For example, I tried encrypting the password "SAILBOAT" using:

    DatabaseProviderHelper.goingOut("SAILBOAT")
    

    In this particular instance, the result was:

    0527C290B40C41D71139B5E7A4446E94D7678359087249A463
    

    The first byte is constant:

    05
    

    The next 8 bytes represent the randomly generated secret key (for the DES cipher):

    27C290B40C41D711
    

    The remaining bytes are the encrypted password:

    39B5E7A4446E94D7678359087249A463
    

    Therefore, to decrypt the password, you simply use this:

    public static byte[] decryptPassword(byte[] result) throws GeneralSecurityException {
        byte constant = result[0];
        if (constant != 5) {
            throw new IllegalArgumentException();
        }
    
        byte[] secretKey = new byte[8];
        System.arraycopy(result, 1, secretKey, 0, 8);
    
        byte[] encryptedPassword = new byte[result.length - 9];
        System.arraycopy(result, 9, encryptedPassword, 0, encryptedPassword.length);
    
        byte[] iv = new byte[8];
        for (int i = 0; i < iv.length; i++) {
            iv[i] = 0;
        }
    
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(secretKey, "DES"), new IvParameterSpec(iv));
        return cipher.doFinal(encryptedPassword);
    }
    

提交回复
热议问题