الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

احتاج كود للتشفير

مغلق
بدأه عبد المعز في 6 ديسمبر 2006 · 8 رد · 818 مشاهدة · في Microsoft Visual Basic.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

احتاج خوارزمية تشفير وفك التشفير , او مكتبة جاهزة

#2

السلام عليكم

إليك أكثر من طريقة لشفير المعلومات ..حمل المرفق للمعاينة.

#Region " Encrypt & Decrypt "

	Public Sub encrypt()
		Try

			'define an 8-byte key
			Dim k() As Byte = {12, 13, 44, 22, 21, 44, 22, 128}

			'define an IV
			Dim Vector() As Byte = {2, 4, 46, 141, 8, 5, 99, 3}


			'Create a file stream
			Dim fs As New FileStream(Application.StartupPath & "\testFile.txt", _
			 FileMode.Create, FileAccess.Write)

			'create a byte array holding the message:
			Dim barray As Byte() = _
			(New UnicodeEncoding).GetBytes(txtResult.Text)

			Dim des As New DESCryptoServiceProvider
			'stuff the key & IV
			des.Key = k
			des.IV = Vector

			Dim desencrypt As ICryptoTransform = des.CreateEncryptor()
			'create a cryptostream, specifying DESencrypt
			'as the transform (the encrypting scheme) 
			Dim cryptostream As New CryptoStream _
			(fs, desencrypt, CryptoStreamMode.Write)

			'save the encrypted file to the hard drive
			cryptostream.Write(barray, 0, barray.Length)


			cryptostream.Close()
			Me.txtResult.Clear()


			'Reading Encryption To TEXT BOX
			Dim SR As New StreamReader(File.OpenRead( _
			Application.StartupPath & "\testFile.txt"))
			Do Until SR.Peek = -1
				txtResult.Text &= SR.ReadLine()
			Loop
			SR.Close()

		Catch ex As Exception
			MessageBox.Show(ex.Message)
		End Try

	End Sub


	Public Sub decrypt()

		'define an 8-byte key
		Dim k() As Byte = {12, 13, 44, 22, 21, 44, 22, 128}

		'define an IV
		Dim Vector() As Byte = {2, 4, 46, 141, 8, 5, 99, 3}


		Dim des As New DESCryptoServiceProvider
		'stuff the key & IV
		des.Key = k
		des.IV = Vector


		'NOW BRING IT BACK IN AND DECRYPT IT

		'create the input file stream 
		Dim fs As New FileStream(Application.StartupPath & "\testFile.txt", FileMode.Open, FileAccess.Read)

		'create a decryptor from the crypto service provider
		'  (uses same key assigned earlier) 
		Dim desdecrypt As ICryptoTransform = des.CreateDecryptor()

		'create a cryptostream for reading the file in, and decrypt its contents.
		Dim cryptostreamDecrypt As New CryptoStream _
		(fs, desdecrypt, CryptoStreamMode.Read)

		'display the result

		Me.txtResult.Text = CType(New StreamReader _
	   (cryptostreamDecrypt, New UnicodeEncoding).ReadToEnd(), String)

		cryptostreamDecrypt.Close()

	End Sub

#End Region

