将字符串转换为'巨大'整数 [英] Convert string to 'huge' integer

查看:90
本文介绍了将字符串转换为'巨大'整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的C#控制台应用程序生成非常非常大的数字。到目前为止,我已经能够使用它们的字符串形式,但现在我需要它们作为我需要做的整数。唯一的问题是它们非常大而且我的意思是大(超过500个字符,并且当我将其用于其预期目的时,很可能是数千个)并且我需要将数字添加到整数而不是生成数字的字符串。是否有一个足以容纳数百个字符的整数?如果是这样,请有人帮我做。它们必须是整数。谢谢。

解决方案

有一个 BigInteger结构 [ ^ ]在.NET 4.0及更高版本中,它将适用于任何大小的整数,直至可用内存。


< blockquote>感谢您的澄清。我明白了:你的方向错了。



如果你真的需要一些整数运算来你自己的加密算法,你会真的需要一些大的整数对象。实际上,在引擎盖下,加密算法使用它们。 (不,我不是说这个确切的结构是用的:加密算法实现通常是原生的,而不是.NET。[结束编辑])但是这些大整数比你想要的更多,更少。如果您尝试使用您想要的整数大小的类似(或任何此类问题)加密算法,它们将起作用,但性能过低。此外,很有可能,当中间计算结果占用所有可用的计算机内存时,您会遇到一些情况。使用 BigInteger ,这并不容易,但非常很简单。



如你所说你不需要这个算术,它会让你的请求毫无意义。在所有加密算法的公共方面,所有数据都没有排除,它表示为 byte [] 数组。



您需要学习如何抽象为密码学等数据处理发布的所有文本编码。为此,您只需要了解编码的工作原理,特别是了解Unicode。仅供参考:

http://msdn.microsoft .com / zh-cn / library / system.text.encoding.aspx [ ^ ],

http:/ /en.wikipedia.org/wiki/Unicode [ ^ ],

http://unicode.org/ [ ^ ]。



不要混淆它们。







HenryHunt写道:

我想要做的是当有人登录或填写详细信息以请求帐户时,它会对输入的数据进行加密并使用WCF将其发送到另一台包含帐户数据库的计算机,然后将其解密发送的数据...

好的,但这里没什么特别的。您需要使用可用的加密,更准确地说,使用公钥加密:

http:// en .wikipedia.org / wiki / Public-key_cryptography [ ^ ]。



你需要彻底了解它的工作原理。这个想法是:服务器部分应为每个请求生成密钥对,并向用户发送加密密钥。如果此密钥被窃听,其他人只能加密但不能解密,因为它是使用仅存储在服务器上的私钥完成的,从未发布过。在下一个状态,客户端加密消息并发送它。 (可以使用某种识别令牌。)如果是HTTPS,您可以使用HTTP服务器(TLS)提供的内容,以及使用证书的数字签名,始终在服务器上,可选在客户端。请参阅:

http://en.wikipedia.org/wiki/HTTPS [< a href =http://en.wikipedia.org/wiki/HTTPStarget =_ blanktitle =New Window> ^ ],

http://en.wikipedia.org/wiki/Transport_Layer_Security [ ^ ],

http ://en.wikipedia.org/wiki/Digital_signature [ ^ ],

http://en.wikipedia.org/wiki/Digital_certificate [ ^ ],

http://en.wikipedia.org/wiki/Certificate_authorit y [ ^ ]。



但你可以在自定义服务器上自己完成。



另外,重要的是要理解,如果你也是需要密码保护,你应该明白1)密码不应该存储在任何地方(它绝对不需要进行身份验证,至少会侵犯隐私),2)你不需要加密,因为加密哈希函数可以使用。请查看我过去的答案:

我已经加密了密码但是当我登录时给出了我错了。如何解密 [ ^ ] ,

使用用户名和密码进行TCP连接 [ ^ ],

解密加密密码 [ ^ ],

