private static string EncryptString(string key, string plainText) { byte[] iv = new byte[16]; byte[] array; using (Aes aes = Aes.Create()) { aes.Key = Encoding.UTF8.GetBytes(key); aes.IV = iv; ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV); using (MemoryStream memoryStream = new MemoryStream()) { using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, encryptor, CryptoStreamMode.Write)) { using (StreamWriter streamWriter = new StreamWriter((Stream)cryptoStream)) { streamWriter.Write(plainText); } array = memoryStream.ToArray(); } } } return Convert.ToBase64String(array); }
时间: 2024-02-10 07:27:41 浏览: 45
这段代码是用于加密字符串的,使用的加密算法是AES(高级加密标准)。下面是对代码的解释:
1. 创建一个长度为16的字节数组iv,用于设置AES算法的初始化向量。
2. 使用Aes.Create()方法创建一个Aes对象。
3. 将传入的key转换为UTF-8编码的字节数组,并将其赋值给Aes对象的Key属性。
4. 将初始化向量iv赋值给Aes对象的IV属性。
5. 使用Aes对象的CreateEncryptor方法创建一个加密器对象encryptor,用于执行加密操作。
6. 创建一个内存流对象memoryStream,用于存储加密后的数据。
7. 创建一个CryptoStream对象cryptoStream,将内存流与加密器和写入模式关联起来,用于实现数据的加密。
8. 创建一个StreamWriter对象streamWriter,将CryptoStream对象作为参数传入,用于将明文写入加密流。
9. 将明文写入加密流后,关闭StreamWriter对象。
10. 将内存流的数据转换为字节数组并赋值给array。
11. 将字节数组array转换为Base64字符串,并返回结果。
此方法使用给定的key和明文,通过AES算法进行加密,并将结果以Base64字符串的形式返回。请注意,这段代码只提供了加密功能,并没有提供解密功能。
相关问题
. Rijndael算法
Rijndael算法是一种高级加密标准(AES)的候选算法,它是由美国国家标准与技术协会(NIST)所选定的。该算法在密码学中被广泛应用于数据加密和解密的过程中。Rijndael算法是一种对称密钥算法,意味着加密和解密使用相同的密钥。它可以用于保护敏感数据的机密性,确保数据在传输和存储过程中不被未经授权的人访问。
Rijndael算法的实现可以使用不同的编程语言,例如C#。下面是一个使用C#实现Rijndael算法的示例:
```csharp
using System;
using System.Security.Cryptography;
using System.Text;
public class RijndaelExample
{
public static void Main()
{
string plainText = "Hello, World!";
string key = "ThisIsARijndaelKey";
string iv = "ThisIsARijndaelIV";
byte[] encryptedBytes = EncryptString(plainText, key, iv);
string encryptedText = Convert.ToBase64String(encryptedBytes);
Console.WriteLine("Encrypted Text: " + encryptedText);
string decryptedText = DecryptString(encryptedBytes, key, iv);
Console.WriteLine("Decrypted Text: " + decryptedText);
}
public static byte[] EncryptString(string plainText, string key, string iv)
{
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
using (RijndaelManaged rijndael = new RijndaelManaged())
{
rijndael.Key = keyBytes;
rijndael.IV = ivBytes;
ICryptoTransform encryptor = rijndael.CreateEncryptor(rijndael.Key, rijndael.IV);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter sw = new StreamWriter(cs))
{
sw.Write(plainText);
}
}
return ms.ToArray();
}
}
}
public static string DecryptString(byte[] encryptedBytes, string key, string iv)
{
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
using (RijndaelManaged rijndael = new RijndaelManaged())
{
rijndael.Key = keyBytes;
rijndael.IV = ivBytes;
ICryptoTransform decryptor = rijndael.CreateDecryptor(rijndael.Key, rijndael.IV);
using (MemoryStream ms = new MemoryStream(encryptedBytes))
{
using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
{
using (StreamReader sr = new StreamReader(cs))
{
return sr.ReadToEnd();
}
}
}
}
}
}
```
这个示例演示了如何使用C#编写的Rijndael算法进行文本加密和解密。在示例中,我们使用了一个密钥和一个初始化向量(IV)来加密和解密文本。加密后的文本以Base64字符串的形式进行存储和传输。
用vb.net基于Windows序列号的软件加密技术代码
以下是一个简单的示例代码,可以基于 Windows 序列号实现软件加密:
```vb.net
'获取Windows序列号
Dim wmi As Object = GetObject("winmgmts:")
Dim sn As String = ""
For Each item In wmi.InstancesOf("Win32_OperatingSystem")
sn = item.SerialNumber
Next
'使用序列号生成加密密钥
Dim key As String = "mysecretkey" + sn
'使用密钥对数据进行加密
Dim plainText As String = "Hello world"
Dim encryptedText As String = EncryptString(plainText, key)
'使用密钥对数据进行解密
Dim decryptedText As String = DecryptString(encryptedText, key)
'Show结果
Console.WriteLine("Plain Text: " + plainText)
Console.WriteLine("Encrypted Text: " + encryptedText)
Console.WriteLine("Decrypted Text: " + decryptedText)
'加密函数
Private Function EncryptString(ByVal plainText As String, ByVal key As String) As String
Dim plainBytes As Byte() = Encoding.UTF8.GetBytes(plainText)
Dim keyBytes As Byte() = Encoding.UTF8.GetBytes(key)
Dim cryptoProvider As New DESCryptoServiceProvider
cryptoProvider.Mode = CipherMode.ECB
cryptoProvider.Padding = PaddingMode.PKCS7
Dim memoryStream As New MemoryStream
Dim cryptoStream As New CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(keyBytes, keyBytes), CryptoStreamMode.Write)
cryptoStream.Write(plainBytes, 0, plainBytes.Length)
cryptoStream.FlushFinalBlock()
Return Convert.ToBase64String(memoryStream.ToArray)
End Function
'解密函数
Private Function DecryptString(ByVal encryptedText As String, ByVal key As String) As String
Dim encryptedBytes As Byte() = Convert.FromBase64String(encryptedText)
Dim keyBytes As Byte() = Encoding.UTF8.GetBytes(key)
Dim cryptoProvider As New DESCryptoServiceProvider
cryptoProvider.Mode = CipherMode.ECB
cryptoProvider.Padding = PaddingMode.PKCS7
Dim memoryStream As New MemoryStream(encryptedBytes)
Dim cryptoStream As New CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(keyBytes, keyBytes), CryptoStreamMode.Read)
Dim plainBytes(encryptedBytes.Length - 1) As Byte
Dim decryptedByteCount As Integer = cryptoStream.Read(plainBytes, 0, encryptedBytes.Length)
Return Encoding.UTF8.GetString(plainBytes, 0, decryptedByteCount)
End Function
```
以上代码使用 DES 对称加密算法进行加密,密钥由自定义的字符串和 Windows 序列号拼接而成。实际应用中,为了更好的安全性,可以使用更高强度的加密算法和更复杂的密钥生成算法。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)