Want to use generated RSA public key in java, in c# function to encrypt data and decrypt it in Java decrypt function.
Generated Java public key has been replaced in Modulus tag in c#:
static string publicKey = "<RSAKeyValue><Modulus>MFwwDQ...wEAAQ==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
C# Encrypt function:
static string Encrypt(string text)
{
const int PROVIDER_RSA_FULL = 1;
const string CONTAINER_NAME = "Tracker";
CspParameters cspParams;
cspParams = new CspParameters(PROVIDER_RSA_FULL);
cspParams.KeyContainerName = CONTAINER_NAME;
RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider(512,cspParams);
rsa1.FromXmlString(publicKey);
byte[] textBytes = Encoding.UTF8.GetBytes(text);
byte[] encryptedOutput = rsa1.Encrypt(textBytes, RSAEncryptionPadding.Pkcs1);
string outputB64 = Convert.ToBase64String(encryptedOutput);
return outputB64;
}
Java Decrypt function:
static String Decrypt(String encodedString,PrivateKey privKey) {
try {
Cipher cipher = Cipher.getInstance(cipherInstancename);
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(encodedString));
return new String(decrypted, "UTF-8");
} catch (Exception err) {
return err.fillInStackTrace().toString();
}
}
First question: Is it correct to replace Java public key in Modulus tag in c# XML string? What about the Exponent tag? I used AQAB value for it.
The Second question: Why on decrypting in Java got this error:
javax.crypto.IllegalBlockSizeException: Data must not be longer than 64 bytes
After some research I found, it's a general error what cause can make this type of error?