summaryrefslogtreecommitdiff
path: root/pipermail/pycrypto/2014q1/000762.html
blob: c1bd3cb9db430886e42a03d7630fe4b205ca0576 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
 <HEAD>
   <TITLE> [pycrypto] Unicode?
   </TITLE>
   <LINK REL="Index" HREF="index.html" >
   <LINK REL="made" HREF="mailto:pycrypto%40lists.dlitz.net?Subject=Re%3A%20%5Bpycrypto%5D%20Unicode%3F&In-Reply-To=%3CCAEncD4eja_Su42nR-6QLcM578RBsrF%2BKyNDZ9R_8_s2S%2B7GTvw%40mail.gmail.com%3E">
   <META NAME="robots" CONTENT="index,nofollow">
   <style type="text/css">
       pre {
           white-space: pre-wrap;       /* css-2.1, curent FF, Opera, Safari */
           }
   </style>
   <META http-equiv="Content-Type" content="text/html; charset=us-ascii">
   <LINK REL="Previous"  HREF="000761.html">
   <LINK REL="Next"  HREF="000763.html">
 </HEAD>
 <BODY BGCOLOR="#ffffff">
   <H1>[pycrypto] Unicode?</H1>
    <B>Dave Pawson</B> 
    <A HREF="mailto:pycrypto%40lists.dlitz.net?Subject=Re%3A%20%5Bpycrypto%5D%20Unicode%3F&In-Reply-To=%3CCAEncD4eja_Su42nR-6QLcM578RBsrF%2BKyNDZ9R_8_s2S%2B7GTvw%40mail.gmail.com%3E"
       TITLE="[pycrypto] Unicode?">dave.pawson at gmail.com
       </A><BR>
    <I>Mon Feb  3 05:21:28 PST 2014</I>
    <P><UL>
        <LI>Previous message: <A HREF="000761.html">[pycrypto] Unicode?
</A></li>
        <LI>Next message: <A HREF="000763.html">[pycrypto] Unicode?
</A></li>
         <LI> <B>Messages sorted by:</B> 
              <a href="date.html#762">[ date ]</a>
              <a href="thread.html#762">[ thread ]</a>
              <a href="subject.html#762">[ subject ]</a>
              <a href="author.html#762">[ author ]</a>
         </LI>
       </UL>
    <HR>  
<!--beginarticle-->
<PRE>&lt;grin/&gt;
Not sure Jeremy! I'd never heard of it.
I'll let you know.... I'm assuming it is 2.x compatible?

regards