以安全的方式存储密码值int sql server [ ^ ]。



您已经拥有.NET FCL中的所有算法实现,特别是:

http://msdn.microsoft.com/en-us/library/system.security.cryptography.asymmetricalgorithm%28v=vs.110%29。 aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.security.cryptography.hashalgorithm.aspx [ ^ ]。



-SA

My C# console application generates very, very large numbers. Up to now I have been able to get past with using the string form of them but now I need them to be integers for what I need to do. The only problem is that they are very big and I mean big (in excess of 500 characters and will most likely be in the thousands when I use it for the purpose it is intended for) and I need the numbers to be added to integers not strings as the number is generated. Is there an integer big enough to hold hundreds and hundreds of characters? If so, please could someone help me do it. They must be integers. Thank you.

解决方案

There is the BigInteger structure[^] in .NET 4.0 and above which will work with integers of any size, up to available memory.


Thank you for the clarification. I got it: you are going in a wrong way.

If you really needed some integer arithmetic for your own encryption algorithm, you would really need some big integer objects. Actually, under the hood, encryption algorithms use them. ([EDIT] No, I don't mean this exact structure is use: cryptography algorithm implementations are usually native, not .NET. [END EDIT]) But these big integers are much, much less then those you want to have. If you tried similar (or any, for this matter) encryption algorithms with the integer sizes you wanted, they would work, but with prohibitively low performance. Besides, chances are, you could come across some situations when intermediate calculation result would occupy all available computer memory. With BigInteger, this is not easy but very easy.

As you say you don't need this arithmetic, it makes you request totally pointless. On the public side of all cryptography algorithms, all data, with no exclusions, it represented as byte[] arrays.

And you need to learn how to abstract all that text encoding issued for data processing like cryptography. For that, you just need to understand how encoding works, and learn about Unicode, in particular. Just for the hints:
http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx[^],
http://en.wikipedia.org/wiki/Unicode[^],
http://unicode.org/[^].

Don't mix them up.

[EDIT]

HenryHunt wrote:

What I am trying to do is when someone signs in or fills in details to request an account it encrypts the entered data and sends it using WCF to another computer containing a database of accounts which it then decrypts the sent data…

Okay, but there is nothing special here. You need to use available encryption, more exactly, public-key cryptography:
http://en.wikipedia.org/wiki/Public-key_cryptography[^].

You need to learn thoroughly why it works. The idea is: the server part should generate a key pair for each request and send the user a key for encryption. If this key is eavesdropped, someone else can only encrypt, but not decrypt, because it is done with a private key only stored on the server, never published. On next state, client encrypts the message and sends it. (Some kind of identification token can be used.) If it was HTTPS, you could use what is already supplied with an HTTP server (TLS), plus digital signing with the certificate, always on server, optionally on client. Please see:
http://en.wikipedia.org/wiki/HTTPS[^],
http://en.wikipedia.org/wiki/Transport_Layer_Security[^],
http://en.wikipedia.org/wiki/Digital_signature[^],
http://en.wikipedia.org/wiki/Digital_certificate[^],
http://en.wikipedia.org/wiki/Certificate_authority[^].

But you can do it all by yourself in a custom server.

Also, it's important to understand that, if you also need password protection, you should understand that 1) a password should not be stored anywhere (it's absolutely not needed for authentication and would violate at least privacy), 2) you would not need encryption, as cryptographic hash function can be used. Please see my past answers:
i already encrypt my password but when i log in it gives me an error. how can decrypte it[^],
TCP Connection with username and password[^],
Decryption of Encrypted Password[^],
storing password value int sql server with secure way[^].

And you already have all algorithm implementations in .NET FCL, in particular:
http://msdn.microsoft.com/en-us/library/system.security.cryptography.asymmetricalgorithm%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.security.cryptography.hashalgorithm.aspx[^].

—SA


这篇关于将字符串转换为'巨大'整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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