title: Python: Write encrypted data to file
body: <p>I've made a chat app for school, and some people just write into the database. So my new project on it is to encrypt the resources. So I've made an encrypt function.</p>
  
  <p>It's working fine, but when I try to write a encrypted data at a file, I get an error Message:</p>
  
  <pre><code>File "C:\Python34\lib\encodings\cp1252.py", line 19, in encode
      return codecs.charmap_encode(input,self.errors,encoding_table)[0]
  UnicodeEncodeError: 'charmap' codec can't encode character '\x94' in position 0:         
  character maps to &lt;undefined&gt;
  </code></pre>
  
  <p>How to fix that problem?</p>
  
  <p>complete code:</p>
  
  <pre><code>def encrypts(data, step):
      newdata = ""
      i = 0
      while (len(data) &gt; len(step)):
          step += step[i]
          i += 1
      if (len(data) &lt; len(step)):
          step = step[:len(data)]
      for i in range(len(data)):
          a = ord(data[i])
          b = ord(step[i])
          newdata += chr(a+b)
      return newdata
  
  file = open("C:/Users/David/Desktop/file.msg","wb")
  file.write(encrypts("12345","code"))
  </code></pre>
  
  <p>Now, I finally solved my problem. The created ASCII Characters didn't exist. So I changed my functions: </p>
  
  <pre><code>def encrypts(data, step):
      newdata = ""
      i = 0
      while (len(data) &gt; len(step)):
          step += step[i]
          i += 1
      if (len(data) &lt; len(step)):
          step = step[:len(data)]
      for i in range(len(data)):
          a = ord(data[i])
          b = ord(step[i])
          newdata += chr(a+b-100)     #The "-100" fixed the problem.
      return newdata
  </code></pre>