On 3 February 2014 12:40, Jeremy Gray &lt;<A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">jrgray at gmail.com</A>&gt; wrote:
&gt;<i> I may not understand the constraints of your situation, but wouldn't
</I>&gt;<i> python's &quot;universal newlines&quot; file-open mode achieve what you want?  infile
</I>&gt;<i> = open(filename, 'Urb')
</I>&gt;<i>
</I>&gt;<i> --Jeremy
</I>&gt;<i>
</I>&gt;<i>
</I>&gt;<i> On Mon, Feb 3, 2014 at 5:35 AM, Dave Pawson &lt;<A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">dave.pawson at gmail.com</A>&gt; wrote:
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> Now resolved.
</I>&gt;&gt;<i> Source plain text is created / modified in either DOS or Linux (np to add
</I>&gt;&gt;<i> Mac)
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> read using
</I>&gt;&gt;<i> chunk = unicode(infile.read(chunksize))
</I>&gt;&gt;<i> # replace newline as appropriate
</I>&gt;&gt;<i> chunk=replaceNL(chunk)
</I>&gt;&gt;<i> # Convert to byte string
</I>&gt;&gt;<i> chunk=safe_str(chunk)
</I>&gt;&gt;<i> encrypt and write to disk
</I>&gt;&gt;<i>
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> # for decrypt
</I>&gt;&gt;<i> #Iterate over file to read into string
</I>&gt;&gt;<i> # replace the individual bytes of the Unicode character (u2022 in my case)
</I>&gt;&gt;<i> # with \n for the local machine
</I>&gt;&gt;<i> retval = replaceBullet(retval)
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> code below
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> #
</I>&gt;&gt;<i> #Swap \n for \u2022
</I>&gt;&gt;<i> #
</I>&gt;&gt;<i> def replaceNL(str):
</I>&gt;&gt;<i>     # If DOS, replace \r\x0A
</I>&gt;&gt;<i>     # If Unix, replace \n
</I>&gt;&gt;<i>     lineEnd=u'\n'
</I>&gt;&gt;<i>     if string.find(str,'\r\x0a'):
</I>&gt;&gt;<i>         lineEnd=u'\r\x0A'
</I>&gt;&gt;<i>     return string.replace(str,lineEnd,u'\u2022')
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> #
</I>&gt;&gt;<i> # Replace bullet by \n
</I>&gt;&gt;<i> #
</I>&gt;&gt;<i> def replaceBullet(bstr):
</I>&gt;&gt;<i>     return string.replace(bstr,u'\\u2022',u'\n')
</I>&gt;&gt;<i>
</I>&gt;&gt;<i>
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> def safe_str(obj):
</I>&gt;&gt;<i>     &quot;&quot;&quot; return the byte string representation of obj &quot;&quot;&quot;
</I>&gt;&gt;<i>     try:
</I>&gt;&gt;<i>         return str(obj)
</I>&gt;&gt;<i>     except UnicodeEncodeError:
</I>&gt;&gt;<i>         # obj is unicode
</I>&gt;&gt;<i>         return unicode(obj).encode('unicode_escape')
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> HTH others, though it seems messy, it works.
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> Dave
</I>&gt;&gt;<i>
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> On 3 February 2014 09:39, Dave Pawson &lt;<A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">dave.pawson at gmail.com</A>&gt; wrote:
</I>&gt;&gt;<i> &gt; I'm having a problem 'sharing' an encrypted file between
</I>&gt;&gt;<i> &gt; MSDOS and Linux.
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt; so I thought I'd replace \nl in the plain text with a non ASCII
</I>&gt;&gt;<i> &gt; character prior to encryption.
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt; encryptor = AES.new(key, AES.MODE_CBC, iv)
</I>&gt;&gt;<i> &gt; outfile.write(encryptor.encrypt(chunk))
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt; gives me
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt;  File &quot;/usr/lib64/python2.7/site-packages/Crypto/Cipher/blockalgo.py&quot;,
</I>&gt;&gt;<i> &gt; line 244, in encrypt
</I>&gt;&gt;<i> &gt;     return self._cipher.encrypt(plaintext)
</I>&gt;&gt;<i> &gt; UnicodeEncodeError: 'ascii' codec can't encode character u'\u2022' in
</I>&gt;&gt;<i> &gt; position 34: ordinal not in range(128)
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt; It would seem I can't use non-ASCII characters, at least with AES, is
</I>&gt;&gt;<i> &gt; this right ?
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt; If not, how to address it please?
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt; regards
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt; --
</I>&gt;&gt;<i> &gt; Dave Pawson
</I>&gt;&gt;<i> &gt; XSLT XSL-FO FAQ.
</I>&gt;&gt;<i> &gt; Docbook FAQ.
</I>&gt;&gt;<i> &gt; <A HREF="http://www.dpawson.co.uk">http://www.dpawson.co.uk</A>
</I>&gt;&gt;<i>
</I>&gt;&gt;<i>
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> --
</I>&gt;&gt;<i> Dave Pawson
</I>&gt;&gt;<i> XSLT XSL-FO FAQ.
</I>&gt;&gt;<i> Docbook FAQ.
</I>&gt;&gt;<i> <A HREF="http://www.dpawson.co.uk">http://www.dpawson.co.uk</A>
</I>&gt;&gt;<i> _______________________________________________
</I>&gt;&gt;<i> pycrypto mailing list
</I>&gt;&gt;<i> <A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">pycrypto at lists.dlitz.net</A>
</I>&gt;&gt;<i> <A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto</A>
</I>&gt;<i>
</I>&gt;<i>
</I>&gt;<i>
</I>&gt;<i> _______________________________________________
</I>&gt;<i> pycrypto mailing list
</I>&gt;<i> <A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">pycrypto at lists.dlitz.net</A>
</I>&gt;<i> <A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto</A>
</I>&gt;<i>
</I>


-- 
Dave Pawson
XSLT XSL-FO FAQ.
Docbook FAQ.
<A HREF="http://www.dpawson.co.uk">http://www.dpawson.co.uk</A>
</PRE>





<!--endarticle-->
    <HR>
    <P><UL>
        <!--threads-->
	<LI>Previous message: <A HREF="000761.html">[pycrypto] Unicode?
</A></li>
	<LI>Next message: <A HREF="000763.html">[pycrypto] Unicode?
</A></li>
         <LI> <B>Messages sorted by:</B> 
              <a href="date.html#762">[ date ]</a>
              <a href="thread.html#762">[ thread ]</a>
              <a href="subject.html#762">[ subject ]</a>
              <a href="author.html#762">[ author ]</a>
         </LI>
       </UL>

<hr>
<a href="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">More information about the pycrypto
mailing list</a><br>
</body></html>