Last active
March 31, 2017 06:50
-
-
Save VictorZhang2014/f0a69d5a120ea500bdd7ca07ac86659a to your computer and use it in GitHub Desktop.
RSACryptography, a set of RSA de/encryt methods who can read the p12 file with a password , and can generate public key in terms of its private key. It was written by C#.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This file is encrypt/decrypt data in a default way which can not decrypt/decrypt a chunk of data | |
using System; | |
using System.Text; | |
using System.Security.Cryptography; | |
using System.Security.Cryptography.X509Certificates; | |
namespace RSA_CSharp_Example | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Generate public key in terms of private key file | |
X509Certificate2 x509 = new X509Certificate2("C:\\Users\\victor\\Desktop\\private_key.p12", "123456"); | |
//Gain private key | |
RSACryptoServiceProvider provider = (RSACryptoServiceProvider)x509.PrivateKey; | |
string public_key = x509.PublicKey.Key.ToXmlString(false); | |
string hello = "hello, world!"; | |
string cipherHello = RSAEncrypt(public_key, hello); | |
Console.WriteLine(cipherHello); | |
string plainHello = RSADecrypt(provider, cipherHello); | |
Console.WriteLine(plainHello); | |
Console.ReadKey(); | |
} | |
/// <summary> | |
/// RSA Encryption | |
/// </summary> | |
public static string RSAEncrypt(string xmlPublicKey, string m_strEncryptString) | |
{ | |
string result = ""; | |
try | |
{ | |
RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); | |
provider.FromXmlString(xmlPublicKey); | |
byte[] bytes = new UnicodeEncoding().GetBytes(m_strEncryptString); | |
result = Convert.ToBase64String(provider.Encrypt(bytes, false)); | |
} | |
catch (Exception exception) | |
{ | |
throw exception; | |
} | |
return result; | |
} | |
/// <summary> | |
/// RSA Decryption | |
/// </summary> | |
public static string RSADecrypt(RSACryptoServiceProvider provider, string m_strDecryptString) | |
{ | |
string result = ""; | |
try | |
{ | |
byte[] rgb = Convert.FromBase64String(m_strDecryptString); | |
byte[] buffer2 = provider.Decrypt(rgb, false); | |
result = new UnicodeEncoding().GetString(buffer2); | |
} | |
catch (Exception exception) | |
{ | |
throw exception; | |
} | |
return result; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This file is encrypt/decrypt data in a subsection way which can decrypt/decrypt a chunk of data | |
using System; | |
using System.Text; | |
using System.Security.Cryptography; | |
using System.Security.Cryptography.X509Certificates; | |
using System.IO; | |
namespace RSA_CSharp_Example | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Generate public key in terms of private key file | |
X509Certificate2 x509 = new X509Certificate2("C:\\Users\\victor\\Desktop\\VL_Private_Key.p12", "123456"); | |
//Gain private key | |
RSACryptoServiceProvider provider = (RSACryptoServiceProvider)x509.PrivateKey; | |
string public_key = x509.PublicKey.Key.ToXmlString(false); | |
string hello = "hello my world"; | |
string cipherHello = RSAEncrypt(public_key, hello); | |
Console.WriteLine(cipherHello); | |
string plainHello = RSADecrypt(provider, cipherHello); | |
Console.WriteLine(plainHello); | |
Console.ReadKey(); | |
} | |
/// <summary> | |
/// RSA Encryption | |
/// </summary> | |
public static string RSAEncrypt(string xmlPublicKey, string m_strEncryptString) | |
{ | |
string result = ""; | |
try | |
{ | |
RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); | |
provider.FromXmlString(xmlPublicKey); | |
//https://dotblogs.com.tw/yc421206/archive/2012/06/26/73044.aspx | |
//分段加密, 如果不分段加密,则会提示异常“ 要解密的数据超过此模块的最大值 128 字节” | |
int bufferSize = (provider.KeySize / 8) - 11; | |
byte[] buffer = new byte[bufferSize]; | |
//byte[] originalData = new UnicodeEncoding().GetBytes(m_strEncryptString); | |
byte[] originalData = Encoding.UTF8.GetBytes(m_strEncryptString); | |
MemoryStream input = new MemoryStream(originalData); | |
MemoryStream output = new MemoryStream(); | |
int readLine = input.Read(buffer, 0, bufferSize); | |
while (readLine > 0) | |
{ | |
if (readLine <= 0) break; | |
byte[] temp = new byte[readLine]; | |
Array.Copy(buffer, 0, temp, 0, readLine); | |
byte[] encrypt = provider.Encrypt(temp, false); | |
output.Write(encrypt, 0, encrypt.Length); | |
readLine = input.Read(buffer, 0, bufferSize); | |
} | |
input.Close(); | |
byte[] encryptedBytes = output.ToArray(); | |
output.Close(); | |
result = Convert.ToBase64String(encryptedBytes); | |
} | |
catch (Exception exception) | |
{ | |
throw exception; | |
} | |
return result; | |
} | |
/// <summary> | |
/// RSA Decryption | |
/// </summary> | |
public static string RSADecrypt(RSACryptoServiceProvider provider, string m_strDecryptString) | |
{ | |
string result = ""; | |
try | |
{ | |
int keySize = provider.KeySize / 8; byte[] buffer = new byte[keySize]; | |
byte[] EncryptDada = Convert.FromBase64String(m_strDecryptString); | |
using (MemoryStream input = new MemoryStream(EncryptDada)) | |
{ | |
using (MemoryStream output = new MemoryStream()) | |
{ | |
while (true) | |
{ | |
int readLine = input.Read(buffer, 0, keySize); | |
if (readLine <= 0) | |
{ | |
break; | |
} | |
byte[] temp = new byte[readLine]; | |
Array.Copy(buffer, 0, temp, 0, readLine); | |
byte[] decrypt = provider.Decrypt(temp, false); | |
output.Write(decrypt, 0, decrypt.Length); | |
} | |
result = Encoding.UTF8.GetString(output.ToArray()); | |
//result = new UnicodeEncoding().GetString(buffer2); | |
} | |
} | |
} | |
catch (Exception exception) | |
{ | |
throw exception; | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment