Can anyone provide code in cSharp
I need C# implementation of RSA encryption and decryption with Cipher Type - RSA/ECB/PKCS1Padding.
Try This code.
String pubB64 = PHRConstants.publickey;
String text = strText;
byte[] textBytes = System.Text.Encoding.UTF8.GetBytes(text);
byte[] publicKeyBytes = Convert.FromBase64String(pubB64);
var keyLengthBits = 2048; // need to know length of public key in advance!
byte[] exponent = new byte[3];
byte[] modulus = new byte[keyLengthBits / 8];
Array.Copy(publicKeyBytes, publicKeyBytes.Length - exponent.Length, exponent, 0, exponent.Length);
Array.Copy(publicKeyBytes, publicKeyBytes.Length - exponent.Length - 2 - modulus.Length, modulus, 0, modulus.Length);
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
RSAParameters rsaKeyInfo = rsa.ExportParameters(false);
rsaKeyInfo.Modulus = modulus;
rsaKeyInfo.Exponent = exponent;
rsa.ImportParameters(rsaKeyInfo);
byte[] encrypted = rsa.Encrypt(textBytes, RSAEncryptionPadding.Pkcs1);
return Convert.ToBase64String(encrypted);
@AyushAgarwal i try this code and generate encrypted OTP with public key , but API https://healthidsbx.abdm.gov.in/api/v2/document/verify/mobile/otp gives error msg as incorrect OTP . can you please guide us in issue.
Please use this C# method for encrypting the data for V2 API
using System.Reflection;
using System.Security.Cryptography;
public async static Task RsaEncode(string message, bool encode = true)
{
try
{
if (encode && !string.IsNullOrEmpty(message))
{
byte[] byteData = Encoding.UTF8.GetBytes(message);
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @“abdm_public_key.pem”);
string publicKeyString = File.ReadAllText(path);
var rsaPublicKey = RSA.Create();
rsaPublicKey.ImportFromPem(publicKeyString);
byte[] bytesEncrypted = rsaPublicKey.Encrypt(byteData, RSAEncryptionPadding.Pkcs1);
return await Task.FromResult(Convert.ToBase64String(bytesEncrypted));
}
else
{
return await Task.FromResult(message);
}
}
catch (Exception ex)
{
ex.ToString();
}
return await Task.FromResult(string.Empty);
}
can someone pls provide the code in java as well
i have used this code to encryt aadhar. but when i posted that encrypted value, im getting respone as invalid loginid