CryptDecode

CryptEncode()で返還された配列からデータの逆変換を行います。

int  CryptEncode(
  ENUM_CRYPT_METHOD   method,        // 方法
  const uchar&        data[],        // ソース配列
  const uchar&        key[],        // キー
  uchar&              result[]       // ターゲットの配列
  );

パラメータ

method

[in]  データ変換方法。ENUM_CRYPT_METHOD 列挙のいずれかです。

data[]

[in]  ソース配列

key[]

[in]  キー配列

result[]

[out]  ターゲットの配列

戻り値

受け取り側の配列のバイト数、またはエラーの場合は 0。 エラー情報を取得するにはGetLastError() 関数が呼ばれます。

Example:

input string InpKey = "ABCDEFG"; // Encryption key
 
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
 {
  string text="The quick brown fox jumps over the lazy dog";
  uchar src[],dst[],key[];
 
//--- prepare the encryption key
  StringToCharArray(InpKey,key);
//--- prepare the src[] source array
  StringToCharArray(text,src);
//--- display source data
  PrintFormat("Initial data: size=%d, string='%s'",ArraySize(src),CharArrayToString(src));
 
//--- encrypt the src[] array using the DES method with the key[] 56-bit key
  int res=CryptEncode(CRYPT_DES,src,key,dst);
 
//--- check the encryption result
  if(res>0)
    {
    //--- display encrypted data
    PrintFormat("Encoded data: size=%d %s",res,ArrayToHex(dst));
    //--- decrypt the dst[] array data using the DES method with the key[] 56-bit key
    res=CryptDecode(CRYPT_DES,dst,key,src);
    //--- check the result
    if(res>0)
       {
        //--- display decrypted data
        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="";
 
//--- check the size
  if(count<0 || count>ArraySize(arr))
    count=ArraySize(arr);
 
//--- convert to hexadecimal string
  for(int i=0; i<count; i++)
    res+=StringFormat("%.2X",arr[i]);
 
  return(res);
 }

参照

配列関数 CryptEncode()