dimanche 10 mai 2015

C++ Crypto++ Run Time Crashing

I am a little bit more than just in the dark, about how to get around an issue I have been having for a while now. I've been using Crypto++ and the following bit of code.

// The Code
void encode (string data,string Skey,string &CipherText)
{
    byte key[16];
    byte iv[16];
    string theData,cipher;
    theData = data;
    string theKey(Skey);
    int i(0);
    theKey.append("t;rke;tlrke65409654ytr");
    while(i != 16)
    {
        key[i] = theKey[i];
        iv[i] = theKey[i];
        i++;
    }
    // Cipher Text Sink
    //string *CipherText = new string;

    // Encryptor
    CryptoPP::ECB_Mode< CryptoPP::AES >::Encryption 
    //Encryptor( key, sizeof(key), iv );
    Encryptor( key, sizeof(key));

  // Encryption
  CryptoPP::StringSource( theData, true,
    new CryptoPP::StreamTransformationFilter( Encryptor,
      new CryptoPP::StringSink( cipher )
    ) // StreamTransformationFilter
  ); // StringSource

}

void decode (string CT,string Skey,string &RText)
{
    byte key[16];
    byte iv[16];
    string theKey(Skey);
    string cipher,text;
    cipher = CT;

    int i(0);
    theKey.append("t;rke;tlrke65409654ytr");
    while(i != 16)
    {
        key[i] = theKey[i];
        iv[i] = theKey[i];
        i++;
    }
    // Decryptor
    CryptoPP::ECB_Mode< CryptoPP::AES >::Decryption
    // Decryptor( key, sizeof(key), iv );
    Decryptor( key, sizeof(key) );

  // Decryption
  CryptoPP::StringSource( cipher, true,
    new CryptoPP::StreamTransformationFilter( Decryptor,
      new CryptoPP::StringSink( text )
    ) // StreamTransformationFilter
  ); // StringSource
}
// Code ends here

This code works fine as a console application but once you try it with windows forms under the same compiler it simply crashes.

// The error
/*
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Heavy>cd F:\C++ Code\cryptGUI\cryptGUI\Release

C:\Users\Heavy>F:

F:\C++ Code\cryptGUI\cryptGUI\Release>cryptGUI.exe

Unhandled Exception: System.AccessViolationException: Attempted to read or write
 protected memory. This is often an indication that other memory is corrupt.
   at encode(basic_string<char\,std::char_traits<char>\,std::allocator<char> >*
, basic_string<char\,std::char_traits<char>\,std::allocator<char> >* , basic_str
ing<char\,std::char_traits<char>\,std::allocator<char> >* )
   at aesEncrypt(basic_string<char\,std::char_traits<char>\,std::allocator<char>
 >* , basic_string<char\,std::char_traits<char>\,std::allocator<char> >* data, b
asic_string<char\,std::char_traits<char>\,std::allocator<char> >* key) in f:\c++
 code\cryptgui\cryptgui\cryptgui\drivercode.cpp:line 141
   at GUI.mainGUI.encode_Click(Object sender, EventArgs e) in f:\c++ code\cryptg
ui\cryptgui\cryptgui\maingui.h:line 299
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, In
t32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr
wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.Uns
afeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int
32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 r
eason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason
, ApplicationContext context)
   at System.Windows.Forms.Application.RunDialog(Form form)
   at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
   at System.Windows.Forms.Form.ShowDialog()
   at main() in f:\c++ code\cryptgui\cryptgui\cryptgui\maingui.cpp:line 13
   at _mainCRTStartup()

F:\C++ Code\cryptGUI\cryptGUI\Release>
*/

I've tried to be as clear as possible with problem I am having. I hope someone can help or give advice.

Aucun commentaire:

Enregistrer un commentaire