Unk*_*own 33
编辑
我已经制定了一个使用ctypes(后者又使用C)来使内存为零的解决方案.
import sys
import ctypes
def zerome(string):
    location = id(string) + 20
    size     = sys.getsizeof(string) - 20
    memset =  ctypes.cdll.msvcrt.memset
    # For Linux, use the following. Change the 6 to whatever it is on your computer.
    # memset =  ctypes.CDLL("libc.so.6").memset
    print "Clearing 0x%08x size %i bytes" % (location, size)
    memset(location, 0, size)
我不保证此代码的安全性.它经过测试可用于x86和CPython 2.6.2.这里有更长的写作.
在Python中解密和加密是行不通的.字符串和整数是间接和持久的,这意味着你将在整个地方留下一堆密码信息.
散列是标准答案,当然明文最终需要在某处进行处理.
正确的解决方案是将敏感过程作为C模块.
但是,如果你的记忆不断被破坏,我会重新考虑你的安全设置.
... 唯一的解决方案是使用可变数据结构.也就是说,您必须仅使用允许您动态替换元素的数据结构.例如,在Python中,您可以使用列表来存储字符数组.但是,每次从列表中添加或删除元素时,语言都可能会将整个列表复制到背后,具体取决于实现细节.为了安全起见,如果必须动态调整数据结构的大小,则应创建一个新数据结构,复制数据,然后覆盖旧数据结构.例如:
def paranoid_add_character_to_list(ch, l):
  """Copy l, adding a new character, ch.  Erase l.  Return the result."""
  new_list = []
  for i in range(len(l)):
    new_list.append(0)
  new_list.append(ch)
  for i in range(len(l)):
    new_list[i] = l[i]
    l[i] = 0
  return new_list
来源:http://www.ibm.com/developerworks/library/s-data.html