Avatar billede testpilot_dk Nybegynder
14. april 2005 - 12:57 Der er 15 kommentarer og
1 løsning

Load af unmanaged dll filer

Hej!

Nu har jeg så lavet min dll fil, hvordan implementere jeg den så i c#

Filen er skrevet i visual c++ 7.0
Avatar billede burningice Nybegynder
14. april 2005 - 13:42 #1
brug PInvoke og DllImport
Avatar billede testpilot_dk Nybegynder
14. april 2005 - 15:09 #3
Jeg er blevet meget klogere på nogle ting, men min fil fejler stadig.
Vender tilbage senere
Avatar billede arne_v Ekspert
14. april 2005 - 21:07 #4
Vi skal jo nok se lidt kode, hvis vi skal hjælpe mere
Avatar billede testpilot_dk Nybegynder
15. april 2005 - 08:53 #5
Det kommer, har bare lige en deadline som skal overholdes inden jeg kan bruge tiden til det her, skriver i løbet af weekenden
Avatar billede testpilot_dk Nybegynder
15. april 2005 - 09:22 #6
Assembly TestAfDllAssembly;
        Type dllfile;

        try
        {
            TestAfDllAssembly = Assembly.Load("DES_Crypt.dll");
            dllfile = TestAfDllAssembly.GetType("somenamespace" + "." + "someclass");
            Object obj = Activator.CreateInstance(dllfile);

            string str2= "Test";
            Object[] param = new Object[2];
            param[0] = str2;
            param[1] = str2;
         
            MethodInfo mi = dllfile.GetMethod("Encrypt");
            String str = (String)mi.Invoke(obj, param);
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
            MessageBox.Show(ex.StackTrace);
        }

Mine filer til Dll'en kommer her

Header filen Header filen  Header filen  Header filen  Header filen
// DES_Crypt.h : main header file for the DES_Crypt DLL
//

#pragma once

#ifndef __AFXWIN_H__
    #error include 'stdafx.h' before including this file for PCH
#endif

#include "resource.h"        // main symbols
#include <afxtempl.h>

// CDES_CryptApp
// See DES_Crypt.cpp for the implementation of this class
//

struct StringData{
    CString str;
};

class CDES
{
protected:
      CDES();
      CList<StringData *, StringData*> * KeyScheduling(CString str);
      CString InitialPermutation(CString str);
      CString InversePermutation(CString str);
      CString EBitPermutation(CString str);
      CString PBitPermutation(CString str);
      CString SBox(CString str);
      CString LeftShift1(CString str);
      CString LeftShift2(CString str);
      CString RotateBits(CString str, int n );
      CString BitWiseXOR(CString str1, CString str2);
      CString CalculateSBoxValue(int TmpArray[5], int nBox);
      CString CharToBin(int n);
      CString CharToBinString(CString str);
      int BinToChar(CString str);
      CString BinToCharString(CString str);

      // Kryptering og Dekrypterings funktionerne

public:
      CString Encrypting(CString strText, CString strKey);
      CString Decrypting(CString strText, CString strKey);
      CString Encrypt(CString strText, CString strKey);
      CString Decrypt(CString strText, CString strKey);
};

class CDES_CryptApp : public CWinApp
{
public:
    CDES_CryptApp();

// Overrides
public:
    virtual BOOL InitInstance();

    DECLARE_MESSAGE_MAP()
};

CPP filen  CPP filen  CPP filen  CPP filen  CPP filen

// DES_Crypt.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "DES_Crypt.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// This is the main DLL file.

#include "stdafx.h"

int main()
{}


