Czy jest to poprawna implementacja?
prosze o odpowiedz, pozdrawiam
Kod: Zaznacz cały
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MCV
{
internal static class Coding
{
public static Tuple<byte[],byte> getKey(byte[] Vec, byte[] VecInit)
{
int v = Vec.Length;
int vi = VecInit.Length;
byte s = 0;
int n = 0;
byte[] P = new byte[255];
for (byte i = 0; i < P.Length; i++)
{
P[i] = i;
}
for (int m = 0; m < 768; m++)
{
n = m % 255;
s = P[(s + P[n] + Vec[m % v]) %255];
var temp = P[n];
P[n] = P[s];
P[s] = temp;
}
for (int m = 0; m < 768; m++)
{
n = m % 255;
s = P[(s + P[n] + VecInit[m % vi]) % 255];
var temp = P[n];
P[n] = P[s];
P[s] = temp;
}
return Tuple.Create(P,s);
}
public static IEnumerable<byte> Encode(byte[] Key, byte[] Msg)
{
var temp1 = Msg;
byte[] initv = {213 ,123,32,42,31,62
,12,31,12,35,21,24,53,16,21,53,13,131,53,12,5,1,31,12,33 };
byte n = 0;
int L = Msg.Length;
Tuple<byte[], byte> kse = getKey(Key, initv);
byte s = kse.Item2;
byte[] P = kse.Item1;
for (int i = 0; i < L; i++)
{
s = P[(s + P[n]) % 255];
yield return P[(P[P[s]] + 1) % 255];
var temp = P[n];
P[n] = P[s];
P[s] = temp;
n++;
}
}
}
}