示例代碼
openssl生成私鑰1024位
genrsa -out rsa1024_private_key.pem 1024
把RSA私鑰轉換成PKCS8格式(java用的PKCS8格式,C#不用轉換)
pkcs8 -topk8 -inform PEM -in rsa1024_private_key.pem -outform PEM –nocrypt
openssl生成公鑰
rsa -in rsa1024_private_key.pem -pubout -out rsa1024_public_key.pem
//待加密字元串
string encryptString = "歐陽修讀書";
//加密文本
string encode = RsaOpenSSLKeyConvert.Encrypt(encryptString, publicKey, "UTF-8");
//解密文本
string jimistr1 = RsaOpenSSLKeyConvert.Decrypt(encode, privateKey, "UTF-8");
public class RsaOpenSSLKeyConvert
{
/// <summary>
/// RSA私鑰解密
/// </summary>
/// <param name="txt">待解密字元串</param>
/// <param name="privateKey">私鑰字元串,非xml</param>
/// <param name="input_charset">原文格式編碼</param>
/// <returns></returns>
public static string Decrypt(string txt, string privateKey, string input_charset)
{
RSACryptoServiceProvider rsa = RsaOpenSSLKeyConvert.CreateDecryptRSA(privateKey);
byte[] plaintextbarray = Convert.FromBase64String(txt);
byte[] cyphertextbarray = rsa.Decrypt(plaintextbarray, false);
string result = Encoding.GetEncoding(input_charset).GetString(cyphertextbarray);
return result;
}
/// <summary>
/// 根據字元串私鑰key創建RSA對象,解密
/// </summary>
/// <param name="priKey"></param>
/// <returns></returns>
private static RSACryptoServiceProvider CreateDecryptRSA(string privateKey)
{
var privkey = Convert.FromBase64String(privateKey);
byte[] MODULUS, E, D, P, Q, DP, DQ, IQ;
// --------- Set up stream to decode the asn.1 encoded RSA private key ------
MemoryStream mem = new MemoryStream(privkey);
BinaryReader binr = new BinaryReader(mem); //wrap Memory Stream with BinaryReader for easy reading
byte bt = ;
ushort twobytes = ;
int elems = ;
try
{
twobytes = binr.ReadUInt16();
if (twobytes == ) //data read as little endian order (actual data order for Sequence is 30 81)
binr.ReadByte(); //advance 1 byte
else if (twobytes == )
binr.ReadInt16(); //advance 2 bytes
else
return null;
twobytes = binr.ReadUInt16();
if (twobytes != ) //version number
return null;
bt = binr.ReadByte();
if (bt != )
return null;
//------ all private key components are Integer sequences ----
elems = GetIntegerSize(binr);
MODULUS = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
E = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
D = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
P = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
Q = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
DP = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
DQ = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
IQ = binr.ReadBytes(elems);
// ------- create RSACryptoServiceProvider instance and initialize with public key -----
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSAParameters RSAparams = new RSAParameters();
RSAparams.Modulus = MODULUS;
RSAparams.Exponent = E;
RSAparams.D = D;
RSAparams.P = P;
RSAparams.Q = Q;
RSAparams.DP = DP;
RSAparams.DQ = DQ;
RSAparams.InverseQ = IQ;
RSA.ImportParameters(RSAparams);
return RSA;
}
catch (Exception)
{
return null;
}
finally
{
binr.Close();
}
}
private static int GetIntegerSize(BinaryReader binr)
{
byte bt = ;
byte lowbyte = ;
byte highbyte = ;
int count = ;
bt = binr.ReadByte();
if (bt != ) //expect integer
return ;
bt = binr.ReadByte();
if (bt == )
count = binr.ReadByte(); // data size in next byte
else
if (bt == )
{
highbyte = binr.ReadByte(); // data size in next 2 bytes
lowbyte = binr.ReadByte();
byte[] modint = { lowbyte, highbyte, , };
count = BitConverter.ToInt32(modint, );
}
else
{
count = bt; // we already have the data size
}
while (binr.ReadByte() == )
{ //remove high order zeros in data
count -= ;
}
//last ReadByte wasn't a removed zero, so back up a byte
binr.BaseStream.Seek(-, SeekOrigin.Current);
return count;
}
/// <summary>
/// RSA公鑰加密
/// </summary>
/// <param name="resData">待加密的字元串</param>
/// <param name="publicKey">公鑰</param>
/// <param name="input_charset">編碼格式</param>
/// <returns>明文</returns>
public static string Encrypt(string resData, string publicKey, string input_charset)
{
byte[] DataToEncrypt = Encoding.GetEncoding(input_charset).GetBytes(resData);
RSACryptoServiceProvider rsa = EncryptRSAPublicKey(publicKey);
byte[] result = rsa.Encrypt(DataToEncrypt, false);
return Convert.ToBase64String(result);
}
/// <summary>
/// 根據公鑰字元串,創建加密RSA對象
/// </summary>
/// <param name="publickey"></param>
/// <returns></returns>
private static RSACryptoServiceProvider EncryptRSAPublicKey(String publickeyStr)
{
byte[] publickey = Convert.FromBase64String(publickeyStr);
// encoded OID sequence for PKCS #1 rsaEncryption szOID_RSA_RSA = "1.2.840.113549.1.1.1"
byte[] SeqOID = { , , , , , , , , , , , , , , };
byte[] seq = new byte[];
// --------- Set up stream to read the asn.1 encoded SubjectPublicKeyInfo blob ------
MemoryStream mem = new MemoryStream(publickey);
BinaryReader binr = new BinaryReader(mem); //wrap Memory Stream with BinaryReader for easy reading
byte bt = ;
ushort twobytes = ;
try
{
twobytes = binr.ReadUInt16();
if (twobytes == ) //data read as little endian order (actual data order for Sequence is 30 81)
binr.ReadByte(); //advance 1 byte
else if (twobytes == )
binr.ReadInt16(); //advance 2 bytes
else
return null;
seq = binr.ReadBytes(); //read the Sequence OID
if (!CompareBytearrays(seq, SeqOID)) //make sure Sequence for OID is correct
return null;
twobytes = binr.ReadUInt16();
if (twobytes == ) //data read as little endian order (actual data order for Bit String is 03 81)
binr.ReadByte(); //advance 1 byte
else if (twobytes == )
binr.ReadInt16(); //advance 2 bytes
else
return null;
bt = binr.ReadByte();
if (bt != ) //expect null byte next
return null;
twobytes = binr.ReadUInt16();
if (twobytes == ) //data read as little endian order (actual data order for Sequence is 30 81)
binr.ReadByte(); //advance 1 byte
else if (twobytes == )
binr.ReadInt16(); //advance 2 bytes
else
return null;
twobytes = binr.ReadUInt16();
byte lowbyte = ;
byte highbyte = ;
if (twobytes == ) //data read as little endian order (actual data order for Integer is 02 81)
lowbyte = binr.ReadByte(); // read next bytes which is bytes in modulus
else if (twobytes == )
{
highbyte = binr.ReadByte(); //advance 2 bytes
lowbyte = binr.ReadByte();
}
else
return null;
byte[] modint = { lowbyte, highbyte, , }; //reverse byte order since asn.1 key uses big endian order
int modsize = BitConverter.ToInt32(modint, );
byte firstbyte = binr.ReadByte();
binr.BaseStream.Seek(-, SeekOrigin.Current);
if (firstbyte == )
{ //if first byte (highest order) of modulus is zero, don't include it
binr.ReadByte(); //skip this null byte
modsize -= ; //reduce modulus buffer size by 1
}
byte[] modulus = binr.ReadBytes(modsize); //read the modulus bytes
if (binr.ReadByte() != ) //expect an Integer for the exponent data
return null;
int expbytes = (int)binr.ReadByte(); // should only need one byte for actual exponent data (for all useful values)
byte[] exponent = binr.ReadBytes(expbytes);
// ------- create RSACryptoServiceProvider instance and initialize with public key -----
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSAParameters RSAKeyInfo = new RSAParameters();
RSAKeyInfo.Modulus = modulus;
RSAKeyInfo.Exponent = exponent;
RSA.ImportParameters(RSAKeyInfo);
return RSA;
}
catch (Exception)
{
return null;
}
finally { binr.Close(); }
}
private static bool CompareBytearrays(byte[] a, byte[] b)
{
if (a.Length != b.Length)
return false;
int i = ;
foreach (byte c in a)
{
if (c != b[i])
return false;
i++;
}
return true;
}
}