Encryption 带c的加密Js#
我正在学习加密和使用加密js我制作了一个js&c版本。 我试图实现的是,JS或c版本将能够解码彼此的消息 为了进行测试,我在JS和C#实例中保持了IV和键,填充和模式相同 我让他们分别对数据进行解密和加密,但我还没有完成的是提供一个加密的JS,以便能够使用c#解码 JS 在解密中出现问题的地方Encryption 带c的加密Js#,encryption,cryptography,aes,Encryption,Cryptography,Aes,我正在学习加密和使用加密js我制作了一个js&c版本。 我试图实现的是,JS或c版本将能够解码彼此的消息 为了进行测试,我在JS和C#实例中保持了IV和键,填充和模式相同 我让他们分别对数据进行解密和加密,但我还没有完成的是提供一个加密的JS,以便能够使用c#解码 JS 在解密中出现问题的地方 private void btn_Decrypt_Click(object sender, EventArgs e) { Console.WriteLine("Decrypti
private void btn_Decrypt_Click(object sender, EventArgs e)
{
Console.WriteLine("Decrypting..");
using (RijndaelManaged myRijndael = new RijndaelManaged())
{
//Settings
myRijndael.Mode = CipherMode.CBC;
myRijndael.Padding = PaddingMode.PKCS7;
myRijndael.FeedbackSize = 128;
keybytes = Encoding.UTF8.GetBytes("7061737323313233");
//Should be made unique for each message!. TODO
iv = Encoding.UTF8.GetBytes("7061737323313233");
// Decrypt the bytes to a string.
string roundtrip = DecryptToString(txt_Output.Text);
txt_Output.Text = roundtrip;
//Display the original data and the decrypted data.
}
}
public string DecryptToString(string TextValue)
{
return DecryptStringFromBytes(Convert.FromBase64String(TextValue), keybytes, iv);
}
static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("Key");
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an RijndaelManaged object
// with the specified key and IV.
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Key = Key;
rijAlg.IV = IV;
rijAlg.Padding = PaddingMode.PKCS7;
rijAlg.Mode = CipherMode.CBC;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt =
new CryptoStream(msDecrypt,decryptor,CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
private void btn\u Decrypt\u单击(对象发送方,事件参数e)
{
Console.WriteLine(“解密…”);
使用(RijndaelManaged myRijndael=new RijndaelManaged())
{
//背景
myRijndael.Mode=CipherMode.CBC;
myRijndael.Padding=PaddingMode.PKCS7;
myRijndael.FeedbackSize=128;
keybytes=Encoding.UTF8.GetBytes(“7061737323313233”);
//应使每个邮件都具有唯一性!.TODO
iv=Encoding.UTF8.GetBytes(“7061737323313233”);
//将字节解密为字符串。
字符串往返=解密字符串(txt_Output.Text);
txt_Output.Text=往返;
//显示原始数据和解密数据。
}
}
公共字符串DecryptoString(字符串TextValue)
{
返回DecryptStringFromBytes(Convert.FromBase64String(TextValue),keybytes,iv);
}
静态字符串解密StringFromBytes(字节[]密文,字节[]密钥,字节[]IV)
{
//检查参数。
如果(cipherText==null | | cipherText.Length基本上会遇到编码问题。首先,在一个实现中使用Base64解码解析IV,在另一个实现中使用直接字符编码。Base64字符串看起来也不像Base64字符串
此外,许多库(错误地)允许使用不正确的密钥和IV大小。但是,由于没有用于密钥或IV扩展的通用方法,这是令人困惑的。因此,您应该确保密钥和IV的二进制表示对于特定算法是正确的
对于AES,您应使用128、192或256位的密钥大小和与块大小相同的128位IV大小。IV应随机生成并与另一方通信,例如,通过在密文中添加IV前缀
public void startEncryption(string original )
{
using (RijndaelManaged myRijndael = new RijndaelManaged())
{
//Settings
myRijndael.Mode = CipherMode.CBC;
myRijndael.Padding = PaddingMode.PKCS7;
myRijndael.FeedbackSize = 128;
keybytes = Encoding.UTF8.GetBytes("7061737323313233");
//Should be made unique for each message!. TODO
iv = Encoding.UTF8.GetBytes("7061737323313233");
// Encrypt the string to an array of bytes.
encrypted = EncryptStringToBytes(original, keybytes, iv);
//Show Encrypted data
txt_Output.Text = Convert.ToBase64String(encrypted);
// Decrypt the bytes to a string.
string roundtrip = DecryptStringFromBytes(encrypted, keybytes, iv);
//Display the original data and the decrypted data.
Console.WriteLine("Original: {0}", original);
Console.WriteLine("Round Trip: {0}", roundtrip);
}
}
private void btn_Decrypt_Click(object sender, EventArgs e)
{
Console.WriteLine("Decrypting..");
using (RijndaelManaged myRijndael = new RijndaelManaged())
{
//Settings
myRijndael.Mode = CipherMode.CBC;
myRijndael.Padding = PaddingMode.PKCS7;
myRijndael.FeedbackSize = 128;
keybytes = Encoding.UTF8.GetBytes("7061737323313233");
//Should be made unique for each message!. TODO
iv = Encoding.UTF8.GetBytes("7061737323313233");
// Decrypt the bytes to a string.
string roundtrip = DecryptToString(txt_Output.Text);
txt_Output.Text = roundtrip;
//Display the original data and the decrypted data.
}
}
public string DecryptToString(string TextValue)
{
return DecryptStringFromBytes(Convert.FromBase64String(TextValue), keybytes, iv);
}
static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("Key");
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an RijndaelManaged object
// with the specified key and IV.
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Key = Key;
rijAlg.IV = IV;
rijAlg.Padding = PaddingMode.PKCS7;
rijAlg.Mode = CipherMode.CBC;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt =
new CryptoStream(msDecrypt,decryptor,CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}