CString CDES::Encrypt(CString strText, CString strKey)
{
  CString strString = CharToBinString(strText);
  CString strStringKey = CharToBinString(strKey);
 
  CString strResult ="";
    CString strTmp = "";
    int Position = 0;
   
  if (strString.GetLength() < 64)
    {
        if (strString.GetAt(strString.GetLength() - 1) == '0')
        {
            for (int i = strString.GetLength(); i < 64; i++)
                strString += "1";
        }
        else
        {
            for (int i = strString.GetLength(); i < 64; i++)
                strString += "0";
        }
    }
    else if (strString.GetLength()%64 != 0)
    {
        int z = strString.GetLength()%64;
        if (strString.GetAt(strString.GetLength() - 1) == '0')
        {
            for (int i = z; i < 64; i++)
                strString += "1";
        }
        else
        {
            for (int i = z; i < 64; i++)
                strString += "0";
        }
    }


  while (Position < strString.GetLength()-1)
    {
        strTmp = "";
        for(int i = 0; i < 64; i++)
            strTmp += strString.GetAt(i+Position);
        strResult += Encrypting(strTmp, strKey);
        Position += 64;
    }

  return (BinToCharString(strResult));
}
CString CDES::Decrypt(CString strText, CString strKey)
{
  CString strString = CharToBinString(strText);
  CString strStringKey = CharToBinString(strKey);

  CString strResult ="";
    CString strTmp = "";
    int Position = 0;

  while (Position < strString.GetLength()-1)
    {
        strTmp = "";
        for(int i = 0; i < 64; i++)
            strTmp += strString.GetAt(i+Position);
        strResult += Decrypting(strTmp, strKey);
        Position += 64;
  }
  return (BinToCharString(strResult));
}

CString CDES::Decrypting(CString strText, CString strKey)
{
    CList<StringData *, StringData* > * RoundKeys = new CList<StringData *, StringData *>;
    RoundKeys = KeyScheduling(strKey);

    strText = InitialPermutation(strText);

    CString LeftI = strText, RightI_1 = strText; CString RightI = ""; CString strTmp = "";

    RightI_1.Delete(32, strText.GetLength());
    LeftI.Delete(0,32);

    POSITION pos = RoundKeys->GetTailPosition();
    while (pos != NULL)
    {
        StringData *pStringData = RoundKeys->GetAt(pos);

        RightI = LeftI;
        strTmp = EBitPermutation(LeftI);
        strTmp = BitWiseXOR(strTmp, pStringData->str);
        strTmp = SBox(strTmp);
        strTmp = PBitPermutation(strTmp);   
        LeftI = BitWiseXOR(RightI_1, strTmp);

        RoundKeys->GetPrev(pos);

        RightI_1 = RightI;
    }
    CString strResult = "";
    strResult = LeftI + RightI;
    strResult = InversePermutation(strResult);

    TRACE ("DeCipher: " + strResult + "\n");

    return strResult;
}

CString CDES::Encrypting(CString strText, CString strKey)
{
    CList<StringData *, StringData* > * RoundKeys = new CList<StringData *, StringData *>;
    RoundKeys = KeyScheduling(strKey);

    strText = InitialPermutation(strText);

    CString LeftI = "", RightI = strText; CString LeftI_1 = strText; CString strTmp = "";

    // Opdeler Strengen i venstre og højre side
    LeftI_1.Delete(32, strText.GetLength());
    RightI.Delete(0,32);

    POSITION pos = RoundKeys->GetHeadPosition();
    while (pos != NULL)
    {
        StringData *pStringData = RoundKeys->GetAt(pos);

        LeftI = RightI;
        strTmp = EBitPermutation(RightI);
        strTmp = BitWiseXOR(strTmp, pStringData->str);
        strTmp = SBox(strTmp);
        strTmp = PBitPermutation(strTmp);   
        RightI = BitWiseXOR(LeftI_1, strTmp);

        LeftI_1 = LeftI;
        RoundKeys->GetNext(pos);
    }

    CString strResult = "";
    strResult = RightI+ LeftI;
    strResult = InversePermutation(strResult);
    TRACE("Cipher: " + strResult + "\n");

    return strResult;
}   

