Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Java等效于.NET RSACryptoServiceProvider和SHA-1_C#_Java_Android_.net_Cryptography - Fatal编程技术网

C# Java等效于.NET RSACryptoServiceProvider和SHA-1

C# Java等效于.NET RSACryptoServiceProvider和SHA-1,c#,java,android,.net,cryptography,C#,Java,Android,.net,Cryptography,我有以下C语言的数据签名代码# 但是我得到了不匹配的输出字节数组。我错在哪里?在Cipher.getInstance(…)中应该使用什么转换?使用签名.getInstance(“SHA1withRSA”)。加密与签名生成不同。不同的填充机制 由Afshin更新 完整的解决方案。注意使用私有指数,即,而不是公共指数 检查这个@ZohraKhan,我首先从那篇文章中编写了我的Java代码。这篇文章中使用的方法是“加密”,而我使用的SignData内置了一个散列函数。PS我假设这里使用的是旧的PKC

我有以下C语言的数据签名代码#


但是我得到了不匹配的输出字节数组。我错在哪里?在
Cipher.getInstance(…)
中应该使用什么转换?

使用
签名.getInstance(“SHA1withRSA”)
。加密与签名生成不同。不同的填充机制


由Afshin更新

完整的解决方案。注意使用私有指数,即
,而不是公共指数


检查这个@ZohraKhan,我首先从那篇文章中编写了我的Java代码。这篇文章中使用的方法是“加密”,而我使用的SignData内置了一个散列函数。PS我假设这里使用的是旧的PKCS#1填充,RSACryptServiceProvider文档忘记提到使用的填充机制。我的一个老答案也衡量了这一点,它似乎是正确的。
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

string PrivateKeyText = "<RSAKeyValue><Modulus>....</D></RSAKeyValue>";

rsa.FromXmlString(PrivateKeyText);

string data = "my data";        

byte[] SignedByteData = rsa.SignData(Encoding.UTF8.GetBytes(data), new SHA1CryptoServiceProvider());
String modulusElem = "...";
String expElem = "...";

byte[] expBytes = Base64.decode(expElem, Base64.DEFAULT);
byte[] modulusBytes = Base64.decode(modulusElem, Base64.DEFAULT);

BigInteger modulus = new BigInteger(1, modulusBytes);
BigInteger exponent = new BigInteger(1, expBytes);

try {
    KeyFactory factory = KeyFactory.getInstance("RSA");

    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");

    String data = "my data";

    MessageDigest md = MessageDigest.getInstance("SHA-1");
    byte[] hashedData = md.digest(data.getBytes("UTF-8"));

    RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(modulus, exponent);

    PublicKey publicKey = factory.generatePublic(pubSpec);

    cipher.init(Cipher.ENCRYPT_MODE, publicKey);

    byte[] SignedByteData = cipher.doFinal(hashedData);

} catch (Exception e){

}
String modulusElem = "...";
String dElem = "...";

byte[] modulusBytes = Base64.decode(modulusElem, Base64.DEFAULT);
byte[] dBytes = Base64.decode(dElem, Base64.DEFAULT);

BigInteger modulus = new BigInteger(1, modulusBytes);
BigInteger d = new BigInteger(1, dBytes);

String data = "my data";            

try {
        Signature signature = Signature.getInstance("SHA1withRSA");

        KeyFactory factory = KeyFactory.getInstance("RSA");

        RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(modulus, d);

        PrivateKey privateKey = factory.generatePrivate(privateKeySpec);

        signature.initSign(privateKey);

        signature.update(data.getBytes("UTF-8"));

        byte[] SignedByteData = signature.sign();

} catch(Exception e) {
    e.printStackTrace();
}