Because the company uses at the same time. NET and JAVA, and individual service sets use interfaces to communicate. So I looked at the difference between C# secret keys and JAVA keys. RSA makes no difference to the program itself; the format is the same. The syntax used for storage (wrapped classes) will vary from program to program. There are many RSA syntax and syntax standards. The major types are asN.1, PKCS, x.509. .NET uses the standard RSA format and encodes the numbers in base64 to generate XML for storage. Java is used in PKCS#8, x.509 public and private key syntax, store the corresponding Java classes automatically generated base64 strings. C# can use org. BouncyCastle’s standard algorithm to generate Java public and private key encryption and decryption.
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.X509;
using System;
namespace MinervaTeachControlClient.Utils
{
/// <summary>
///RSA key format conversion
/// </summary>
public class RSAKeyConvert
{
public static string PK { get; set; }
public static string SK { get; set; }
/// <summary>
///To get the key
/// </summary>
public static void GetPKAndSk()
{
RsaKeyPairGenerator rsaKeyPairGenerator = new RsaKeyPairGenerator();
RsaKeyGenerationParameters rsaKeyGenerationParameters = new RsaKeyGenerationParameters(BigInteger.ValueOf(3), new Org.BouncyCastle.Security.SecureRandom(), 1024.25);
rsaKeyPairGenerator.Init(rsaKeyGenerationParameters);// Initialize parameters
AsymmetricCipherKeyPair keyPair = rsaKeyPairGenerator.GenerateKeyPair();
AsymmetricKeyParameter publicKey = keyPair.Public;/ / the public key
AsymmetricKeyParameter privateKey = keyPair.Private;/ / the private key
SubjectPublicKeyInfo subjectPublicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey);
PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKey);
Asn1Object asn1ObjectPublic = subjectPublicKeyInfo.ToAsn1Object();
byte[] publicInfoByte = asn1ObjectPublic.GetEncoded();
Asn1Object asn1ObjectPrivate = privateKeyInfo.ToAsn1Object();
byte[] privateInfoByte = asn1ObjectPrivate.GetEncoded();
PK = Convert.ToBase64String(publicInfoByte);// Generated public key
SK = Convert.ToBase64String(privateInfoByte);// The generated key
}
public static string DecryptByPrivateKey(string s, string key)
{
s = s.Replace("\r"."").Replace("\n"."").Replace(""."");
// Asymmetric encryption algorithm for encryption and decryption
IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());
/ / decryption
try
{
engine.Init(false, GetPrivateKeyParameter(key));
byte[] byteData = Convert.FromBase64String(s);
var ResultData = engine.ProcessBlock(byteData, 0, byteData.Length);
return System.Text.Encoding.UTF8.GetString(ResultData);
}
catch (Exception ex)
{
returnex.Message; }}public static string EncryptByPrivateKey(string s, string key)
{
// Asymmetric encryption algorithm for encryption and decryption
IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());
/ / encryption
try
{
engine.Init(true, GetPrivateKeyParameter(key));
byte[] byteData = System.Text.Encoding.UTF8.GetBytes(s);
var ResultData = engine.ProcessBlock(byteData, 0, byteData.Length);
return Convert.ToBase64String(ResultData);
// console. WriteLine(" Ciphertext (base64 encoding) :" + convert.tobase64String (testData) + environment.newline);
}
catch (Exception ex)
{
returnex.Message; }}private static AsymmetricKeyParameter GetPrivateKeyParameter(string s)
{
s = s.Replace("\r"."").Replace("\n"."").Replace(""."");
byte[] privateInfoByte = Convert.FromBase64String(s);
// Asn1Object priKeyObj = Asn1Object.FromByteArray(privateInfoByte); // This can also be read from the stream, imported locally
// PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKey);
AsymmetricKeyParameter priKey = PrivateKeyFactory.CreateKey(privateInfoByte);
return priKey;
}
private static AsymmetricKeyParameter GetPublicKeyParameter(string s)
{
s = s.Replace("\r"."").Replace("\n"."").Replace(""."");
byte[] publicInfoByte = Convert.FromBase64String(s);
Asn1Object pubKeyObj = Asn1Object.FromByteArray(publicInfoByte);// This can also be read from the stream, imported locally
AsymmetricKeyParameter pubKey = PublicKeyFactory.CreateKey(publicInfoByte);
returnpubKey; }}}Copy the code