这个例子:https://docs.microsoft.com/en-us/dotnet/standard/security/walkthrough-creating-a-cryptographic-application
发现#NKCSS非常好。我用它构建了一个测试应用程序,通过了安全官员的代码审查。
仅需从示例中复制相关部分,以防链接发生更改:
// Declare global objects
//
// Declare CspParmeters and RsaCryptoServiceProvider
// objects with global scope of your Form class.
readonly CspParameters _cspp = new CspParameters();
RSACryptoServiceProvider _rsa;
// Path variables for source, encryption, and
// decryption folders. Must end with a backslash.
const string EncrFolder = @"c:\Encrypt\";
const string DecrFolder = @"c:\Decrypt\";
const string SrcFolder = @"c:\docs\";
// Public key file
const string PubKeyFile = @"c:\encrypt\rsaPublicKey.txt";
// Key container name for
// private/public key value pair.
const string KeyName = "Key01";
private void buttonCreateAsmKeys_Click(object sender, EventArgs e)
{
// Stores a key pair in the key container.
_cspp.KeyContainerName = KeyName;
_rsa = new RSACryptoServiceProvider(_cspp)
{
PersistKeyInCsp = true
};
label1.Text = _rsa.PublicOnly
? $"Key: {_cspp.KeyContainerName} - Public Only"
: $"Key: {_cspp.KeyContainerName} - Full Key Pair";
}
private void buttonEncryptFile_Click(object sender, EventArgs e)
{
if (_rsa is null)
{
MessageBox.Show("Key not set.");
}
else
{
// Display a dialog box to select a file to encrypt.
_encryptOpenFileDialog.InitialDirectory = SrcFolder;
if (_encryptOpenFileDialog.ShowDialog() == DialogResult.OK)
{
string fName = _encryptOpenFileDialog.FileName;
if (fName != null)
{
// Pass the file name without the path.
EncryptFile(new FileInfo(fName));
}
}
}
}
// Add the following EncryptFile method to the form.
private void EncryptFile(FileInfo file)
{
// Create instance of Aes for
// symmetric encryption of the data.
Aes aes = Aes.Create();
ICryptoTransform transform = aes.CreateEncryptor();
// Use RSACryptoServiceProvider to
// encrypt the AES key.
// rsa is previously instantiated:
// rsa = new RSACryptoServiceProvider(cspp);
byte[] keyEncrypted = _rsa.Encrypt(aes.Key, false);
// Create byte arrays to contain
// the length values of the key and IV.
int lKey = keyEncrypted.Length;
byte[] LenK = BitConverter.GetBytes(lKey);
int lIV = aes.IV.Length;
byte[] LenIV = BitConverter.GetBytes(lIV);
// Write the following to the FileStream
// for the encrypted file (outFs):
// - length of the key
// - length of the IV
// - ecrypted key
// - the IV
// - the encrypted cipher content
// Change the file's extension to ".enc"
string outFile =
Path.Combine(EncrFolder, Path.ChangeExtension(file.Name, ".enc"));
using (var outFs = new FileStream(outFile, FileMode.Create))
{
outFs.Write(LenK, 0, 4);
outFs.Write(LenIV, 0, 4);
outFs.Write(keyEncrypted, 0, lKey);
outFs.Write(aes.IV, 0, lIV);
// Now write the cipher text using
// a CryptoStream for encrypting.
using (var outStreamEncrypted =
new CryptoStream(outFs, transform, CryptoStreamMode.Write))
{
// By encrypting a chunk at
// a time, you can save memory
// and accommodate large files.
int count = 0;
int offset = 0;
// blockSizeBytes can be any arbitrary size.
int blockSizeBytes = aes.BlockSize / 8;
byte[] data = new byte[blockSizeBytes];
int bytesRead = 0;
using (var inFs = new FileStream(file.FullName, FileMode.Open))
{
do
{
count = inFs.Read(data, 0, blockSizeBytes);
offset += count;
outStreamEncrypted.Write(data, 0, count);
bytesRead += blockSizeBytes;
} while (count > 0);
}
outStreamEncrypted.FlushFinalBlock();
}
}
}
// Then to Decrypt a file -
private void buttonDecryptFile_Click(object sender, EventArgs e)
{
if (_rsa is null)
{
MessageBox.Show("Key not set.");
}
else
{
// Display a dialog box to select the encrypted file.
_decryptOpeFileDialog.InitialDirectory = EncrFolder;
if (_decryptOpeFileDialog.ShowDialog() == DialogResult.OK)
{
string fName = _decryptOpeFileDialog.FileName;
if (fName != null)
{
DecryptFile(new FileInfo(fName));
}
}
}
}
// And -
private void DecryptFile(FileInfo file)
{
// Create instance of Aes for
// symmetric decryption of the data.
Aes aes = Aes.Create();
// Create byte arrays to get the length of
// the encrypted key and IV.
// These values were stored as 4 bytes each
// at the beginning of the encrypted package.
byte[] LenK = new byte[4];
byte[] LenIV = new byte[4];
// Construct the file name for the decrypted file.
string outFile =
Path.ChangeExtension(file.FullName.Replace("Encrypt", "Decrypt"), ".txt");
// Use FileStream objects to read the encrypted
// file (inFs) and save the decrypted file (outFs).
using (var inFs = new FileStream(file.FullName, FileMode.Open))
{
inFs.Seek(0, SeekOrigin.Begin);
inFs.Read(LenK, 0, 3);
inFs.Seek(4, SeekOrigin.Begin);
inFs.Read(LenIV, 0, 3);
// Convert the lengths to integer values.
int lenK = BitConverter.ToInt32(LenK, 0);
int lenIV = BitConverter.ToInt32(LenIV, 0);
// Determine the start postition of
// the ciphter text (startC)
// and its length(lenC).
int startC = lenK + lenIV + 8;
int lenC = (int)inFs.Length - startC;
// Create the byte arrays for
// the encrypted Aes key,
// the IV, and the cipher text.
byte[] KeyEncrypted = new byte[lenK];
byte[] IV = new byte[lenIV];
// Extract the key and IV
// starting from index 8
// after the length values.
inFs.Seek(8, SeekOrigin.Begin);
inFs.Read(KeyEncrypted, 0, lenK);
inFs.Seek(8 + lenK, SeekOrigin.Begin);
inFs.Read(IV, 0, lenIV);
Directory.CreateDirectory(DecrFolder);
// Use RSACryptoServiceProvider
// to decrypt the AES key.
byte[] KeyDecrypted = _rsa.Decrypt(KeyEncrypted, false);
// Decrypt the key.
ICryptoTransform transform = aes.CreateDecryptor(KeyDecrypted, IV);
// Decrypt the cipher text from
// from the FileSteam of the encrypted
// file (inFs) into the FileStream
// for the decrypted file (outFs).
using (var outFs = new FileStream(outFile, FileMode.Create))
{
int count = 0;
int offset = 0;
// blockSizeBytes can be any arbitrary size.
int blockSizeBytes = aes.BlockSize / 8;
byte[] data = new byte[blockSizeBytes];
// By decrypting a chunk a time,
// you can save memory and
// accommodate large files.
// Start at the beginning
// of the cipher text.
inFs.Seek(startC, SeekOrigin.Begin);
using (var outStreamDecrypted =
new CryptoStream(outFs, transform, CryptoStreamMode.Write))
{
do
{
count = inFs.Read(data, 0, blockSizeBytes);
offset += count;
outStreamDecrypted.Write(data, 0, count);
} while (count > 0);
outStreamDecrypted.FlushFinalBlock();
}
}
}
}
// you can also try to Export a public key:
void buttonExportPublicKey_Click(object sender, EventArgs e)
{
// Save the public key created by the RSA
// to a file. Caution, persisting the
// key to a file is a security risk.
Directory.CreateDirectory(EncrFolder);
using (var sw = new StreamWriter(PubKeyFile, false))
{
sw.Write(_rsa.ToXmlString(false));
}
}
// or Import a public key
void buttonImportPublicKey_Click(object sender, EventArgs e)
{
using (var sr = new StreamReader(PubKeyFile))
{
_cspp.KeyContainerName = KeyName;
_rsa = new RSACryptoServiceProvider(_cspp);
string keytxt = sr.ReadToEnd();
_rsa.FromXmlString(keytxt);
_rsa.PersistKeyInCsp = true;
label1.Text = _rsa.PublicOnly
? $"Key: {_cspp.KeyContainerName} - Public Only"
: $"Key: {_cspp.KeyContainerName} - Full Key Pair";
}
}
// (Get a private key)
private void buttonGetPrivateKey_Click(object sender, EventArgs e)
{
_cspp.KeyContainerName = KeyName;
_rsa = new RSACryptoServiceProvider(_cspp)
{
PersistKeyInCsp = true
};
label1.Text = _rsa.PublicOnly
? $"Key: {_cspp.KeyContainerName} - Public Only"
: $"Key: {_cspp.KeyContainerName} - Full Key Pair";
}
注意,您可能希望从使用AES切换,因为这只是一个代码示例(由微软),并且还考虑替换RijnDayl类的使用进行加密。
代码示例:
private static string _privateKey;
private static string _publicKey;
private static UnicodeEncoding _encoder = new UnicodeEncoding();
private static void RSA()
{
var rsa = new RSACryptoServiceProvider();
_privateKey = rsa.ToXmlString(true);
_publicKey = rsa.ToXmlString(false);
var text = "Test1";
Console.WriteLine("RSA // Text to encrypt: " + text);
var enc = Encrypt(text);
Console.WriteLine("RSA // Encrypted Text: " + enc);
var dec = Decrypt(enc);
Console.WriteLine("RSA // Decrypted Text: " + dec);
}
public static string Decrypt(string data)
{
var rsa = new RSACryptoServiceProvider();
var dataArray = data.Split(new char[] { ',' });
byte[] dataByte = new byte[dataArray.Length];
for (int i = 0; i < dataArray.Length; i++)
{
dataByte[i] = Convert.ToByte(dataArray[i]);
}
rsa.FromXmlString(_privateKey);
var decryptedByte = rsa.Decrypt(dataByte, false);
return _encoder.GetString(decryptedByte);
}
public static string Encrypt(string data)
{
var rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(_publicKey);
var dataToEncrypt = _encoder.GetBytes(data);
var encryptedByteArray = rsa.Encrypt(dataToEncrypt, false).ToArray();
var length = encryptedByteArray.Count();
var item = 0;
var sb = new StringBuilder();
foreach (var x in encryptedByteArray)
{
item++;
sb.Append(x);
if (item < length)
sb.Append(",");
}
return sb.ToString();
}
我找到了几个可以使用的解决方案。Net RSA Provider使用公钥对消息进行加密,并使用私钥对其解密。 但我想要的是用私钥加密,用公钥解密。 我希望在我的应用程序中存储公钥,并使用私钥加密许可证,例如在我的开发人员计算机上,将其发送到应用程序,并让信息使用公钥解密。 我怎样才能做到这一点?
我需要在C#中加密数据,以便将其传递给Java。Java代码属于第三方,但我得到了相关的源代码,因此我决定,由于Java使用Bouncy Castle库,所以我将使用C#端口。 解密工作正常。但是,解密仅在使用私钥使用encrypt时有效,而不是使用公钥。使用公钥时,解密失败,出现。 编辑: 我还添加了一个单元测试,它证明公钥等于从私钥中提取的公钥:
我想使用带有RSA算法的OpenSSL使用私钥加密文件: 现在,如果我执行解密操作: 此操作需要私钥 我知道我应该使用公钥进行加密,如果我使用私钥,我会得到一个签名。 然而,我想这样做是为了学习。
并且我将这个函数称为用RSA公钥加密DSA密钥的函数:
问题内容: 关于AES 256加密: 什么是公钥和私钥? 如何生成这两个密钥? 如何使用公众加密数据? 如何使用私有解密数据? 问题答案: 在.Net中,您可以这样创建密钥对: 然后,您可以使用公共密钥对消息进行加密,如下所示: 并使用私钥像这样解密:
我有以下代码。 当我使用rsacyncrypt方法时,它采用参数“RSAKeyInfo”(用于加密的公钥和用于解密的私钥)。 如何获取此方法用于加密和解密的私钥和公钥的值。 谢谢