#Region " Encrypt & Decrypt With Hash "

	Public Sub encryptWithHash()

		'define an 8-byte key
		Dim k() As Byte = {12, 13, 44, 22, 21, 44, 22, 128}
		'define an IV
		Dim Vector() As Byte = {2, 4, 46, 141, 8, 5, 99, 2}

		'Create a file stream
		Dim fsOUT As New FileStream(Application.StartupPath & "\EH.txt", _
		FileMode.Create, FileAccess.Write)

		'create a byte array holding the plaintext message:
		Dim barray As Byte() = (New UnicodeEncoding).GetBytes _
		(txtEH.Text)

		'******* GET THE HASH VALUE OF THE PLAINTEXT BYTE ARRAY:
		Dim sha1 As SHA1 = Security.Cryptography.SHA1.Create()
		Dim hashValue() As Byte = sha1.ComputeHash(barray)

		Dim des As New DESCryptoServiceProvider
		'stuff the key
		des.Key = k
		des.IV = Vector

		Dim desencrypt As ICryptoTransform = des.CreateEncryptor()

		'********* SAVE ciphertext and hashvalue
		'create a cryptostream, specifying DESencrypt
		'  as the transform (the encrypting scheme) 
		Dim cryptostream As New CryptoStream(fsOUT, desencrypt, CryptoStreamMode.Write)

		'save the CIPHERTEXT to the hard drive
		cryptostream.Write(barray, 0, barray.Length)

		'append the hash value
		cryptostream.Write(hashValue, 0, hashValue.Length)
		cryptostream.Close()
		fsOUT.Close()

		'Reading to Text box
		txtEH.Clear()
		Dim SR As New StreamReader(File.OpenRead( _
		Application.StartupPath & "\EH.txt"))
		Do Until SR.Peek = -1
			txtEH.Text &= SR.ReadLine()
		Loop
		SR.Close()

	End Sub


	Public Sub decryptWithHash()

		'define an 8-byte key
		Dim k() As Byte = {12, 13, 44, 22, 21, 44, 22, 128}

		Dim des As New DESCryptoServiceProvider

		'define the IV
		Dim Vector() As Byte = {2, 4, 46, 141, 8, 5, 99, 2}

		'stuff the key and IV
		des.Key = k
		des.IV = Vector


		'NOW BRING THE MESSAGE BACK IN AND DECRYPT IT

		'create the input file stream 
		Dim fs As New FileStream(Application.StartupPath & "\EH.txt", FileMode.Open, FileAccess.Read)

		'create a decryptor from the crypto service
		'  provider (uses same key assigned earlier) 
		Dim desdecrypt As ICryptoTransform = des.CreateDecryptor()

		'create a cryptostream for reading the file in, and decrypt its contents.
		Dim cryptostreamDecrypt As New CryptoStream _
		(fs, desdecrypt, CryptoStreamMode.Read)

		'put the whole message into "m"
		Dim m As String = New StreamReader _
	   (cryptostreamDecrypt, New UnicodeEncoding).ReadToEnd()


		'now test the hash 

		' compute the hash value of everything but the hash in ms
		Dim sha1 As SHA1 = Security.Cryptography.SHA1.Create()
		'figure out the bytes needed 
		Dim hashSize As Integer = sha1.HashSize / 8

		'create byte array and fill with the entire
		'  restored plaintext plus the hash values
		Dim sArray As Byte() = (New UnicodeEncoding).GetBytes(m)

		'calculate the hash value of the entire message
		' (to compare with the transmitted one "messageHashValue")
		Dim hashValue() As Byte = sha1.ComputeHash(sArray, 0, sArray.Length - hashSize)


		Dim messageHashValue(hashSize - 1) As Byte
		Dim ms(sArray.Length - 1) As Object

		'extract the appended hash value from the message array
		Array.Copy(sArray, sArray.Length - hashSize, messageHashValue, 0, hashSize)
		Array.Copy(sArray, 0, ms, 0, sArray.Length - hashSize)

		'compute plaintext
		Dim s As String = String.Empty
		For i As Integer = 0 To ms.Length - 1 Step 2
			s &= Chr(ms(i))
		Next i

		' compare hashValue and msHashValue
		For i As Integer = 0 To hashSize - 1

			If messageHashValue(i) <> hashValue(i) Then
				MsgBox("There has been an intrusion. " & _
				"The hash values are not identical." & s)
				Exit Sub
			End If

		Next
		txtEH.Clear()
		txtEH.Text = s
		cryptostreamDecrypt.Close()
	End Sub

#End Region

#Region " Buttons Functionality "

	Private Sub btnEncr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncr.Click
		encrypt()
	End Sub

	Private Sub btnDecr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecr.Click
		decrypt()
	End Sub

	Private Sub btnDecrWHash_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecrWHash.Click
		decryptWithHash()
	End Sub

	Private Sub btnEncrWHash_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrWHash.Click
		encryptWithHash()
	End Sub

#End Region

EncAndDecrFileWithDES.rar

في المثال يتضح المقال

#3

السلام عليكم.

using System;
using System.Text;

namespace En{

 public class Encrypt
 {

  private static byte GetPassword(string Password)
  {

   byte[] Pass = Encoding.Unicode.GetBytes(Password);
   int sum = 0;

   foreach(byte P in Pass) {
	sum += P;
   }

   byte Result = Convert.ToByte(sum % ((int)255));
   Result ^= 255;

   return Result;
  }

  public static string Crypt(string ToBase64)
  {
   try {

	if(ToBase64 == "" || ToBase64 == null) 
	 throw new Exception("ToBase64");

	byte[] bytes = Encoding.Unicode.GetBytes(ToBase64);
	byte[] Result = new byte[bytes.Length + 1];
	byte Pass = GetPassword("\0");

	Array.Copy(bytes, 0, Result, 1, bytes.Length);

	Result[0] = Pass;

	for(int i = 1; i < Result.Length; i++) {
	 Result ^= Pass;
	}

	return Convert.ToBase64String(Result);

   } catch {
	return null;
   }
  }