CList<StringData*, StringData*>* CDES::KeyScheduling(CString str)
{
    /** Permutation nummer 1, den går ind og finder på hvilke plads i input at tallet   
    er, så finder den række nummer                                                   
    og søjle nummer, lægger dem sammen og sætter værdien ind på den nye plads        */
    int PC1[8][7] = {57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4};
    int PC2[8][6] = {14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32};

    CString strTmp = "00000000000000000000000000000000000000000000000000000000";

    int x = strTmp.GetLength();
    char letter;

    for (int z = 0; z < str.GetLength(); z++)
    {
        int count = 1;
        letter = str.GetAt(z);
        for (int j = 0; j < 8; j++)
        {
            for (int i = 0; i < 7; i++)
            {
                int test = PC1[j][i];
                if (z+1 == PC1[j][i])
                {
                    strTmp.SetAt(count + i-1, letter);
                    break;
                }       
            }
            count += 7;
        }
    }
    /** Permutation nummer 2, den går ind og finder på hvilke plads i input at tallet   
    er, så finder den række nummer                                                   
    og søjle nummer, lægger dem sammen og sætter værdien ind på den nye plads       
    Lægger hver rundenøgle i en liste, som bruges senere                            */
    CString strResult = strTmp;
    strTmp = "000000000000000000000000000000000000000000000000";
    CList<StringData *, StringData *> * m_Rounds = new CList<StringData* , StringData*>;
    for (int i = 1; i < 17; i++)
    {
        strResult = RotateBits(strResult, i);

        for (int z = 0; z < strResult.GetLength(); z++)
        {
            int count = 1;
            letter = strResult.GetAt(z);
            for (int j = 0; j < 8; j++)
            {
                for (int i = 0; i < 6; i++)
                    if (z+1 == PC2[j][i])
                    {
                        strTmp.SetAt(count + i-1, letter);
                        break;
                    }       
                    count += 6;
            }
        }
        StringData * pStringData = new StringData;
        pStringData->str = strTmp;

        m_Rounds->AddTail(pStringData);
    }
    return m_Rounds;

}

/**
                    Funktion InitialPermutation                           
*//**
Funktionen tager en streng på 64bit, strengen består af 0 og 1 taller
disse tal står som bekendt på forskellige pladser i strengen, det er
det som InitialPermutation benytter. Funktionen tager den enkelte bit
Leder en matrix på 8x8 igennem for at finde plads nummeret. Når det 
sker så flyttes 0 eller 1 tallet hen til den nye placering som findes
ved at lægge række nummer og kolonne nummer sammen, man skal lige   
plusse kolonne nummer med 8 for hver kolonnen man er rykket ned i   
matrixen.                                                           
Eks. Første tal i matrixen er 58, så den 58bit i input strengen skal
flyttes til plads 1 osv.                                               

*/
CString CDES::InitialPermutation(CString str)
{
    int Init_per[8][8] = {58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7};
  CString strTmp = "0000000000000000000000000000000000000000000000000000000000000000";
  int x = str.GetLength();
  char letter;

  for (int z = 0; z < str.GetLength(); z++)
  {
      int count = 1;
      letter = str.GetAt(z);
      for (int j = 0; j < 8; j++)
      {
          for (int i = 0; i < 8; i++)
          {
              int test = Init_per[j][i];
              if (z+1 == Init_per[j][i])
              {
                  strTmp.SetAt(count + i-1, letter);
                  break;
              }       
          }
          count += 8;
      }
  }

  return strTmp;
}

