Translate .NET 6 to .NET 4.8 ImportRSAPrivateKey in C# does not exist

I am trying to translate this C# .NET 6.0 code to run on .NET 4.8. I am trying to get a token using a RSA private key pem file. But I didn’t succeed.

Please, can someone help me ?

I have a problem with the ImportRSAPrivateKey method. It doesn’t exist in .NET 4.8. How can I change this ImportRSAPrivateKey method ?

It is an important method but Is there any other package in .NET 4.8 that can help to change this method?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>using System;
using System.IO;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
namespace toto.Auth
{
public class AuthRequestService
{
private const string DATE_PATTERN = "yyyy-MM-ddTHH:mm:ss.fffK";
private const string ODC_AUTH_URL = "https://myurl/auth";
private FileInfo certFile;
private string partnerId;
private RSA privateKey;
private JsonSerializerOptions jsonOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
public AuthRequestService(string certFilePath, string partnerId)
{
this.certFile = new FileInfo(certFilePath);
this.privateKey = ReadPKCS8PrivateKey(certFile);
this.partnerId = partnerId;
}
public string GetTokenForRPPS(string rpps)
{
var timeStamp = DateTime.UtcNow.ToString(DATE_PATTERN);
var body = new AuthRequestDTO(partnerId, rpps, timeStamp);
string result = string.Empty;
try
{
using (var privateSignature = new RSACryptoServiceProvider())
{
privateSignature.ImportRSAPrivateKey(privateKey.ExportRSAPrivateKey(), out _);
Console.WriteLine("unsigned json:");
var unsignedJson = JsonSerializer.Serialize(body, jsonOptions);
Console.WriteLine(unsignedJson);
var signatureBytes = privateSignature.SignData(Encoding.UTF8.GetBytes(unsignedJson), HashAlgorithmName.SHA512, RSASignaturePadding.Pkcs1);
string signB64 = Convert.ToBase64String(signatureBytes);
body.Signature = signB64;
string signedPayload = JsonSerializer.Serialize(body, jsonOptions);
Console.WriteLine("signed json:");
Console.WriteLine(signedPayload);
using (HttpClient httpClient = new HttpClient())
{
var httpRequest = new HttpRequestMessage(HttpMethod.Post, ODC_AUTH_URL)
{
Content = new StringContent(signedPayload, Encoding.UTF8, "application/json")
};
var response = httpClient.SendAsync(httpRequest).Result;
result = response.Content.ReadAsStringAsync().Result;
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
return result;
}
protected RSA ReadPKCS8PrivateKey(FileInfo file)
{
string key = File.ReadAllText(file.FullName);
string privateKeyPEM = key
.Replace("-----BEGIN RSA PRIVATE KEY-----", "")
.Replace(Environment.NewLine, "")
.Replace("-----END RSA PRIVATE KEY-----", "");
byte[] encoded = Convert.FromBase64String(privateKeyPEM);
return RSA.Create();
}
}
}
</code>
<code>using System; using System.IO; using System.Net.Http; using System.Security.Cryptography; using System.Text; using System.Text.Json; namespace toto.Auth { public class AuthRequestService { private const string DATE_PATTERN = "yyyy-MM-ddTHH:mm:ss.fffK"; private const string ODC_AUTH_URL = "https://myurl/auth"; private FileInfo certFile; private string partnerId; private RSA privateKey; private JsonSerializerOptions jsonOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; public AuthRequestService(string certFilePath, string partnerId) { this.certFile = new FileInfo(certFilePath); this.privateKey = ReadPKCS8PrivateKey(certFile); this.partnerId = partnerId; } public string GetTokenForRPPS(string rpps) { var timeStamp = DateTime.UtcNow.ToString(DATE_PATTERN); var body = new AuthRequestDTO(partnerId, rpps, timeStamp); string result = string.Empty; try { using (var privateSignature = new RSACryptoServiceProvider()) { privateSignature.ImportRSAPrivateKey(privateKey.ExportRSAPrivateKey(), out _); Console.WriteLine("unsigned json:"); var unsignedJson = JsonSerializer.Serialize(body, jsonOptions); Console.WriteLine(unsignedJson); var signatureBytes = privateSignature.SignData(Encoding.UTF8.GetBytes(unsignedJson), HashAlgorithmName.SHA512, RSASignaturePadding.Pkcs1); string signB64 = Convert.ToBase64String(signatureBytes); body.Signature = signB64; string signedPayload = JsonSerializer.Serialize(body, jsonOptions); Console.WriteLine("signed json:"); Console.WriteLine(signedPayload); using (HttpClient httpClient = new HttpClient()) { var httpRequest = new HttpRequestMessage(HttpMethod.Post, ODC_AUTH_URL) { Content = new StringContent(signedPayload, Encoding.UTF8, "application/json") }; var response = httpClient.SendAsync(httpRequest).Result; result = response.Content.ReadAsStringAsync().Result; } } } catch (Exception e) { Console.WriteLine(e); } return result; } protected RSA ReadPKCS8PrivateKey(FileInfo file) { string key = File.ReadAllText(file.FullName); string privateKeyPEM = key .Replace("-----BEGIN RSA PRIVATE KEY-----", "") .Replace(Environment.NewLine, "") .Replace("-----END RSA PRIVATE KEY-----", ""); byte[] encoded = Convert.FromBase64String(privateKeyPEM); return RSA.Create(); } } } </code>
using System;
using System.IO;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;

namespace toto.Auth
{
    public class AuthRequestService
    {
        private const string DATE_PATTERN = "yyyy-MM-ddTHH:mm:ss.fffK";
        private const string ODC_AUTH_URL = "https://myurl/auth";

        private FileInfo certFile;
        private string partnerId;
        private RSA privateKey;

        private JsonSerializerOptions jsonOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };

        public AuthRequestService(string certFilePath, string partnerId)
        {
            this.certFile = new FileInfo(certFilePath);
            this.privateKey = ReadPKCS8PrivateKey(certFile);
            this.partnerId = partnerId;
        }

        public string GetTokenForRPPS(string rpps)
        {
            var timeStamp = DateTime.UtcNow.ToString(DATE_PATTERN);

            var body = new AuthRequestDTO(partnerId, rpps, timeStamp);

            string result = string.Empty;

            try
            {
                using (var privateSignature = new RSACryptoServiceProvider())
                {
                    privateSignature.ImportRSAPrivateKey(privateKey.ExportRSAPrivateKey(), out _);

                    Console.WriteLine("unsigned json:");
                    var unsignedJson = JsonSerializer.Serialize(body, jsonOptions);
                    Console.WriteLine(unsignedJson);

                    var signatureBytes = privateSignature.SignData(Encoding.UTF8.GetBytes(unsignedJson), HashAlgorithmName.SHA512, RSASignaturePadding.Pkcs1);
                    string signB64 = Convert.ToBase64String(signatureBytes);
                    body.Signature = signB64;

                    string signedPayload = JsonSerializer.Serialize(body, jsonOptions);

                    Console.WriteLine("signed json:");
                    Console.WriteLine(signedPayload);

                    using (HttpClient httpClient = new HttpClient())
                    {
                        var httpRequest = new HttpRequestMessage(HttpMethod.Post, ODC_AUTH_URL)
                        {
                            Content = new StringContent(signedPayload, Encoding.UTF8, "application/json")
                        };

                        var response = httpClient.SendAsync(httpRequest).Result;

                        result = response.Content.ReadAsStringAsync().Result;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return result;
        }

        protected RSA ReadPKCS8PrivateKey(FileInfo file)
        {
            string key = File.ReadAllText(file.FullName);
            string privateKeyPEM = key
                .Replace("-----BEGIN RSA PRIVATE KEY-----", "")
                .Replace(Environment.NewLine, "")
                .Replace("-----END RSA PRIVATE KEY-----", "");

            byte[] encoded = Convert.FromBase64String(privateKeyPEM);
            return RSA.Create();
        }
    }
}

6

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Translate .NET 6 to .NET 4.8 ImportRSAPrivateKey in C# does not exist

I am trying to translate this C# .NET 6.0 code to run on .NET 4.8. I am trying to get a token using a RSA private key pem file. But I didn’t succeed.

Please, can someone help me ?

I have a problem with the ImportRSAPrivateKey method. It doesn’t exist in .NET 4.8. How can I change this ImportRSAPrivateKey method ?

It is an important method but Is there any other package in .NET 4.8 that can help to change this method?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>using System;
using System.IO;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
namespace toto.Auth
{
public class AuthRequestService
{
private const string DATE_PATTERN = "yyyy-MM-ddTHH:mm:ss.fffK";
private const string ODC_AUTH_URL = "https://myurl/auth";
private FileInfo certFile;
private string partnerId;
private RSA privateKey;
private JsonSerializerOptions jsonOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
public AuthRequestService(string certFilePath, string partnerId)
{
this.certFile = new FileInfo(certFilePath);
this.privateKey = ReadPKCS8PrivateKey(certFile);
this.partnerId = partnerId;
}
public string GetTokenForRPPS(string rpps)
{
var timeStamp = DateTime.UtcNow.ToString(DATE_PATTERN);
var body = new AuthRequestDTO(partnerId, rpps, timeStamp);
string result = string.Empty;
try
{
using (var privateSignature = new RSACryptoServiceProvider())
{
privateSignature.ImportRSAPrivateKey(privateKey.ExportRSAPrivateKey(), out _);
Console.WriteLine("unsigned json:");
var unsignedJson = JsonSerializer.Serialize(body, jsonOptions);
Console.WriteLine(unsignedJson);
var signatureBytes = privateSignature.SignData(Encoding.UTF8.GetBytes(unsignedJson), HashAlgorithmName.SHA512, RSASignaturePadding.Pkcs1);
string signB64 = Convert.ToBase64String(signatureBytes);
body.Signature = signB64;
string signedPayload = JsonSerializer.Serialize(body, jsonOptions);
Console.WriteLine("signed json:");
Console.WriteLine(signedPayload);
using (HttpClient httpClient = new HttpClient())
{
var httpRequest = new HttpRequestMessage(HttpMethod.Post, ODC_AUTH_URL)
{
Content = new StringContent(signedPayload, Encoding.UTF8, "application/json")
};
var response = httpClient.SendAsync(httpRequest).Result;
result = response.Content.ReadAsStringAsync().Result;
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
return result;
}
protected RSA ReadPKCS8PrivateKey(FileInfo file)
{
string key = File.ReadAllText(file.FullName);
string privateKeyPEM = key
.Replace("-----BEGIN RSA PRIVATE KEY-----", "")
.Replace(Environment.NewLine, "")
.Replace("-----END RSA PRIVATE KEY-----", "");
byte[] encoded = Convert.FromBase64String(privateKeyPEM);
return RSA.Create();
}
}
}
</code>
<code>using System; using System.IO; using System.Net.Http; using System.Security.Cryptography; using System.Text; using System.Text.Json; namespace toto.Auth { public class AuthRequestService { private const string DATE_PATTERN = "yyyy-MM-ddTHH:mm:ss.fffK"; private const string ODC_AUTH_URL = "https://myurl/auth"; private FileInfo certFile; private string partnerId; private RSA privateKey; private JsonSerializerOptions jsonOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; public AuthRequestService(string certFilePath, string partnerId) { this.certFile = new FileInfo(certFilePath); this.privateKey = ReadPKCS8PrivateKey(certFile); this.partnerId = partnerId; } public string GetTokenForRPPS(string rpps) { var timeStamp = DateTime.UtcNow.ToString(DATE_PATTERN); var body = new AuthRequestDTO(partnerId, rpps, timeStamp); string result = string.Empty; try { using (var privateSignature = new RSACryptoServiceProvider()) { privateSignature.ImportRSAPrivateKey(privateKey.ExportRSAPrivateKey(), out _); Console.WriteLine("unsigned json:"); var unsignedJson = JsonSerializer.Serialize(body, jsonOptions); Console.WriteLine(unsignedJson); var signatureBytes = privateSignature.SignData(Encoding.UTF8.GetBytes(unsignedJson), HashAlgorithmName.SHA512, RSASignaturePadding.Pkcs1); string signB64 = Convert.ToBase64String(signatureBytes); body.Signature = signB64; string signedPayload = JsonSerializer.Serialize(body, jsonOptions); Console.WriteLine("signed json:"); Console.WriteLine(signedPayload); using (HttpClient httpClient = new HttpClient()) { var httpRequest = new HttpRequestMessage(HttpMethod.Post, ODC_AUTH_URL) { Content = new StringContent(signedPayload, Encoding.UTF8, "application/json") }; var response = httpClient.SendAsync(httpRequest).Result; result = response.Content.ReadAsStringAsync().Result; } } } catch (Exception e) { Console.WriteLine(e); } return result; } protected RSA ReadPKCS8PrivateKey(FileInfo file) { string key = File.ReadAllText(file.FullName); string privateKeyPEM = key .Replace("-----BEGIN RSA PRIVATE KEY-----", "") .Replace(Environment.NewLine, "") .Replace("-----END RSA PRIVATE KEY-----", ""); byte[] encoded = Convert.FromBase64String(privateKeyPEM); return RSA.Create(); } } } </code>
using System;
using System.IO;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;

namespace toto.Auth
{
    public class AuthRequestService
    {
        private const string DATE_PATTERN = "yyyy-MM-ddTHH:mm:ss.fffK";
        private const string ODC_AUTH_URL = "https://myurl/auth";

        private FileInfo certFile;
        private string partnerId;
        private RSA privateKey;

        private JsonSerializerOptions jsonOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };

        public AuthRequestService(string certFilePath, string partnerId)
        {
            this.certFile = new FileInfo(certFilePath);
            this.privateKey = ReadPKCS8PrivateKey(certFile);
            this.partnerId = partnerId;
        }

        public string GetTokenForRPPS(string rpps)
        {
            var timeStamp = DateTime.UtcNow.ToString(DATE_PATTERN);

            var body = new AuthRequestDTO(partnerId, rpps, timeStamp);

            string result = string.Empty;

            try
            {
                using (var privateSignature = new RSACryptoServiceProvider())
                {
                    privateSignature.ImportRSAPrivateKey(privateKey.ExportRSAPrivateKey(), out _);

                    Console.WriteLine("unsigned json:");
                    var unsignedJson = JsonSerializer.Serialize(body, jsonOptions);
                    Console.WriteLine(unsignedJson);

                    var signatureBytes = privateSignature.SignData(Encoding.UTF8.GetBytes(unsignedJson), HashAlgorithmName.SHA512, RSASignaturePadding.Pkcs1);
                    string signB64 = Convert.ToBase64String(signatureBytes);
                    body.Signature = signB64;

                    string signedPayload = JsonSerializer.Serialize(body, jsonOptions);

                    Console.WriteLine("signed json:");
                    Console.WriteLine(signedPayload);

                    using (HttpClient httpClient = new HttpClient())
                    {
                        var httpRequest = new HttpRequestMessage(HttpMethod.Post, ODC_AUTH_URL)
                        {
                            Content = new StringContent(signedPayload, Encoding.UTF8, "application/json")
                        };

                        var response = httpClient.SendAsync(httpRequest).Result;

                        result = response.Content.ReadAsStringAsync().Result;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return result;
        }

        protected RSA ReadPKCS8PrivateKey(FileInfo file)
        {
            string key = File.ReadAllText(file.FullName);
            string privateKeyPEM = key
                .Replace("-----BEGIN RSA PRIVATE KEY-----", "")
                .Replace(Environment.NewLine, "")
                .Replace("-----END RSA PRIVATE KEY-----", "");

            byte[] encoded = Convert.FromBase64String(privateKeyPEM);
            return RSA.Create();
        }
    }
}

6

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật