{"id":120,"date":"2009-01-26T06:27:41","date_gmt":"2009-01-26T13:27:41","guid":{"rendered":"http:\/\/cknotes.com\/?p=120"},"modified":"2009-01-26T06:27:41","modified_gmt":"2009-01-26T13:27:41","slug":"encrypting-chinese-characters","status":"publish","type":"post","link":"https:\/\/cknotes.com\/encrypting-chinese-characters\/","title":{"rendered":"Encrypting Chinese Characters"},"content":{"rendered":"<p><strong>Question:<\/strong><br \/>\nWhy is it the return is blank when encrypting chinese characters?<br \/>\nHere&#8217;s a snippet of my code:<\/p>\n<pre>  crypt.KeyLength := 256;\r\n  crypt.SecretKey := Password;\r\n  crypt.CryptAlgorithm := 'aes';\r\n  crypt.EncodingMode := 'base64';\r\n  OutPutStr := crypt.EncryptStringENC(StringToEncrypt);<\/pre>\n<p><strong>Answer:<\/strong><\/p>\n<p>Strings in some programming languages such as Visual Basic, C#, VB.NET, Delphi, Foxpro, etc. should be thought of as objects.\u00a0 The object contains a string (i.e. a sequence of characters that renders to a sequence of glyphs).\u00a0 The representation of the string within the object is private &#8212; the application shouldn&#8217;t care.\u00a0 For these languages it happens to be Unicode (the 2-byte per char encoding), so the string object is capable of containing characters in any spoken language.\u00a0 (Of course, just because the string may contain characters in any spoken language doesn&#8217;t mean glyphs of any language are renderable, and this is a big problem in older programming languages such as VB6, Delphi, etc. where the visual controls are not capable of mixing glyphs of any language &#8212; i.e. they are not Unicode capable controls even though the string data type (i.e. object) holds characters represented internally in Unicode.<\/p>\n<p>OK, back to the main point&#8230;<\/p>\n<p>The representation of the string (i.e. the encoding used to represent each character as a sequence of 1 or more bytes) within the string object is private &#8212; the application shouldn&#8217;t care.\u00a0\u00a0 With encryption however, it matters greatly.\u00a0 Encryption algorithms operate on bytes.\u00a0 (The same goes for hash algorithms)\u00a0\u00a0 Therefore, when you encrypt Chinese characters, did you intend to encrypt 2-byte per char Unicode?\u00a0 Did you intend to encrypt the utf-8 representation of the characters?\u00a0 What about the &#8220;big5&#8221; or &#8220;gb2312&#8221; character encoding representations?\u00a0 All would provide different results (of course).<\/p>\n<p>The Crypt.Charset property controls the charset (character encoding) used for encrypting strings.\u00a0 The string passed to EncryptString* is first converted (internally) to a byte array using the specified character encoding, and then this byte array is encrypted.\u00a0 The default value for Crypt.Charset is &#8220;ANSI&#8221;.\u00a0 In most cases, this is what you expect &#8212; you&#8217;re expecting a typical European accented character to be represented as a single byte in the default charset of the computer.\u00a0 This doesn&#8217;t work with Chinese (or other Asian languages), or any language that doesn&#8217;t match the locale of the computer.\u00a0 The internal conversion from Unicode to ANSI is dropping the characters where there is no 1-byte\/char representation.<\/p>\n<p>The solution:\u00a0 Set Crypt.Charset equal to the encoding desired.\u00a0 For Chinese it would be one of the following:\u00a0 &#8220;utf-8&#8221;, &#8220;Unicode&#8221;, &#8220;big5&#8221;, &#8220;gb2312&#8221;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Question: Why is it the return is blank when encrypting chinese characters? Here&#8217;s a snippet of my code: crypt.KeyLength := 256; crypt.SecretKey := Password; crypt.CryptAlgorithm := &#8216;aes&#8217;; crypt.EncodingMode := &#8216;base64&#8217;; OutPutStr := crypt.EncryptStringENC(StringToEncrypt); Answer: Strings in some programming languages such as Visual Basic, C#, VB.NET, Delphi, Foxpro, etc. should be thought of as objects.\u00a0 The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[427,431],"class_list":["post-120","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-encoding","tag-encryption"],"_links":{"self":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/120","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/comments?post=120"}],"version-history":[{"count":0,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/120\/revisions"}],"wp:attachment":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/media?parent=120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/categories?post=120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/tags?post=120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}