/**
                    Funktion InversePermutation                           
*//**
Funktionen tager og benytter den inverse af Initial Permutation dvs.
man kan tage sin tekst og sende gennem Initial Permutation og       
derefter gennem InversePermutation, så vil man have det samme som man
startede med.                                                       
*/
CString CDES::InversePermutation(CString str)
{
  int Invers_InitPerm[8][8] = {40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25};
  CString strTmp = "0000000000000000000000000000000000000000000000000000000000000000";
  int x = str.GetLength();
  char letter;

  for (int z = 0; z < str.GetLength(); z++)
  {
      int count = 1;
      letter = str.GetAt(z);
      for (int j = 0; j < 8; j++)
      {
          for (int i = 0; i < 8; i++)
          {
              int test = Invers_InitPerm[j][i];
              if (z+1 == Invers_InitPerm[j][i])
              {
                  strTmp.SetAt(count + i-1, letter);
                  break;
              }       
          }
          count += 8;
      }
  }

  return strTmp;
}
/**
                    Funktionen EBitPermutation                         
*//**
Funktionen tager som input en streng på 32 bit og returnere en streng
på 48 bit, det gøres for at man senere kan XOR den sammen med nøglen
som er på 48 bit, funktionen gør ligesom Initial og Inverse           
Permutation, nemlig at bytte rundt på de forskellige bits.           
*/
CString CDES::EBitPermutation(CString str)
{
    CString strTmp = "000000000000000000000000000000000000000000000000";
    int EBit[8][6] = {32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1};
    int x = str.GetLength();
    char letter;

    for (int z = 0; z < str.GetLength(); z++)
    {
        int count = 1;
        letter = str.GetAt(z);
        for (int j = 0; j < 8; j++)
        {
            for (int i = 0; i < 6; i++)
            {
                int test = EBit[j][i];
                if (z+1 == EBit[j][i])
                {
                    strTmp.SetAt(count + i-1, letter);
                    break;
                }       
            }
            count += 6;
        }
    }

    return strTmp;
}
/**
                    Funktionen PBitPermutation                           
*//**
Er det sidste der gøres inden man XOR sin tidligere højre eller       
venstre side til resultatet. som senere skal igennem InversePerm..   
Funktionen bytter også rundt på de bit som kommer ind i den.           
*/
CString CDES::PBitPermutation(CString str)
{
        int PBit[8][4] = {16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25};
    CString strTmp = "00000000000000000000000000000000";
    int x = str.GetLength();
    char letter;

    for (int z = 0; z < str.GetLength(); z++)
    {
        int count = 1;
        letter = str.GetAt(z);
        for (int j = 0; j < 8; j++)
        {
            for (int i = 0; i < 4; i++)
            {
                int test = PBit[j][i];
                if (z+1 == PBit[j][i])
                {
                    strTmp.SetAt(count + i-1, letter);
                    break;
                }       
            }
            count += 4;
        }
    }

    return strTmp;
}
/**
                    Funktionen SBox                                   
*//**
Funktionen deler sin input streng på 48 bit op i 8 blokke af 6 bit 
Herefter tager man bit 1 og 6 af den enkelte blok, finder tal værdien
for de to og så tager man den binære tal værdi af elementer mellem 1
og 6, dvs. bit 2,3,4 og 5 denne værdi samt værdien for bit 1 og 6   
skal bruges til at slå op i de forskellige S-Boxe, det gøres ved       
de 2 tal som er fremkommet som reprænsetere henholdsvis række og       
søjle, det nye tal som man finder i boksen omdannes til et binært tal
som lægges i output strengen som ender med at have længden 32.       
*/
CString CDES::SBox(CString str)
{
    int Part1[6] = {0}; int Part2[6] = {0}; int Part3[6] = {0}; int Part4[6] = {0}; int Part5[6] = {0}; int Part6[6] = {0}; int Part7[6] = {0}; int Part8[6] = {0};

    for (int i = 0; i < 6; i++)
        Part1[i] = str.GetAt(i) - 48;
    for (int i = 0; i < 6; i++)
        Part2[i] = str.GetAt(i+6) - 48;
    for (int i = 0; i < 6; i++)
        Part3[i] = str.GetAt(i+12) - 48;
    for (int i = 0; i < 6; i++)
        Part4[i] = str.GetAt(i+18) - 48;
    for (int i = 0; i < 6; i++)
        Part5[i] = str.GetAt(i+24) - 48;
    for (int i = 0; i < 6; i++)
        Part6[i] = str.GetAt(i+30) - 48;
    for (int i = 0; i < 6; i++)
        Part7[i] = str.GetAt(i+36) - 48;
    for (int i = 0; i < 6; i++)
        Part8[i] = str.GetAt(i+42) - 48;

    CString strResult = "";

    strResult = strResult + CalculateSBoxValue(Part1, 1);
    int x = strResult.GetLength();
    strResult = strResult + CalculateSBoxValue(Part2, 2);
    x = strResult.GetLength();
    strResult = strResult + CalculateSBoxValue(Part3, 3);
    x = strResult.GetLength();
    strResult = strResult + CalculateSBoxValue(Part4, 4);
    x = strResult.GetLength();
    strResult = strResult + CalculateSBoxValue(Part5, 5);
    x = strResult.GetLength();
    strResult = strResult + CalculateSBoxValue(Part6, 6);
    x = strResult.GetLength();
    strResult = strResult + CalculateSBoxValue(Part7, 7);
    x = strResult.GetLength();
    strResult = strResult + CalculateSBoxValue(Part8, 8);
    x = strResult.GetLength();

    return strResult;
}

