c#
RSA
RSACryptoServiceProvider
Visual Studio
2019 12 12
Console 程式示範:
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
class ConsoleRsaCryp
{
static void Main()
{
try
{
string oldData = "taiyonghai";
CreateRSAKey();
string ciphertext = RSAEncrypt(oldData);
string newData = RSADecrypt(ciphertext);
}
catch (ArgumentNullException)
{
//Catch this exception in case the encryption did
//not succeed.
Console.WriteLine("Encryption failed.");
}
Console.ReadKey();
}
/// <summary>
/// 建立RSA公鑰私鑰
/// </summary>
public static void CreateRSAKey()
{
//設定[公鑰私鑰]檔案路徑
string privateKeyPath = @"d:\\PrivateKey.xml";
string publicKeyPath = @"d:\\PublicKey.xml";
//建立RSA物件
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
//生成RSA[公鑰私鑰]
string privateKey = rsa.ToXmlString(true);
string publicKey = rsa.ToXmlString(false);
//將金鑰寫入指定路徑
File.WriteAllText(privateKeyPath, privateKey);//檔案內包含公鑰和私鑰
File.WriteAllText(publicKeyPath, publicKey);//檔案內只包含公鑰
}
/// <summary>
/// 使用RSA實現加密
/// </summary>
/// <param name="data">加密資料</param>
/// <returns></returns>
public static string RSAEncrypt(string data)
{
//C#預設只能使用[公鑰]進行加密(想使用[公鑰解密]可使用第三方元件BouncyCastle來實現)
string publicKeyPath = @"d:\\PublicKey.xml";
string publicKey = File.ReadAllText(publicKeyPath);
//建立RSA物件並載入[公鑰]
RSACryptoServiceProvider rsaPublic = new RSACryptoServiceProvider();
rsaPublic.FromXmlString(publicKey);
//對資料進行加密
byte[] publicValue = rsaPublic.Encrypt(Encoding.UTF8.GetBytes(data), false);
string publicStr = Convert.ToBase64String(publicValue);//使用Base64將byte轉換為string
return publicStr;
}
/// <summary>
/// 使用RSA實現解密
/// </summary>
/// <param name="data">解密資料</param>
/// <returns></returns>
public static string RSADecrypt(string data)
{
//C#預設只能使用[私鑰]進行解密(想使用[私鑰加密]可使用第三方元件BouncyCastle來實現)
string privateKeyPath = @"d:\\PrivateKey.xml";
string privateKey = File.ReadAllText(privateKeyPath);
//建立RSA物件並載入[私鑰]
RSACryptoServiceProvider rsaPrivate = new RSACryptoServiceProvider();
rsaPrivate.FromXmlString(privateKey);
//對資料進行解密
byte[] privateValue = rsaPrivate.Decrypt(Convert.FromBase64String(data), false);//使用Base64將string轉換為byte
string privateStr = Encoding.UTF8.GetString(privateValue);
return privateStr;
}
}
[time= 2019 11 07 ] 原文 & 參考: https://blog.miniasp.com/post/2019/01/02/Common-Regex-patterns-for-Unicode-characters https://unicode.org/charts/ UTF-8 相容於 ASCII 的文字範圍: 數字 (Numbers)[0-9]
Apr 8, 2020[time= 2019 11 08 ] 原文 & 參考: https://chentsulin.github.io/redux/docs/basics/UsageWithReact.html 使用 Mac 建置環境: 找一個你想建立此專案的位置 此範例目錄 ~/你的本機路徑/redux-base/
Apr 8, 2020[time= 2019 11 06 ] 原文 & 參考: https://www.bnext.com.tw/article/47401/chrome-screenshot 完整網頁長截圖 首先確保Chrome已升級至59或更高版本, 在想要截圖的網頁中,按下: Mac:
Apr 8, 2020or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up