C#使用公共RSA-1280十六进制密钥加密字符串 [英] C# Encrypt string with public RSA-1280 hex key

查看:285
本文介绍了C#使用公共RSA-1280十六进制密钥加密字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用由服务器发送给我的公共RSA密钥加密密码。

I've been trying to encrypt a password with a public RSA key that is sent to me by the server.

var csp = new CspParameters(1, "Microsoft Strong Cryptographic Provider");
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(1280, csp);
byte[] key = ByteUtils.HexToBytes(client.RSAKey);
RSA.ImportCspBlob(key);
byte[] encrypted = RSA.Encrypt(Encoding.ASCII.GetBytes(password), true);

十六进制键以以下格式提供:

The hex key is provided in such format:

string key = "30819D300D06092A864886F70D010101050003818B0030818702818100C7BD672D8C634D443840AD809790852770D3A2E99F456D6516329E0205D0645C23FD001D4D070CEE368A20526FEB2402358C915D7E86102B1659AA8651C449C344599F72BE904B8E338E7002E9978453C5BBCCA51AC165AA265069E0EAB1411D11A2FFDD35E5A8296A6A2AF238945874E8206979B0A16E2E4260A161CAB5C905020111";

由于字符串是十六进制格式的320字节长,我假设密钥是160字节(RSA 1280)
使用这种方法,提供者一直在说Bad Version of provider.\r\\\

我已经尝试了几种方法,将其转换为Base64,只需将其作为ASCII / Unicode导入。没有任何工作到目前为止。

As the string is 320-bytes long in hex format, I assume the key is 160 bytes (RSA 1280) Using this method, the provider keeps saying "Bad Version of provider.\r\n". I've tried several methods, convert it to Base64, simply import it as ASCII / Unicode. Nothing worked so far.

编辑:我的HexToBytes函数(它的工作原理,它返回正确的160-b数组):

My HexToBytes function (which works afaik, it returns me correct 160-b array):

public static byte[] HexToBytes(string pValue)
        {
            // FIRST. Use StringBuilder.
            StringBuilder builder = new StringBuilder();

            // SECOND... USE STRINGBUILDER!... and LINQ.
            foreach (char c in pValue.Where(IsHexDigit).Select(Char.ToUpper))
            {
                builder.Append(c);
            }

            // THIRD. If you have an odd number of characters, something is very wrong.
            string hexString = builder.ToString();
            if (hexString.Length % 2 == 1)
            {
                //throw new InvalidOperationException("There is an odd number of hexadecimal digits in this string.");
                // I will just add a zero to the end, who cares (0 padding)
                Log.WriteLine(LogLevel.Debug, "Hexstring had an odd number of hexadecimal digits.");
                hexString += '0';
            }

            byte[] bytes = new byte[hexString.Length / 2];
            // FOURTH. Use the for-loop like a pro :D
            for (int i = 0, j = 0; i < bytes.Length; i++, j += 2)
            {
                string byteString = String.Concat(hexString[j], hexString[j + 1]);
                bytes[i] = HexToByte(byteString);
            }
            return bytes;
        }


推荐答案

您的公钥不在正确的格式。它不是CSP blob。它是一个DER编码的SubjectPublicKeyInfo结构。你可以找到源代码来解析它,或者你可以自己编写。 这里是此类代码的一个例子。

Your public key is not in the correct format. It is not a CSP blob. It is a DER encoded SubjectPublicKeyInfo structure. You can find source code to parse it or you can write your own. Here is one example of such code.

这篇关于C#使用公共RSA-1280十六进制密钥加密字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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