CString CDES::CalculateSBoxValue(int TmpArray[6], int nBox)
{
    int S_Box1[4][16]= {14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 };
    int S_Box2[4][16]= {15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 };
    int S_Box3[4][16]= {10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 };
    int S_Box4[4][16]= {7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 };
    int S_Box5[4][16]= {2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 };
    int S_Box6[4][16]= {12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 };
    int S_Box7[4][16]= {4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 };
    int S_Box8[4][16]= {13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 };

    CString row = "", column ="";
    int rowNumber = 0, columnNumber = 0;
    row.Format("%d%d", TmpArray[0], TmpArray[5]);
    column.Format("%d%d%d%d", TmpArray[1], TmpArray[2], TmpArray[3], TmpArray[4]);
    rowNumber = BinToChar(row);
    columnNumber = BinToChar(column);

    int TmpValue = 0;
    if (nBox == 1)
        TmpValue = S_Box1[rowNumber][columnNumber];
    else if (nBox == 2)
        TmpValue = S_Box2[rowNumber][columnNumber];
    else if (nBox == 3)
        TmpValue = S_Box3[rowNumber][columnNumber];
    else if (nBox == 4)
        TmpValue = S_Box4[rowNumber][columnNumber];
    else if (nBox == 5)
        TmpValue = S_Box5[rowNumber][columnNumber];
    else if (nBox == 6)
        TmpValue = S_Box6[rowNumber][columnNumber];
    else if (nBox == 7)
        TmpValue = S_Box7[rowNumber][columnNumber];
    else if (nBox == 8)
        TmpValue = S_Box8[rowNumber][columnNumber];

    CString strResult = "";
    strResult = CharToBin(TmpValue);

    return strResult;
}

/**
                        Funktion CharToBin                             
*//**                                                                   
Funktionen tager en integer som input, denne integer bruges til at   
beregne, hvad den binære værdi for tallet er, det gøres ved at finde   
ud af om tallet går op i mod2, alt afhængigt at udfald, tilføres       
enten 0 eller 1, funktionen sætter ekstra  0'er på i tilfælde af for   
Korte strenge. Funktionen returnere altid en streng på længde 4       
*/
CString CDES::CharToBin(int n)
{
    int nTmp = n;
    CString strTmp;
   
    // Calculate the binary number of NthPower
    while (nTmp > 0)
    {
        if (nTmp%2)
            strTmp = "1" +strTmp;
        else
            strTmp = "0" + strTmp;
        nTmp = nTmp/2;
    }
    if (n == 0)
        strTmp = "0000";
    else if (n == 1)
        strTmp = "000" + strTmp;
    else if (n == 2|| n == 3)
        strTmp = "00" + strTmp;
    else if (n == 4 || n == 5 || n == 6 || n == 7)
        strTmp = "0" + strTmp;
    else
        strTmp = strTmp;
   
    return strTmp;
}

/**
                        Funktion BinToChar                         
*//**                                                                   
Funktionen tager en String som input, denne streng kan enten have   
længder på 2 eller 4, alt afhængigt af længden vælges den rigtige 
Konverterings funktion. Funktionen returnere en int som er mellem   
0 og 3 eller 0 og 15, begge inkl.                                   
*/
int CDES::BinToChar(CString str)
{
    char userin[8] = {0};
    int output = 0;
    if (str.GetLength() == 2)
    {
        for(int i = 0; i < 2; i++)
            userin[i] = str.GetAt(i);
        if(userin[0] == '1')
            output = output + 2;
        if(userin[1] == '1')
            output = output + 1;
    }
    if (str.GetLength() == 4)
    {
        for(int i = 0; i < 4; i++)
            userin[i] = str.GetAt(i);
        if(userin[0] == '1')
            output = output + 8;
        if(userin[1] == '1')
            output = output + 4;
        if(userin[2] == '1')
            output = output + 2;
        if(userin[3] == '1')
            output = output + 1;
    }
    return output;
}

/**
                    Funktion Leftshift1 og LeftShift2                 
*//**                                                                       
Funktionerne shifter henholdsvis 1 og 2 pladser i den aktuelle streng
Dette gør at strengens enkelte bit flyttes x antal pladser           
*/
CString CDES::LeftShift1(CString str)
{
    CString temp = str;
    CString letter;
    letter = str.GetAt(0);
    temp.Delete(0,1);
    temp = temp + letter;
    return temp;
}
CString CDES::LeftShift2(CString str)
{
    CString temp = str;
    CString letters;
    letters = str.GetAt(0);
    letters = letters + str.GetAt(1);
    temp.Delete(0,2);
    temp = temp + letters;
   

    return temp;
}

