Can a secret be hidden in a 'safe' java class offering access credentials?

后端 未结 12 1747
北荒
北荒 2020-12-03 09:04

This is a brainstorming question about what\'s possible in Java (or not). I want to know if it is possible to hide a secret within a class and prevent anymore from accessing

12条回答
  •  广开言路
    2020-12-03 09:29

    Assuming information passed to method calls are safe, a key is a good solution. The key doesn't need to be stored anywhere in the app, and because of this, the information can't be accessed through Java only. It gets interesting if you want a way to share the secret with others without giving them your key, which is what the shareSecret method is for below. However, it becomes tricky managing this. One process could be:

    1) The secret seeker requests access, entering a temp key that is stored

    2) The secret keeper grants access with their key, the temp key is deleted, and a temp Safe object is created that works for the temp key.

    3) The secret seeker enters the temp key and a permanent key, the temp Safe object is deleted, and a new permanent Safe object is created that can be accessed with the permanent key.

    Again, assuming parameters passed to method calls are safe, the main problem with the above procedure is that someone could have hijacked the temp key between 1 and 2 and use it to view the temp secret between steps 2 and 3. However, it would make it tougher to crack than storing it in a plain-text string.

    public final class Safe {
        private String secret;
    
        public Safe(String secret, String key){
            this.secret = encode(secret, key}
    
        public String getSecret(String key){
            return decode(this.secret, credentials);
        }
    
        public Safe shareSecret(String fromKey, String toKey){
            return new Safe(decode(this.secret, fromKey), toKey); 
        }
    
        private String encode(String secret, String key){
           //Code to encode the secret based on key here...
        }
    
        private String decode(String secret, String key){
           //Code to decode the secret based on key here...
        }
    }
    

提交回复
热议问题