PDA

View Full Version : سوال: کلاس رمزگزاری و رمزگشایی AES نوشته شده توسط مایکروسافت



daniyaltjm
یک شنبه 25 مهر 1395, 22:13 عصر
سلام، دوستان آیا خود مایکروسافت برای دات نت کلاسی برای این کار داره که برای رمز کردن رشته فقط یک رشته بگیرد و رمز شده تحول بده یا یک فایل بهش بدهیم و رمز شده برگردونه؟ که دیگه کاربر رو درگیر تبدیل داده به بایت و ... نکنه؟ :متفکر:

juza66
یک شنبه 25 مهر 1395, 22:27 عصر
سلام


این using کن


using System.Security.Cryptography;


اینم کد کاملش


using System;
using System.IO;
using System.Security.Cryptography;

namespace RijndaelManaged_Example
{
class RijndaelExample
{
public static void Main()
{
try
{

string original = "Here is some data to encrypt!";

// Create a new instance of the RijndaelManaged
// class. This generates a new key and initialization
// vector (IV).
using (RijndaelManaged myRijndael = new RijndaelManaged())
{

myRijndael.GenerateKey();
myRijndael.GenerateIV();
// Encrypt the string to an array of bytes.
byte[] encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);

// Decrypt the bytes to a string.
string roundtrip = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);

//Display the original data and the decrypted data.
Console.WriteLine("Original: {0}", original);
Console.WriteLine("Round Trip: {0}", roundtrip);
}

}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
}
}
static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;
// Create an RijndaelManaged object
// with the specified key and IV.
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Key = Key;
rijAlg.IV = IV;

// Create a decryptor to perform the stream transform.
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{

//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}


// Return the encrypted bytes from the memory stream.
return encrypted;

}

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("IV");

// 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;

// 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;

}
}
}



اینم منبعش:
http://stackoverflow.com/questions/273452/using-aes-encryption-in-c-sharp

juza66
یک شنبه 25 مهر 1395, 22:30 عصر
یکبار هم در اینجا آموزش استفاده اش رو واس پوشه ها گفته بودم

http://barnamenevis.org/showthread.php?423711-%D9%82%D9%81%D9%84-%DA%A9%D8%B1%D8%AF%D9%86-%D9%81%D8%A7%DB%8C%D9%84-%D9%87%D8%A7-%D8%A8%D8%A7-%D8%A7%D9%84%DA%AF%D9%88%D8%B1%DB%8C%D8%AA%D9%85-Rijndael&highlight=Rijndael

juza66
یک شنبه 25 مهر 1395, 22:34 عصر
جدایی رمزنگاری خود سی شارپ

میتونی از منبع زیر کلاس رو برای خودت یعنی استفاده از رمزنگاری AES استفاده کنی
https://msdn.microsoft.com/en-us/library/system.security.cryptography.aes(v=vs.110).aspx

اینا رو using کن

System.Object
System.Security.Cryptography.SymmetricAlgorithm
System.Security.Cryptography.Aes
System.Security.Cryptography.AesCng
System.Security.Cryptography.AesCryptoServiceProvi der
System.Security.Cryptography.AesManaged

اینم کد کاملش

using System;
using System.IO;
using System.Security.Cryptography;

namespace Aes_Example
{
class AesExample
{
public static void Main()
{
try
{

string original = "Here is some data to encrypt!";

// Create a new instance of the Aes
// class. This generates a new key and initialization
// vector (IV).
using (Aes myAes = Aes.Create())
{

// Encrypt the string to an array of bytes.
byte[] encrypted = EncryptStringToBytes_Aes(original, myAes.Key, myAes.IV);

// Decrypt the bytes to a string.
string roundtrip = DecryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV);

//Display the original data and the decrypted data.
Console.WriteLine("Original: {0}", original);
Console.WriteLine("Round Trip: {0}", roundtrip);
}

}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
}
}
static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV;

// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{

//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}


// Return the encrypted bytes from the memory stream.
return encrypted;

}

static string DecryptStringFromBytes_Aes(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("IV");

// Declare the string used to hold
// the decrypted text.
string plaintext = null;

// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV;

// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.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 decryptingstream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}

}

return plaintext;

}
}
}

daniyaltjm
یک شنبه 25 مهر 1395, 22:34 عصر
سلام


این using کن


using System.Security.Cryptography;


اینم کد کاملش


using System;
using System.IO;
using System.Security.Cryptography;

namespace RijndaelManaged_Example
{
class RijndaelExample
{
public static void Main()
{
try
{

string original = "Here is some data to encrypt!";

// Create a new instance of the RijndaelManaged
// class. This generates a new key and initialization
// vector (IV).
using (RijndaelManaged myRijndael = new RijndaelManaged())
{

myRijndael.GenerateKey();
myRijndael.GenerateIV();
// Encrypt the string to an array of bytes.
byte[] encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);

// Decrypt the bytes to a string.
string roundtrip = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);

//Display the original data and the decrypted data.
Console.WriteLine("Original: {0}", original);
Console.WriteLine("Round Trip: {0}", roundtrip);
}

}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
}
}
static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;
// Create an RijndaelManaged object
// with the specified key and IV.
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Key = Key;
rijAlg.IV = IV;

// Create a decryptor to perform the stream transform.
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{

//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}


// Return the encrypted bytes from the memory stream.
return encrypted;

}

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("IV");

// 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;

// 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;

}
}
}



اینم منبعش:
http://stackoverflow.com/questions/273452/using-aes-encryption-in-c-sharp

ممنون دوست عزیز فکر کنم درست متوجه نشدین !!!!! اینجا شما یک کلاس ساختین خوب من یک کلاس دیگه از اینجا (http://www.codeproject.com/Articles/769741/Csharp-AES-bits-Encryption-Library-with-Salt) تست کردم جواب داد کدشم خیلی کمتره !!!! سوال من ایین بود که آیا مایکروسافت یک کلاس مخصوص این ار قرار نداده که خیلی راحتر فقط یک متن یا یک فایل بهش بدیم و عملیات تبدیل به بایت رو خودش اتوماتیک انجام بده و کاربر رو درگیر نکنه و با استفاده از متد هاش بشه خیلی راحتر باهاش کار کرد؟ :متفکر: حتما بااید باشه!

juza66
یک شنبه 25 مهر 1395, 22:35 عصر
این یک توضیح از یکی دوستان:

لگوریتم AES ، الگوریتمی است برای رمزنگاری که به روش متقارن کار میکنه ، یعنی با کلیدی که داده ها رمز میشن ، با همون کلید داده ها زمزگشایی میشن ،
این الگوریتم در واقع همان الگوریتم Rijndael هست که بعد از استاندارد سازی به نام AES (Advanced Encryption Standard) تغییر نام داد...
فضای کلید رمزنگاری این الگوریتم 128 بیتی هست و به همین دلیل دور زدن این الگورتم را کاری بسیار سخت میکنه ...
این الگوریتم برای جایگزینی الگوریتم DES ارائه داده شد ، به این دلیل که الگوریتم DES توسط دولت فدرال آمریکا به همراه IBM ابداع شده بود و حس میشد دارای back door می تواند باشد...
برای پیاده سازی رمز نگاری در دات نت می تونید لینک زیر را مطالعه کنید :
http://barnamenevis.org/showthread.php?381814-%D8%B1%D9%85%D8%B2-%D9%86%DA%AF%D8%A7%D8%B1%DB%8C-%D9%85%D8%AA%D9%82%D8%A7%D8%B1%D9%86-%D8%AF%D8%B1-%D8%AF%D8%A7%D8%AA-%D9%86%D8%AA-%D8%A8%D9%87-%DA%A9%D9%85%DA%A9-C

juza66
یک شنبه 25 مهر 1395, 22:47 عصر
این رو نمیدونم .... :لبخند:

ولی میدونم واس استفاده از این کلاس ها باید از رفرنس های خود مایکروسافت استفاده بشه ..
کلاس رو براش بساز و رشته ات رو براش پاس بده برای رمزنگاری و برعکس ..


و اگر منظورت اینه که شما با یک خط کد رشته ات رو به AES رمزنگاری کنی فکر نکنم بشه. با روش بالا که گفتم کلاس رو درون پروژه ات بساز و پاس بده رشته ات رو .. میشه ولی حالا اساتید و دوستان اگر اطلاعاتی دارن بگن ماهم بدونیم.

daniyaltjm
یک شنبه 25 مهر 1395, 23:05 عصر
این رو نمیدونم .... :لبخند:

ولی میدونم واس استفاده از این کلاس ها باید از رفرنس های خود مایکروسافت استفاده بشه ..
کلاس رو براش بساز و رشته ات رو براش پاس بده برای رمزنگاری و برعکس ..


و اگر منظورت اینه که شما با یک خط کد رشته ات رو به AES رمزنگاری کنی فکر نکنم بشه. با روش بالا که گفتم کلاس رو درون پروژه ات بساز و پاس بده رشته ات رو .. میشه ولی حالا اساتید و دوستان اگر اطلاعاتی دارن بگن ماهم بدونیم.

حالا نه با یک خط با دو خط :لبخند: یکی برای نمونه ساختن از کلاسش و یکی هم برای استفاده از متد هاش!!
ولی در کل اگه هم همچین امکانی نبود خوب یکی از اساتید لطف کنه کارایی رو که الگوریتم AES انجام میده رو دقیق بررسی کنه چون هر جا گشتم فقط طرز استفاده ازش بود. ممنون

juza66
یک شنبه 25 مهر 1395, 23:14 عصر
سلام یک سرچ زدم

شما این رو اضاف کن

using System.Security.Cryptography.Aes



اینجوری کلاسش رو صدا کن

AesManaged myAes = new AesManaged();

اینجوری استفاده کن



myAes.Mode = CipherMode.ECB;
myAes.IV = new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
myAes.Key = CipherKey;
myAes.Padding = PaddingMode.None;

ICryptoTransform encryptor = myAes.CreateEncryptor();

daniyaltjm
دوشنبه 26 مهر 1395, 00:55 صبح
سلام یک سرچ زدم

شما این رو اضاف کن

using System.Security.Cryptography.Aes



اینجوری کلاسش رو صدا کن

AesManaged myAes = new AesManaged();

اینجوری استفاده کن



myAes.Mode = CipherMode.ECB;
myAes.IV = new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
myAes.Key = CipherKey;
myAes.Padding = PaddingMode.None;

ICryptoTransform encryptor = myAes.CreateEncryptor();

ممنون، خوبه حالا منبع رو هم بزارید لطفا .
بعد اگر میشه تک تک این دستورات رو یه توضیح بدین (قسمت های آخر ) که اصلا ECB چیه IV چیه و.... IV هم توی هک وایفای برای الپوریتم WEP استفاده میشه که وقتی با Aircrack-ng وقتی می خوایم هک کنیم میگه حداقال 5000 IV نیاز داره حالا نکنه این الگوریتم هم امنیت پایینی داشته باشه؟ اصلا تعداد بیت هاش چنده؟ 256 بیتی چطور بزاریم :متفکر: