Verifica las llaves ya que podria ser problema de como las estan almacenando o leyendo
Este es un ejemplo que te podría servir
Java, esta hecho con bouncy castle
package javaapplication1;
import static com.sun.org.apache.xml.internal.serialize.OutputFormat.Defaults.Encoding;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.security.interfaces.RSAPrivateKey;
import javax.crypto.Cipher;
import org.bouncycastle.util.encoders.Base64;
import org.bouncycastle.asn1.*;
import org.bouncycastle.crypto.util.PrivateKeyInfoFactory;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAPrivateKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemReader;
public class JavaApplication1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
//String de prueba encriptado en C#, las llaves las genere en c# tambien
String hola = "3ges2OQM8SNQimvZc8LrKbtgRhXutgWj7U9QpLYLRFNmlC12FqZoS/RcHMRZ4nj/AGmBbWRqSJW8060FLKUgCszXs4K54Rhy0+P09WADPI9WJZAsPySbEboqhQldqFroXYQciGY67hzafGhtQmY04ig8/s66n/zymnXeyV34/sg=";
String filename = "\\private.Key.PEM";
PemReader pemReader = new PemReader(new InputStreamReader(new FileInputStream(filename)));
try {
Security.addProvider(new BouncyCastleProvider());
KeyFactory factory = KeyFactory.getInstance("RSA", "BC");
PemObject pemObject = pemReader.readPemObject();
byte[] content = pemObject.getContent();
PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(content);
PrivateKey privateKeyJ = factory.generatePrivate(privKeySpec);
System.out.println(decrypt(hola, (RSAPrivateKey) privateKeyJ));
} finally {
pemReader.close();
}
System.in.read();
}
public static String decrypt(String message, RSAPrivateKey key) {
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] dataEncripted = Base64.decode(message.getBytes());
byte[] data = cipher.doFinal(dataEncripted);
return new String(data, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
***** C# en este código de consola se crean y almacenan las llaves en archivos
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.X509;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var rsa = new RSACryptoServiceProvider();
JavaKey(rsa);
StringBuilder sb = new StringBuilder();
ExportPrivateKey(rsa, new StringWriter(sb));
System.IO.File.WriteAllText("private.Key.PEM", sb.ToString());
string pubkey = System.IO.File.ReadAllText("public.Key.cs.xml"); //rsa.ToXmlString(false);
string prikey = System.IO.File.ReadAllText("private.Key.cs.xml"); //rsa.ToXmlString(true);
var encr = Encrypt("hola", pubkey);
Console.WriteLine(encr);
Console.WriteLine(Dencrypt(encr , prikey));
}
public static string Encrypt(string data, string publicKey)
{
var sb = string.Empty;
try
{
var rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(publicKey);
byte[] encryptedByteArray;
byte[] dataToEncrypt;
dataToEncrypt = Encoding.ASCII.GetBytes(data);
encryptedByteArray = rsa.Encrypt(dataToEncrypt, false);
sb = Convert.ToBase64String(encryptedByteArray);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return sb;
}
public static string Dencrypt(string data, string privateKey)
{
var sb = string.Empty;
try
{
var rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(privateKey);
byte[] encryptedByteArray;
byte[] dataToEncrypt;
dataToEncrypt = Convert.FromBase64String(data);
encryptedByteArray = rsa.Decrypt(dataToEncrypt, false);
sb = Encoding.UTF8.GetString(encryptedByteArray);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return sb;
}
private static void JavaKey(RSACryptoServiceProvider rsa)
{
string serializedPublic = rsa.ToXmlString(false);
string serializedPrivate = rsa.ToXmlString(true);
System.IO.File.WriteAllText("public.Key.cs.xml", serializedPublic);
System.IO.File.WriteAllText("private.Key.cs.xml", serializedPrivate);
}
#region http://stackoverflow.com/a/23739932/890839
private static void ExportPrivateKey(RSACryptoServiceProvider csp, TextWriter outputStream)
{
if (csp.PublicOnly) throw new ArgumentException("CSP does not contain a private key", "csp");
var parameters = csp.ExportParameters(true);
using (var stream = new MemoryStream())
{
var writer = new BinaryWriter(stream);
writer.Write((byte)0x30); // SEQUENCE
using (var innerStream = new MemoryStream())
{
var innerWriter = new BinaryWriter(innerStream);
EncodeIntegerBigEndian(innerWriter, new byte[] { 0x00 }); // Version
EncodeIntegerBigEndian(innerWriter, parameters.Modulus);
EncodeIntegerBigEndian(innerWriter, parameters.Exponent);
EncodeIntegerBigEndian(innerWriter, parameters.D);
EncodeIntegerBigEndian(innerWriter, parameters.P);
EncodeIntegerBigEndian(innerWriter, parameters.Q);
EncodeIntegerBigEndian(innerWriter, parameters.DP);
EncodeIntegerBigEndian(innerWriter, parameters.DQ);
EncodeIntegerBigEndian(innerWriter, parameters.InverseQ);
var length = (int)innerStream.Length;
EncodeLength(writer, length);
writer.Write(innerStream.GetBuffer(), 0, length);
}
var base64 = Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length).ToCharArray();
outputStream.WriteLine("-----BEGIN RSA PRIVATE KEY-----");
// Output as Base64 with lines chopped at 64 characters
for (var i = 0; i < base64.Length; i += 64)
{
outputStream.WriteLine(base64, i, Math.Min(64, base64.Length - i));
}
outputStream.WriteLine("-----END RSA PRIVATE KEY-----");
}
}
private static void EncodeLength(BinaryWriter stream, int length)
{
if (length < 0) throw new ArgumentOutOfRangeException("length", "Length must be non-negative");
if (length < 0x80)
{
// Short form
stream.Write((byte)length);
}
else
{
// Long form
var temp = length;
var bytesRequired = 0;
while (temp > 0)
{
temp >>= 8;
bytesRequired++;
}
stream.Write((byte)(bytesRequired | 0x80));
for (var i = bytesRequired - 1; i >= 0; i--)
{
stream.Write((byte)(length >> (8 * i) & 0xff));
}
}
}
private static void EncodeIntegerBigEndian(BinaryWriter stream, byte[] value, bool forceUnsigned = true)
{
stream.Write((byte)0x02); // INTEGER
var prefixZeros = 0;
for (var i = 0; i < value.Length; i++)
{
if (value[i] != 0) break;
prefixZeros++;
}
if (value.Length - prefixZeros == 0)
{
EncodeLength(stream, 1);
stream.Write((byte)0);
}
else
{
if (forceUnsigned && value[prefixZeros] > 0x7f)
{
// Add a prefix zero to force unsigned if the MSB is 1
EncodeLength(stream, value.Length - prefixZeros + 1);
stream.Write((byte)0);
}
else
{
EncodeLength(stream, value.Length - prefixZeros);
}
for (var i = prefixZeros; i < value.Length; i++)
{
stream.Write(value[i]);
}
}
}
#endregion
}
}