input string InpKey = "ABCDEFG"; // Clé de cryptage
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
string text="The quick brown fox jumps over the lazy dog";
uchar src[],dst[],key[];
//--- prépare la clé de cryptage
StringToCharArray(InpKey,key);
//--- prépare the la tableau source src[]
StringToCharArray(text,src);
//--- affiche les données source
PrintFormat("Initial data: size=%d, string='%s'",ArraySize(src),CharArrayToString(src));
//--- encrypte le tableau src[] avec la méthode DES avec la clé key[] sur 56 bits
int res=CryptEncode(CRYPT_DES,src,key,dst);
//--- vérifie le résultat du cryptage
if(res>0)
{
//--- affiche les données encryptées
PrintFormat("Encoded data: size=%d %s",res,ArrayToHex(dst));
//--- décrypte le tableau dst[] avec la méthode DES et la clé key[] sur 56 bits
res=CryptDecode(CRYPT_DES,dst,key,src);
//--- vérification du résultat
if(res>0)
{
//--- affiche les données décryptées
PrintFormat("Decoded data: size=%d, string='%s'",ArraySize(src),CharArrayToString(src));
}
else
Print("CryptDecode failed. Error: ",GetLastError());
}
else
Print("CryptEncode failed. Error: ",GetLastError());
}
//+------------------------------------------------------------------+
//| ArrayToHex |
//+------------------------------------------------------------------+
string ArrayToHex(uchar &arr[],int count=-1)
{
string res="";
//--- vérification de la taille
if(count<0 || count>ArraySize(arr))
count=ArraySize(arr);
//--- conversion en chaîne hexadécimale
for(int i=0; i<count; i++)
res+=StringFormat("%.2X",arr[i]);
return(res);
}
|