/**
                        Funktion RotateBits                           
*//**                                                                       
Funktionen tager sig af de forskellige pladsskifter der skal til få   
at det virker. Den skifter henholdsvis 1 og 2 skift alt afhængigt at
rundenøglen. Den returner strengen med de nye pladser.               
*/
CString CDES::RotateBits(CString strTmp, int i)
{
    CString Left = strTmp, Rigth = strTmp;

    CString strResult;
    Left.Delete(28,strTmp.GetLength());
    Rigth.Delete(0, 28);
    if (i == 1 || i == 2 || i == 9 || i == 16)
    {
        CString L1 = "";
        L1 = LeftShift1(Left) + LeftShift1(Rigth);
        strResult = "";
        strResult = L1;
    }
    else
    {
        CString L2 = "";
        L2 = LeftShift2(Left) + LeftShift2(Rigth);
        strResult = "";
        strResult = L2;
    }
    return strResult;
}

/**
                        Funktion BitWiseXOR                           
*//**                                                                       
Funktionen tager 2 strengen som bit for bit XOR med hinanden, dette 
giver en ny streng som returneres.                                   
*/
CString CDES::BitWiseXOR(CString str1, CString str2)
{
    int str1int = 0;
    int str2int = 0;
    int bitXor = 0;
    CString strResult = "", str ="";
    for(int i = 0; i < str1.GetLength(); i++)
    {
        str1int = (int)str1.GetAt(i)-48;
        str2int = (int)str2.GetAt(i)-48;
        bitXor = str1int ^ str2int;
        str.Format("%d", bitXor);
        strResult = strResult + str;
    }
    return strResult;
}

CString CDES::CharToBinString(CString str)
{
    int first, second, third, fourth, fifth, sixth, seventh, eighth;
    int ascii;

    CString strBin = "";
    CString strBinary = "";
    for(int i = 0; i < str.GetLength() ; i++)
    {
        ascii = int(str.GetAt(i));

        if(ascii >= 128)
        {
            eighth = 1;
            ascii = ascii - 128;
        }
        else
            eighth = 0;
        if(ascii >= 64)
        {
            first = 1;
            ascii = ascii - 64;
        }
        else
            first = 0;
        if(ascii  >= 32)
        {
            second = 1;
            ascii = ascii - 32;
        }
        else
            second = 0;
        if(ascii >= 16)
        {
            third = 1;
            ascii = ascii - 16;
        }
        else
            third = 0;
        if(ascii >= 8)
        {
            fourth = 1;
            ascii = ascii - 8;
        }
        else
            fourth = 0;
        if(ascii >= 4)
        {
            fifth = 1;
            ascii = ascii - 4;
        }
        else
            fifth = 0;
        if(ascii >= 2)
        {
            sixth = 1;
            ascii = ascii - 2;
        }
        else
            sixth = 0;
        if(ascii >= 1)
        {
            seventh = 1;
            ascii = ascii - 1;
        }
        else
            seventh = 0;
        strBin.Format("%d%d%d%d%d%d%d%d", eighth, first, second, third, fourth, fifth, sixth, seventh);
        strBinary += strBin;
    }
    return strBinary;
}

CString CDES::BinToCharString(CString str)
{
    CString strResult = "";
    CString strChar = "";
    char userin[8] = {0};

    int n = 0;
    do {
        for(int i = 0; i < 8; i++)
            userin[i] = str.GetAt(i + n);
        int output = 0;
        if(userin[0] == '1')
            output = output + 128;
        if(userin[1] == '1')
            output = output + 64;
        if(userin[2] == '1')
            output = output + 32;
        if(userin[3] == '1')
            output = output + 16;
        if(userin[4] == '1')
            output = output + 8;
        if(userin[5] == '1')
            output = output + 4;
        if(userin[6] == '1')
            output = output + 2;
        if(userin[7] == '1')
            output = output + 1;

        n += 8;
        if (output != 255)
        {
            strChar += char(output);
        }
    } while(n < str.GetLength());

    return strChar;

}

// CDES_CryptApp

BEGIN_MESSAGE_MAP(CDES_CryptApp, CWinApp)
END_MESSAGE_MAP()


// CDES_CryptApp construction

CDES_CryptApp::CDES_CryptApp()
{
    // TODO: add construction code here,
    // Place all significant initialization in InitInstance
}


// The one and only CDES_CryptApp object

CDES_CryptApp theApp;


