Irretrievably destroying data in Java

后端 未结 10 1341
时光说笑
时光说笑 2020-12-10 04:26

Is there anyway in Java to delete data (e.g., a variable value, object) and be sure it can\'t be recovered from memory? Does assigning null to a variable in Jav

相关标签:
10条回答
  • 2020-12-10 04:59

    Due to the wonders virtual memory, it is nearly impossible to delete something from memory in a completely irretrievable manner. Your best bet is to zero out the value fields; however:

    • This does not mean that an old (unzeroed) copy of the object won't be left on an unused swap page, which could persist across reboots.
    • Neither does it stop someone from attaching a debugger to your application and poking around before the object gets zeroed or crashing the VM and poking around in the heap dump.
    0 讨论(0)
  • 2020-12-10 05:00

    Primitive data (byte, char, int, double) and arrays of them (byte[], ...) are erasable by writing new random content into them.

    Object data have to be sanitized by overwriting their primitive properties; setting a variable to null just makes the object available for GC, but not immediately dead. A dump of VM will contain them for anyone to see.

    Immutable data such as String cannot be overwritten in any way. Any modification just makes a copy. You shall avoid keeping sensitive data in such objects.

    P.S. If we talk about passwords, it's better to use crypto-strong hash functions (MD5, SHA1, ...), and never ever work with passwords in clear text.

    0 讨论(0)
  • 2020-12-10 05:01

    Totally and completely irretrievable is something almost impossible in this day and age. When you normally delete something, the onlything that happens is that the first spot in your memory is emptied. This first spot used to contain the information as to howfar the memory had to be reserved for that program or something else.

    But all the other info is still there untill it's overwritten by someone else.

    i sudgest either TinyShredder, or using CCleaner set to the Gutmann-pass

    0 讨论(0)
  • 2020-12-10 05:02

    By setting your Object to null doesn't mean that your object is removed from memory. The Virtual Machine will flag that Object as ready for Garbage Collection if there are no more references to that Object. Depending on your code it might still be referenced even though you have set it to null in which case it will not be removed. (Essentially if you expect it to be garbage collected and it is not you have a memory leak!)

    Once it is flagged as ready for collection you have no control over when the Garbage Collector will remove it. You can mess around with Garbage Collection strategies but I wouldn't advise it. Profile your application and look at the object and it's id and you can see what is referencing it. Java provide VisualVM with 1.6.0_07 and above or you can use NetBeans

    0 讨论(0)
  • 2020-12-10 05:06

    I would think that your best bet (that isn't complex) is to use a char[] and then change each position in the array. The other comments about it being possible for it to be copied in memory still apply.

    0 讨论(0)
  • 2020-12-10 05:10

    As zacherates said, zero out the sensitive fields of your Object before removing references to it. Note that you can't zero out the contents of a String, so use char arrays and zero each element.

    0 讨论(0)
提交回复
热议问题