1

I was passing sensitive information (between my app and backend) like password in String format. Later on I realized that password can be easily read from heap memory dump as it is stored in text format.

String password = "super_password";

So I decided to use Char array,

Char[] passChar = password.toCharArray();

but I am worried that password can still be read from memory dump character by character. Then I thought of using byte array instead.

byte[] passByte = password.getBytes();

My question: Is it safe to use byte array for passing sensitive information like password ? OR can anyone recommend anything secure ?

Etheryte
  • 24,589
  • 11
  • 71
  • 116
Script_Junkie
  • 277
  • 2
  • 6
  • 17
  • 1
    If it could still be read char by char, what's to stop the person doing that from reading it byte by byte and converting it back manually? – Pokechu22 Dec 04 '14 at 01:00
  • @Pokechu22 I believe he is referencing [this](http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords). – August Dec 04 '14 at 01:08

4 Answers4

1

may be you can encrypt password by MD5 or other encryption.

package test.md5;

import java.security.MessageDigest;

public class MD5Util {
    public final static String MD5(String s) {
        char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};       

        try {
            byte[] btInput = s.getBytes();
            // get MD5 MessageDigest obj
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // update
            mdInst.update(btInput);
            // get encryption string
            byte[] md = mdInst.digest();
            // change to hexadecimal
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        System.out.println(MD5Util.MD5("20121221"));
        System.out.println(MD5Util.MD5("encrypt"));
    }
}
TraXD
  • 26
  • 1
1

Password-oriented APIs in Java use char[]. For example, Console and JPasswordField return character arrays from password input, and PBEKey and KeyStore require a char[] for password-based encryption.

Conversion between byte[] and char[] would create more copies of the sensitive data in the heap, and it could be difficult to ensure that the character encoder erased all password data from its internal buffers.

Use a char[] to store passwords, and write '\0' to the array as soon as the password is no longer necessary.

erickson
  • 265,237
  • 58
  • 395
  • 493
0

chars are just multi-byte representations of characters, so a memory dump would give pretty much the same results. You should look into encrypting passwords for storage: as in this stackoverflow answer: Encrypt Password in Configuration Files? (Java)

Community
  • 1
  • 1
K.Nicholas
  • 10,956
  • 4
  • 46
  • 66
0

I am assuming that this question primarily relates to transferring of the password from one process on one host to another process on another host but will cover reading in the password to a particular variable as well.

The assignment issue is not limited to just assigning the password to a String instance which suffers from the issues that have been detailed in other SF answers such as this. I would add that you use a char or byte array not in the global context but locally in a method context so that it goes out of scope quickly once the method is exited making it available for GC. If it the variable is in the young generation then multiple GC cycles would be required to clear it once it has gone out of scope. Plus char/byte[] are mutable so it can be cleared out as well. However there would be still an opportunity for the password to be recovered from a heap dump it one coincides with this time.

In short:

  1. NEver persist the password in clear text in the backend. [Use Hashing with salt. Detailed here.]
  2. Never transfer the password in clear text on a non-secure medium. [Use TLS between the hosts]
Community
  • 1
  • 1
Khanna111
  • 3,627
  • 1
  • 23
  • 25
  • @Lucky_Singh: If this info helped, then please help me in increasing my reputation by accepting and / or upvoting. – Khanna111 Dec 04 '14 at 07:56