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

问题描述

网上有一些示例,说明如何使用Java中的弹性城堡库通过RSA/ECB/OAEPWithSHA256AndMGF1Padding进行加密(示例显示在).但是,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进行加密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 18:13