123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Org.BouncyCastle.Asn1.Pkcs;
- using Org.BouncyCastle.Asn1.X509;
- 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.Crypto.Engines;
- using Org.BouncyCastle.X509;
- using Org.BouncyCastle.Crypto;
- using Org.BouncyCastle.Asn1;
- using Org.BouncyCastle.Crypto.Encodings;
- namespace MySystem
- {
- public class RSAForJava
- {
- public RSAForJava()
- {
- }
- /// <summary>
- /// KEY 结构体
- /// </summary>
- public struct RSAKEY
- {
- /// <summary>
- /// 公钥
- /// </summary>
- public string PublicKey
- {
- get;
- set;
- }
- /// <summary>
- /// 私钥
- /// </summary>
- public string PrivateKey
- {
- get;
- set;
- }
- }
- public RSAKEY GetKey()
- {
- //RSA密钥对的构造器
- RsaKeyPairGenerator keyGenerator = new RsaKeyPairGenerator();
- //RSA密钥构造器的参数
- RsaKeyGenerationParameters param = new RsaKeyGenerationParameters(
- Org.BouncyCastle.Math.BigInteger.ValueOf(3),
- new Org.BouncyCastle.Security.SecureRandom(),
- 1024, //密钥长度
- 25);
- //用参数初始化密钥构造器
- keyGenerator.Init(param);
- //产生密钥对
- AsymmetricCipherKeyPair keyPair = keyGenerator.GenerateKeyPair();
- //获取公钥和密钥
- AsymmetricKeyParameter publicKey = keyPair.Public;
- AsymmetricKeyParameter privateKey = keyPair.Private;
- SubjectPublicKeyInfo subjectPublicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey);
- PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKey);
- Asn1Object asn1ObjectPublic = subjectPublicKeyInfo.ToAsn1Object();
- byte[] publicInfoByte = asn1ObjectPublic.GetEncoded("UTF-8");
- Asn1Object asn1ObjectPrivate = privateKeyInfo.ToAsn1Object();
- byte[] privateInfoByte = asn1ObjectPrivate.GetEncoded("UTF-8");
- RSAKEY item = new RSAKEY()
- {
- PublicKey = Convert.ToBase64String(publicInfoByte),
- PrivateKey = Convert.ToBase64String(privateInfoByte)
- };
- return item;
- }
- private AsymmetricKeyParameter GetPublicKeyParameter(string s)
- {
- s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");
- byte[] publicInfoByte = Convert.FromBase64String(s);
- Asn1Object pubKeyObj = Asn1Object.FromByteArray(publicInfoByte);//这里也可以从流中读取,从本地导入
- AsymmetricKeyParameter pubKey = PublicKeyFactory.CreateKey(publicInfoByte);
- return pubKey;
- }
- private AsymmetricKeyParameter GetPrivateKeyParameter(string s)
- {
- s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");
- byte[] privateInfoByte = Convert.FromBase64String(s);
- // Asn1Object priKeyObj = Asn1Object.FromByteArray(privateInfoByte);//这里也可以从流中读取,从本地导入
- // PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKey);
- AsymmetricKeyParameter priKey = PrivateKeyFactory.CreateKey(privateInfoByte);
- return priKey;
- }
- private AsymmetricKeyParameter GetPrivateKeyParameter2(string s)
- {
- s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");
- byte[] privateInfoByte = System.Text.Encoding.UTF8.GetBytes(s);
- // Asn1Object priKeyObj = Asn1Object.FromByteArray(privateInfoByte);//这里也可以从流中读取,从本地导入
- // PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKey);
- AsymmetricKeyParameter priKey = PrivateKeyFactory.CreateKey(privateInfoByte);
- return priKey;
- }
- public string EncryptByPrivateKey(string s, string key)
- {
- //非对称加密算法,加解密用
- IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());
- //加密
- 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("密文(base64编码):" + Convert.ToBase64String(testData) + Environment.NewLine);
- }
- catch (Exception ex)
- {
- return ex.Message;
- }
- }
- public string DecryptByPublicKey(string s, string key)
- {
- s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");
- //非对称加密算法,加解密用
- IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());
- //解密
- try
- {
- engine.Init(false, GetPublicKeyParameter(key));
- byte[] byteData = Convert.FromBase64String(s);
- string result = "";
- List<byte> cache = new List<byte>();
- for (int i = 0; i < byteData.Length; i++)
- {
- cache.Add(byteData[i]);
- if ((i + 1) % 256 == 0 || i + 1 == byteData.Length)
- {
- var ResultData = engine.ProcessBlock(cache.ToArray(), 0, cache.ToArray().Length);
- result += System.Text.Encoding.UTF8.GetString(ResultData);
- cache.Clear();
- }
- }
- return result;
- }
- catch (Exception ex)
- {
- return ex.Message;
- }
- }
- public string DecryptByPrivateKey(string s, string key)
- {
- s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");
- //非对称加密算法,加解密用
- IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());
- //解密
- try
- {
- engine.Init(false, GetPrivateKeyParameter2(key));
- byte[] byteData = Convert.FromBase64String(s);
- string result = "";
- List<byte> cache = new List<byte>();
- for (int i = 0; i < byteData.Length; i++)
- {
- cache.Add(byteData[i]);
- if ((i + 1) % 256 == 0 || i + 1 == byteData.Length)
- {
- var ResultData = engine.ProcessBlock(cache.ToArray(), 0, cache.ToArray().Length);
- result += System.Text.Encoding.UTF8.GetString(ResultData);
- cache.Clear();
- }
- }
- return result;
- }
- catch (Exception ex)
- {
- return ex.Message;
- }
- }
- }
- }
|