如何使用 .net 中的充气城堡使用 RSA/ECB/OAEPWithSHA256AndMGF1Padding 进行加密? [英] How do I use bouncy castle in .net to encrypt using RSA/ECB/OAEPWithSHA256AndMGF1Padding?

查看:66
本文介绍了如何使用 .net 中的充气城堡使用 RSA/ECB/OAEPWithSHA256AndMGF1Padding 进行加密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网络上有关于如何使用 Java 中的充气城堡库来使用 RSA/ECB/OAEPWithSHA256AndMGF1Padding 进行加密的示例(示例显示在 分解 RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING).然而,C# 中的充气城堡库似乎偏离了 Java 库,因为它更明确(因此需要更多步骤),我无法弄清楚如何使其适用于上述算法.

There are examples on the web on how to use bouncy castle library in Java to encrypt with RSA/ECB/OAEPWithSHA256AndMGF1Padding (Example is shown at breaking down RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING). However the bouncy castle library in C# seem to have deviated from Java library in that it is more explicit (hence requires more steps) and I am not able to figure out how to make it work for the above algorithm.

如果有人可以将代码示例放在一起以使用 RSA/ECB/OAEPWithSHA256AndMGF1Padding 加密示例文本,我们将不胜感激.

Would appreciate if some body can put a code sample together to encrypt a sample text using RSA/ECB/OAEPWithSHA256AndMGF1Padding.

推荐答案

不幸的是,即使是 Java 结构也是模棱两可的,因为它对不同且不兼容的解释持开放态度,如图所示 此处.Java Bouncycastle 提供程序将使用 "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" 做一件事,而 Oracle 提供程序将做不同的事情.

Unfortunately, even the Java construct is ambiguous as it's open to different and incompatible interpretations, as is shown here. The Java Bouncycastle provider will do one thing with "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" and the Oracle provider will do a different thing.

您可以而且应该在 Java 和 C# 代码中准确指定您想要的行为.

You can and should specify exactly which behavior you want in both the Java and C# code.

C#:

using System;
using System.IO;
using System.Text;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.OpenSsl;

namespace ScratchPad
{
    class MainClass
    {
        public static void OaepEncryptExample()
        {
            var plain = Encoding.UTF8.GetBytes("The sun also rises.");
            // Read in public key from file
            var pemReader = new PemReader(File.OpenText(@"/Users/horton/tmp/key-examples/myserver_pub.pem"));
            var rsaPub = (Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters)pemReader.ReadObject();
            // create encrypter
            var encrypter = new OaepEncoding(new RsaEngine(), new Sha256Digest(), new Sha256Digest(), null);
            encrypter.Init(true, rsaPub);
            var cipher = encrypter.ProcessBlock(plain, 0, plain.Length);
            Console.WriteLine(Convert.ToBase64String(cipher));
        }
    }
}

Java:

import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.openssl.PEMParser;

import javax.crypto.Cipher;
import javax.crypto.spec.OAEPParameterSpec;
import javax.crypto.spec.PSource;
import java.io.FileReader;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.MGF1ParameterSpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;

public class OaepExample {
    public static void oeapDecrypt() throws Exception {
        final PEMParser pemParser = new PEMParser(new FileReader("/Users/horton/tmp/key-examples/myserver.p8"));
        final PrivateKeyInfo privKey = (PrivateKeyInfo) pemParser.readObject();
        KeyFactory kf = KeyFactory.getInstance("RSA");
        RSAPrivateKey rsaPriv = (RSAPrivateKey) kf.generatePrivate(new PKCS8EncodedKeySpec(privKey.getEncoded()));
        String cipher64 = "k8AYnTV6RgzQXmD7qn8QwucDXGjbYct+qMVvDmMELTnUcCOeTp82oJ0BryZyEEGXVSZ2BFg95e72Jt9ZAKWNcot2rZ0+POcda8pzY/MfdwIpnSJKITovk8xHL3B/jZDJyQrLMmNPjVV/uBFY2vgKhhLhJzzAJATcGpNdw+gF+XI=";
        Cipher decrypter = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
        OAEPParameterSpec parameterSpec = new OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256,
                PSource.PSpecified.DEFAULT);
        decrypter.init(Cipher.DECRYPT_MODE, rsaPriv, parameterSpec);
        final byte[] plain = decrypter.doFinal(Base64.getDecoder().decode(cipher64));
        System.out.println(new String(plain, StandardCharsets.UTF_8));

    }
}

这篇关于如何使用 .net 中的充气城堡使用 RSA/ECB/OAEPWithSHA256AndMGF1Padding 进行加密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