  public static string Crypt(string Password, string ToBase64)
  {
   try {

	if(ToBase64 == "" || ToBase64 == null) 
	 throw new Exception("ToBase64");
	if(Password == "" || Password == null) 
	 throw new Exception("Password");

	byte[] bytes = Encoding.Unicode.GetBytes(ToBase64);
	byte[] Result = new byte[bytes.Length + 1];
	byte Pass = GetPassword(Password);

	Array.Copy(bytes, 0, Result, 1, bytes.Length);

	Result[0] = Pass;

	for(int I = 1; I < Result.Length; I++) {
	 Result ^= Pass;
	}

	return Convert.ToBase64String(Result);

   } catch {
	return null;
   }
  }

  public static string Decrypt(string FromBase64)
  {
   try {

	if(FromBase64 == "" || FromBase64 == null) 
	 throw new Exception("FromBase64");

	byte[] bytes = Convert.FromBase64String(FromBase64);
	byte[] Result = new byte[bytes.Length - 1];
	byte Pass = GetPassword("\0");

	// Check Password in Byte[0]
	if(bytes[0] != Pass) return null;

	Array.Copy(bytes, 1, Result, 0, Result.Length);

	for(int I = 0; I < Result.Length; I++) {
	 Result ^= Pass;
	}
	return Encoding.Unicode.GetString(Result);

   } catch {
	return null;
   }
  }

  public static string Decrypt(string Password, string FromBase64)
  {
   try {
	if(FromBase64 == "" || FromBase64 == null) throw new Exception("FromBase64");
	if(Password == "" || Password == null) throw new Exception("Password");

	byte[] bytes = Convert.FromBase64String(FromBase64);
	byte[] Result = new byte[bytes.Length - 1];
	byte Pass = GetPassword(Password);

	// Check Password in Byte[0]
	if(bytes[0] != Pass)
	 return null;

	Array.Copy(bytes, 1, Result, 0, Result.Length);

	for(int I = 0; I < Result.Length; I++) {
	 Result ^= Pass;
	}
	return Encoding.Unicode.GetString(Result);

   } catch {
	return null;
   }
  }
 }
}

مواضيع متنوعة

#4

اشكركم اخاوين على تفاعلكم

لكن اخي الغملاسي هذا المنهج يسير انه خطئ

Application.StartupPath

تم تعديل هذه المشاركة بواسطة عبد المعز في 6 ديسمبر 2006 في 19:39

#5

اخي اريد برنامج بدون التكست فيل لأني اريد تطبيقه على برنامج ويب وليس ديكتوب

ولا اظن المنهج Application.startuppath في asp.net

#6

السلام عليكم

أعتقد والله أعلم أن في ASP.NET ما يعرف بـ MapPath وهو شبيه بالـ StartupPath

على سبيل المثال

  Dim path As String = MapPath("uploadedImages\")

بالتوفيق إن شاء الله.

تم تعديل هذه المشاركة بواسطة الغملاسي في 6 ديسمبر 2006 في 23:12

في المثال يتضح المقال

#7

كيف يمكن تعديل البرنامج لكي يعمل بدون التكست فيل , لأنه اخاف السيرفر المستضيف لايسمح لي بإنشاء تكست فيل

يعني ادخل سترينج ويكون الناتج عبارة عن سترنج

تم تعديل هذه المشاركة بواسطة عبد المعز في 6 ديسمبر 2006 في 23:17

#8

السلام عليكم

في هذه الحالة يجب أن تحفظ قيمة التشفير داخل الـ Application Setting أو Machine.config.exe

لكن عدم حفظ قيمة التشفير سيأدي لعدم فك القيم المشفرة فيما بعد و قد تحدث ( كارثة تقينة )

توخى الحذر قبل المضي في تطبيق المشروع و اختبره جيداً مع توقع و اختبار جميع الاحتمالات .

السلام عليكم.

في المثال يتضح المقال

#9

اخي لم تفهم قصدي انا اريد ان اشفر نص واحفظه في قاعدة بيانات , اذا احتجته استخرجه من القاعدة وافك التشفير

الطريقة الذي وطعت تحتاج لانشاء ملف تسكت , انا اريد طريقة للتشفير بدون ملف الحاجة لإنشاء ملف تكست

هذا الموضوع مغلق.

مواضيع مشابهة