加密数据时如何保留回车符?
我有以下设置,一个文本区域以其中可能包含回车符的一些数据命名,另一个文本区域具有 style='display:none'
以使其隐藏,如下所示:
<textarea id="myTextarea" onBlur="encryptMyData()"></textarea>
<textarea name="encryptedText" style='display:none'></textarea>
用户输入第一个文本区域中的数据,当该文本区域失去焦点时,“encryptMyData()”javascript 函数将调用 ajax 调用来获取用户在第一个文本字段中输入的任何内容,使用 rijndael 对其进行加密,然后将其粘贴到 cryptoText 文本区域中,以便稍后将其存储在数据库中。
现在我需要做的是,找到一种方法将加密前的回车转换为像 [cr]
这样的标签,以便当我检索数据时,保留所有格式。知道我该怎么做吗?我使用 asp.net 和 c# 来执行加密。
I have this following setup, a textarea named with some data in it that may have carriage returns and another textarea that has style='display:none'
in order to make it hidden as follows:
<textarea id="myTextarea" onBlur="encryptMyData()"></textarea>
<textarea name="encryptedText" style='display:none'></textarea>
the user enters data in the first textarea and when that text area loses focus the 'encryptMyData()' javascript function is calling an ajax call to take whatever the user entered in the first textfield, encrypt it using rijndael, and paste it in the encryptedText textarea so that it is stored in the database later.
Now what I need to do is this, find a way to convert the carriage returns before encryption to a tag like so [cr]
so that when I retrieve the data, all formatting is retained. Any idea how I do this? I'm using asp.net and c# to perform the encryption.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的换行符可能仍然存在于加密数据中。
如果您绝对希望“显示”保留换行符的加密数据,则可能需要执行 stringData.Split(Environment.NewLine) ,分别加密每个结果字符串,然后对 String.Split 进行加密。在返回网页之前,将字符串重新连接在一起(Environment.NewLine,arrayOfEncryptedDataLines)。
-编辑-
不过,你最好不要经过服务器。看看http://www.hanewin.net/encrypt/aes/aes。嗯
Your newline characters are likely still present in the encrypted data.
If you absolutely do want to "display" the encrypted data with newlines retained, you likely need to do a
stringData.Split(Environment.NewLine)
, encrypt each resulting string separately, thenString.Join(Environment.NewLine, arrayOfEncryptedDataLines)
the strings back together before returning to the webpage.-edit-
You might be better off not going by the server, though. Have a look at http://www.hanewin.net/encrypt/aes/aes.htm
您可以使用 JavaScript
escape()
方法注意回车符和空格。在服务器端,您需要再次对序列进行取消转义,但 C# 中没有默认的取消转义方法。您可以尝试在中使用 unescape 方法Microsoft.JScript.GlobalObject
命名空间。You can use the JavaScript
escape()
method to take care of the carriage returns and spaces. Server-side you need to unescape the sequence again, but there is no default unescape method present in C#. You could try using the unescape method in theMicrosoft.JScript.GlobalObject
namespace.基本上,对您的服务的 AJAX 调用可能会删除新行,因此您需要在调用 Web 服务之前进行转换。
在
encryptMyData
函数中,在将其发送到服务器之前执行替换:然后将一些文本传递给 ajax 调用,而不是直接传递文本区域的值。
Basically the AJAX call to your service is probably stripping out the new lines, so you'll need to do the conversion before you call the web service.
In your
encryptMyData
function perform a replace before you send it the server:Then pass sometext to the ajax call, rather than the straight value of the textarea.