// CDES_CryptApp initialization

BOOL CDES_CryptApp::InitInstance()
{
    CWinApp::InitInstance();

    return TRUE;
}
Avatar billede burningice Nybegynder
15. april 2005 - 09:26 #7
testpilot_dk>> hvilken del af PInvoke og DllImport var det du ikke forstod?
Avatar billede testpilot_dk Nybegynder
15. april 2005 - 09:41 #8
Tja, har vist ikke lige læst grundigt nok!

Hvordan benytter jeg de 2 funktioner
Avatar billede burningice Nybegynder
15. april 2005 - 09:50 #9
TestAfDllAssembly = Assembly.Load("DES_Crypt.dll");
            dllfile = TestAfDllAssembly.GetType("somenamespace" + "." + "someclass");
            Object obj = Activator.CreateInstance(dllfile);

det er sådan man gør med managed dll

Dette er dll-import, det står alt sammen i linket jeg postede (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp09192002.asp)

[DllImport("DES_Crypt.dll")]
public static string Encrypt(string text, string key);
Avatar billede testpilot_dk Nybegynder
15. april 2005 - 10:08 #10
Følgende fejl fremkommer når jeg sætter dem ind sådan her:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
using System.IO;
using System.Runtime.InteropServices;


namespace TestAfDll
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
      private System.Windows.Forms.Button button1;
      private System.Windows.Forms.TextBox textBox1;
      private System.Windows.Forms.TextBox textBox2;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
        this.button1 = new System.Windows.Forms.Button();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.textBox2 = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        //
        // button1
        //
        this.button1.Location = new System.Drawing.Point(144, 104);
        this.button1.Name = "button1";
        this.button1.TabIndex = 0;
        this.button1.Text = "button1";
        this.button1.Click += new System.EventHandler(this.button1_Click);
        //
        // textBox1
        //
        this.textBox1.Location = new System.Drawing.Point(16, 72);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(120, 20);
        this.textBox1.TabIndex = 1;
        this.textBox1.Text = "textBox1";
        //
        // textBox2
        //
        this.textBox2.Location = new System.Drawing.Point(16, 104);
        this.textBox2.Name = "textBox2";
        this.textBox2.Size = new System.Drawing.Size(120, 20);
        this.textBox2.TabIndex = 2;
        this.textBox2.Text = "textBox2";
        //
        // Form1
        //
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(292, 273);
        this.Controls.Add(this.textBox2);
        this.Controls.Add(this.textBox1);
        this.Controls.Add(this.button1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);

      }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        [DllImport("DES_Crypt.dll")]
        private static string Encrypt(string text, string key);
      private void button1_Click(object sender, System.EventArgs e)
      {
       
      }
}
}

    Error    1        'TestAfDll.Form1.Encrypt(string, string)' must declare a body because it is not marked abstract or extern    C:\Documents and Settings\Testpilot\Skrivebord\C#\TestAfDll\Form1.cs    114    24
Avatar billede burningice Nybegynder
15. april 2005 - 10:14 #11
du skal sætte access modifieren til public, og der mangler vist også en extern

[DllImport("DES_Crypt.dll")]
public static extern string Encrypt(string text, string key);
Avatar billede testpilot_dk Nybegynder
15. april 2005 - 10:25 #12
Takker Mange, nu fejlen den ikke mere, så skal jeg bare se om jeg har lavet min nye funktion rigtigt.

Cyberfessor, sender du et svar
Avatar billede testpilot_dk Nybegynder
15. april 2005 - 10:27 #13
Er vi ikke enige om at jeg burde kunne kalde Encrypt direkte, eller skal jeg lave en new af filen først
Avatar billede testpilot_dk Nybegynder
15. april 2005 - 10:29 #14
skal min dll fil ikke også have nogle extern metoder ??? eller er jeg galt på den
Avatar billede burningice Nybegynder
15. april 2005 - 10:43 #15
jeg har ingen anelse om hvordan din cpp-fil skal være opbygget, så der kan arne måske svare
Avatar billede testpilot_dk Nybegynder
15. april 2005 - 10:54 #16
Det må jeg lige finde ud af så, men tak for hjælpen
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
IT-kurser om Microsoft 365, sikkerhed, personlig vækst, udvikling, digital markedsføring, grafisk design, SAP og forretningsanalyse.

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester