备用'Convert.FromBase64String'方法 [英] Alternate 'Convert.FromBase64String' Method

查看:94
本文介绍了备用'Convert.FromBase64String'方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

我正在使用

Hi Guys..

I am using

Convert.FromBase64String

,但是我的数据库中有旧数据,我不知道输入数据(来自数据库)是否可以被4整除.

如何检查数据是base64字符串还是简单字符串... ???

对Convert.FromBase64String方法的查看略微 [

but i have old data in database and I don''t know whether the input data (that is coming from database) is divisible by 4 or not.

How can I check whether data is base64 string or the simple string...???

Slight Over View of Convert.FromBase64String Method [^]

Regards
Nikhil S.
Plz I need an URGENT solution to my problem. If you wish I am here to talk at <removed email>

Thanks In Advance...


-- I removed your email address. It''s not a good idea to post that here. When people suggest a solution or post a question or comment about your question you will be emailed and notified.

Hello and Thanks to all who considered a visit :)

As I have already Mentioned that my database contained old data i.e. before I started saving converted vales (Base64String). Now the Issue is that now the DB contains both the BASE64STRING as well as simple STRING values

I''ll try and generalize my problem.
this bit of code here throws an error...

String s = "Hello";
byte[] abc = Convert.FromBase64String(s);


为什么这没做... ???


Why doesn''t this bit do...???

String s = "Hell";
byte[] abc = Convert.FromBase64String(s);



谢谢..
Ns.



Thankks..
Ns.

推荐答案

将长度除以4仅在正确使用填充时才是一个很好的指示.由于填充是可选的(您可以通过使用剩余的%4长度来确定要添加多少个="="),字符串可以具有任意长度.

通常,您不能可靠地告诉
base-64 [
Divisibility of length by 4 is a good indication only when the padding is used properly. Since the padding is optional (you can figure out how many ''=''s to append by taking the remainder of length%4) your strings may have any length.

In general, you cannot reliably tell base-64[^] strings from other strings. In most cases, however, you know a string to be non base-64 if it contains characters outside the list of 64 characters used in base-64 encoding: A-Z, a-z, 0-9, +, /, and trailing = for padding. Any string composed from legal base-64 characters can be decoded successfully.


严格来说,无法对其进行检查.您只能做一些猜测.您可以尝试将其解码为base64,然后查看结果是否有意义,

base64字符串仅包含ASCII字符,8位填充,``0''..''9'',``A''..''Z'',``a''...'' z'',``+'',``/''.编码有几种变体.通常,隐含MIME规范(基于RFC 1421).参见:
http://en.wikipedia.org/wiki/Base64 [ http://en.wikipedia.org/wiki/Base64#MIME [ http://tools.ietf.org/html/rfc1421 [简单字符串"?哪有这回事. (顺便说一句,我不明白除以4在这里有什么意义.)

问题是:base64编码标准不包含任何有助于识别格式的元数据.此类元数据始终放置在base64数据之外.例如,在电子邮件中,您通常会获得标头Content-Type: multipart/mixed;一个或多个零件的零件头将带有Content-Type: application/octet-streamContent-Transfer-Encoding: base64.您可以将其视为显示如何使用字符串的元数据.如果在软件中使用base64,则应该执行类似的操作,但是如何处理可用的旧数据?来不及了...

—SA
Strictly speaking, there is no way to check it up. You can only do some guesswork. You can try to decode it as base64 and see if the results make any sense, no more.

The base64 string simply consists of ASCII characters, 8-bit padded, ''0''..''9'', ''A''..''Z'', ''a''...''z'', ''+'', ''/''. There are several variants of the encoding. Usually, MIME specification (based on RFC 1421) is implied. See:
http://en.wikipedia.org/wiki/Base64[^],
http://en.wikipedia.org/wiki/Base64#MIME[^],
http://tools.ietf.org/html/rfc1421[^].

Imagine you generated a completely random sequence of such characters and format text exactly as base64 encoders usually do, using same delimiters, including end-of-line characters — this formatting is purely optional for base64. Is it a base64-encoded data? Yes and no — it depends on how you look at it. What is "simple string" anyway? There is no such thing. (By the way, I don''t understand how division by 4 can be relevant here.)

The problem is: the standard for base64 encoding does not include any meta-data which would help to recognize the format. Such meta-data is always placed outside of base64 data. For example, in e-mail you will usually get a header Content-Type: multipart/mixed; and one or more of the parts will have part headers with Content-Type: application/octet-stream and Content-Transfer-Encoding: base64. You can consider this as the meta-data showing you how to use the string. You should do something like that if you use base64 in your software, but what to do with available legacy data? Too late…

—SA


作为其他答案,通常是不可能的.但是,根据该字段应保留的内容,可以指定一些规则来做出很好的猜测.这三个规则(按照我的顺序)将适用于大多数类型的数据.

数据应该是多长时间?如果它是固定长度(例如ID),则可以查看字符串的长度,如果它比您预期的字符串base64大约1/3的话.

数据通常是否包含base64范围以外的字符?编码中无效的任何字符(例如空格,标点符号,带重音符号的字符)都表示未对base64进行编码.名称和地址通常会带有空格.

它应该是人类可读的文本吗?尝试对它进行base64解码;如果输出字符串中的任何一个不是可读字符(除了\ r,\ n和\ t以外的任何低于32的字符,可能还有标点符号),则它不是base64.
As the other answers state, in general it is not possible. However, depending on what the field is supposed to hold, you can specify some rules to make a good guess. These three rules (in this order, I think) will work for most types of data.

How long is the data supposed to be? If it is a fixed length (e.g. an ID) you can look at the length of the string, if it is ~1/3 larger than you expect it is base64.

Would the data normally contain characters outside the base64 range? Any character which is not valid in the encoding (e.g. spaces, punctuation, accented characters) means it''s not base64 encoded. Names and addresses will usually have spaces.

Is it supposed to be human readable text? Try base64 decoding it; if any of the output string is not a readable character (anything below 32 except for \r, \n and \t, possibly punctuation characters) then it was not base64.


这篇关于备用'Convert.FromBase64String'方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