18-11-14, 08:35 PM
السلام عليكم ورحمة الله وبركاته
انا اعمل على خوارزمية RC6 ,وهي خوارزمية جميلة وسريعة جدا
هذه صورة الخوارزمية
صنعت فنكشنات كثيرة وفقط اقوم بالاستدعاء لهذه الفنكشنات حتى يسهل العمل
فنكشن لتوليد المفاتيح
فنكشن تحويل البايتات الى ورد
فنكشن التدوير لليمين واليسار
فنكشن حشو النص اذا كان ناقص
فنكشن تشفير وفك شفرة بلوك واحد
فنكشن تشفير وفك شفرة اكثر من بلوك
---
لكن المشكلة عندي بالاستدعاء داخل زر البتوم
كل مرة اكتب الاستداعء يكون عندي مشاكل مو مشكلة وحدة
هذا هو التصميم الذي اريده ان يعمل
يعني بالتكست بوك الاول يدخل النص المشفر وعند الضغط على زر ENCRYPTION
يتشفر النص ويظهر الناتج بزر التكست بوكس الثاني وعند الضغط على زر DECRYPTION
يعود النص الاصلي في التكست بوك الثالث
هذا هو الكود كامل للخوارزمية
اذا الكود فيه خلل ,, ممكن مساعدة في تصحيحه
او اذا اي احد عندة كود جاهز لهذه الخوارزمية بالفجول دوت نت ممكن المساعدة
انا اعمل على خوارزمية RC6 ,وهي خوارزمية جميلة وسريعة جدا
هذه صورة الخوارزمية
صنعت فنكشنات كثيرة وفقط اقوم بالاستدعاء لهذه الفنكشنات حتى يسهل العمل
فنكشن لتوليد المفاتيح
فنكشن تحويل البايتات الى ورد
فنكشن التدوير لليمين واليسار
فنكشن حشو النص اذا كان ناقص
فنكشن تشفير وفك شفرة بلوك واحد
فنكشن تشفير وفك شفرة اكثر من بلوك
---
لكن المشكلة عندي بالاستدعاء داخل زر البتوم
كل مرة اكتب الاستداعء يكون عندي مشاكل مو مشكلة وحدة
هذا هو التصميم الذي اريده ان يعمل
يعني بالتكست بوك الاول يدخل النص المشفر وعند الضغط على زر ENCRYPTION
يتشفر النص ويظهر الناتج بزر التكست بوكس الثاني وعند الضغط على زر DECRYPTION
يعود النص الاصلي في التكست بوك الثالث
هذا هو الكود كامل للخوارزمية
كود :
Public Class Form1
Private Shared w As Integer = 32, r As Integer = 20
Private Shared ReadOnly e As Double = Math.E
Private Const goldenRatio As Double = 1.61803398874965
Private Shared Pw As Integer = CInt(&HB7E15163), Qw As Integer = CInt(&H9E3779B9)
Private Shared S As Integer()
Private Shared Function convBytesWords(key As SByte(), u As Integer, c As Integer) As Integer()
Dim tmp As Integer() = New Integer(c - 1) {}
Dim i As Integer
For i = 0 To tmp.Length - 1
tmp(i) = 0
Next
i = 0
Dim off As Integer = 0
While i < c
tmp(i) = ((key(System.Math.Max(System.Threading.Interlocked.Increment(off), off - 1)) And &HFF)) Or ((key(System.Math.Max(System.Threading.Interlocked.Increment(off), off - 1)) And &HFF) << 8) Or ((key(System.Math.Max(System.Threading.Interlocked.Increment(off), off - 1)) And &HFF) << 16) Or ((key(System.Math.Max(System.Threading.Interlocked.Increment(off), off - 1)) And &HFF) << 24)
i += 1
End While
Return tmp
End Function
Private Shared Function generateSubkeys(key As SByte()) As Integer()
Dim u As Integer = w \ 8
Dim c As Integer = key.Length \ u
Dim t As Integer = 2 * r + 4
Dim L As Integer() = convBytesWords(key, u, c)
Dim S As Integer() = New Integer(t - 1) {}
S(0) = Pw
For i As Integer = 1 To t - 1
S(i) = S(i - 1) + Qw
Next
Dim A As Integer = 0
Dim B As Integer = 0
Dim k As Integer = 0, j As Integer = 0
Dim v As Integer = 3 * Math.Max(c, t)
For i As Integer = 0 To v - 1
A = InlineAssignHelper(S(k), RotateLeft((S(k) + A + B), 3))
B = InlineAssignHelper(L(j), RotateLeft(L(j) + A + B, A + B))
k = (k + 1) Mod t
j = (j + 1) Mod c
Next
Return S
End Function
Private Shared Function paddingKey(key As SByte()) As SByte()
Dim l As Integer = key.Length Mod 4
For i As Integer = 0 To l - 1
key(key.Length + i) = 0
Next
Return key
End Function
Private Shared Function deletePadding(input As SByte()) As SByte()
Dim count As Integer = 0
Dim i As Integer = input.Length - 1
While input(i) = 0
count += 1
i -= 1
End While
Dim tmp As SByte() = New SByte(input.Length - count - 2) {}
Array.Copy(input, 0, tmp, 0, tmp.Length)
Return tmp
End Function
Public Shared Function RotateLeft(value As Integer, count As Integer) As Integer
Dim val As UInteger = CUInt(value)
Return CInt((val << count) Or (val >> (32 - count)))
End Function
Public Shared Function RotateRight(value As Integer, count As Integer) As Integer
Dim val As UInteger = CUInt(value)
Return CInt((value >> count) Or (value << (32 - count)))
End Function
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
target = value
Return value
End Function
Private Shared Function decryptBloc(input As SByte()) As SByte()
Dim tmp As SByte() = New SByte(input.Length - 1) {}
Dim t As Integer, u As Integer
Dim aux As Integer
Dim data As Integer() = New Integer(input.Length \ 4 - 1) {}
For i As Integer = 0 To data.Length - 1
data(i) = 0
Next
Dim off As Integer = 0
For i As Integer = 0 To data.Length - 1
data(i) = ((input(System.Math.Max(System.Threading.Interlocked.Increment(off), off - 1)) And &HFF)) Or ((input(System.Math.Max(System.Threading.Interlocked.Increment(off), off - 1)) And &HFF) << 8) Or ((input(System.Math.Max(System.Threading.Interlocked.Increment(off), off - 1)) And &HFF) << 16) Or ((input(System.Math.Max(System.Threading.Interlocked.Increment(off), off - 1)) And &HFF) << 24)
Next
Dim A As Integer = data(0), B As Integer = data(1), C As Integer = data(2), D As Integer = data(3)
C = C - S(2 * r + 3)
A = A - S(2 * r + 2)
For i As Integer = r To 1 Step -1
aux = D
D = C
C = B
B = A
A = aux
u = RotateLeft(D * (2 * D + 1), 5)
t = RotateLeft(B * (2 * B + 1), 5)
C = RotateRight(C - S(2 * i + 1), t) Xor u
A = RotateRight(A - S(2 * i), u) Xor t
Next
D = D - S(1)
B = B - S(0)
data(0) = A
data(1) = B
data(2) = C
data(3) = D
For i As Integer = 0 To tmp.Length - 1
tmp(i) = CSByte(CInt(CUInt(data(i \ 4)) >> (i Mod 4) * 8) And &HFF)
Next
Return tmp
End Function
Private Shared Function encryptBloc(input As SByte()) As SByte()
Dim tmp As SByte() = New SByte(input.Length - 1) {}
Dim t As Integer, u As Integer
Dim aux As Integer
Dim data As Integer() = New Integer(input.Length \ 4 - 1) {}
For i As Integer = 0 To data.Length - 1
data(i) = 0
Next
Dim off As Integer = 0
For i As Integer = 0 To data.Length - 1
data(i) = ((input(System.Math.Max(System.Threading.Interlocked.Increment(off), off - 1)) And &HFF)) Or ((input(System.Math.Max(System.Threading.Interlocked.Increment(off), off - 1)) And &HFF) << 8) Or ((input(System.Math.Max(System.Threading.Interlocked.Increment(off), off - 1)) And &HFF) << 16) Or ((input(System.Math.Max(System.Threading.Interlocked.Increment(off), off - 1)) And &HFF) << 24)
Next
Dim A As Integer = data(0), B As Integer = data(1), C As Integer = data(2), D As Integer = data(3)
B = B + S(0)
D = D + S(1)
For i As Integer = 1 To r
t = RotateLeft(B * (2 * B + 1), 5)
u = RotateLeft(D * (2 * D + 1), 5)
A = RotateLeft(A Xor t, u) + S(2 * i)
C = RotateLeft(C Xor u, t) + S(2 * i + 1)
aux = A
A = B
B = C
C = D
D = aux
Next
A = A + S(2 * r + 2)
C = C + S(2 * r + 3)
data(0) = A
data(1) = B
data(2) = C
data(3) = D
For i As Integer = 0 To tmp.Length - 1
tmp(i) = CSByte(CInt(CUInt(data(i \ 4)) >> (i Mod 4) * 8) And &HFF)
Next
Return tmp
End Function
Public Function encrypt(data As SByte(), key As SByte()) As SByte()
Dim bloc As SByte() = New SByte(15) {}
key = paddingKey(key)
S = generateSubkeys(key)
Dim lenght As Integer = 16 - data.Length Mod 16
Dim padding As SByte() = New SByte(lenght - 1) {}
padding(0) = CSByte(&H80)
For i As Integer = 1 To lenght - 1
padding(i) = 0
Next
Dim count As Integer = 0
Dim tmp As SByte() = New SByte(data.Length + (lenght - 1)) {}
Dim j As Integer
For j = 0 To data.Length + (lenght - 1)
If j > 0 AndAlso j Mod 16 = 0 Then
bloc = encryptBloc(bloc)
Array.Copy(bloc, 0, tmp, j - 16, bloc.Length)
End If
If j < data.Length Then
bloc(j Mod 16) = data(j)
Else
bloc(j Mod 16) = padding(count)
count += 1
If count > lenght - 1 Then
count = 1
End If
End If
Next
bloc = encryptBloc(bloc)
Array.Copy(bloc, 0, tmp, j - 16, bloc.Length)
Return tmp
End Function
Public Shared Function decrypt(data As SByte(), key As SByte()) As SByte()
Dim tmp As SByte() = New SByte(data.Length - 1) {}
Dim bloc As SByte() = New SByte(15) {}
key = paddingKey(key)
S = generateSubkeys(key)
Dim i As Integer
For i = 0 To data.Length - 1
If i > 0 AndAlso i Mod 16 = 0 Then
bloc = decryptBloc(bloc)
Array.Copy(bloc, 0, tmp, i - 16, bloc.Length)
End If
If i < data.Length Then
bloc(i Mod 16) = data(i)
End If
Next
bloc = decryptBloc(bloc)
Array.Copy(bloc, 0, tmp, i - 16, bloc.Length)
tmp = deletePadding(tmp)
Return tmp
End Function
End Class
اذا الكود فيه خلل ,, ممكن مساعدة في تصحيحه
او اذا اي احد عندة كود جاهز لهذه الخوارزمية بالفجول دوت نت ممكن المساعدة