Welcome to the Xceed Community | Help
Community Search  
More Search Options

I can't decrypt cipher created ny another application

Sort Posts: Previous Next
  •  10-31-2008, 1:31 AM Post no. 16533

    I can't decrypt cipher created ny another application

    Hello All,
    I have one trouble with Xceed Encryption library v1.1.
    I use the appropriate ActiveX component from both VB6.0 and visual C++ (VS2005) applications. But cypher created by VB application can't be decrypted in vc++ application. Both projects use one ActiveX component but result a different encrypted cyphers. The created cyphers are simply with a different lengths.
    Here is my code from VB6.0:
    Private Sub Class_Initialize()

        Randomize Timer

        EMBEDDELIM = Chr$(255)

        '--- initialize the XCeed encryption libs

        mbySecretKey(1) = &HA
        mbySecretKey(2) = &HA
        mbySecretKey(3) = &HA
        mbySecretKey(4) = &HA
        mbySecretKey(5) = &HA
        mbySecretKey(6) = &HA
        mbySecretKey(7) = &HA
        mbySecretKey(8) = &HA
        mbySecretKey(9) = &HA
        mbySecretKey(10) = &HA
        mbySecretKey(11) = &HA
        mbySecretKey(12) = &HA
        mbySecretKey(13) = &HA
        mbySecretKey(14) = &HA
        mbySecretKey(15) = &HA
        mbySecretKey(16) = &HA

        Set mxcEncrypt = New XceedEncryptionLib.XceedEncryption
        Set mxcMethod = New XceedEncryptionLib.XceedTwofishEncryptionMethod

        '--- License the library for run-time use.
        Call mxcEncrypt.License("My license code")
       
        '--- Initialize the encoder.
        mxcMethod.EncryptionMode = emoFreeBlocks
        mxcMethod.SecretKey = mbySecretKey
        mxcMethod.SetInitVectorFromPassPhrase TWOFISHVECTOR

        Set mxcEncrypt.EncryptionMethod = mxcMethod


        Exit Sub
    End Sub

    Public Function EncryptStringToHexString(ByVal sSource As String) As String
        Dim b()                     As Byte
        Dim bOut()                  As Byte
        Dim lLenIn                  As Long
        Dim lLBound                 As Long
       

        If (Len(sSource) > 0) Then
            soso = Len(sSource)
            b() = mxcEncrypt.Encrypt(sSource, True)
            lLBound = LBound(b())
           
            lLenIn = UBound(b()) - lLBound + 1
            '== !!!!!!!!!!!!!!!!!!!!   lLenIn is equal 2064 bytes and Len(sSource) is equal 1024 bytes

        End If
        Exit Function
    End Function


    Here is my code in vc++
    void CEncryptTesterWithAXDlg::EncryptData(const char* pszSource)
    {
        CoInitialize( NULL );

        try
        {
          IXceedEncryptionPtr piEnc;

          piEnc.CreateInstance( CLSID_XceedEncryption );
          piEnc->License( _bstr_t( _T("My license code") ) );

          IXceedTwofishEncryptionMethodPtr twoFish;
          BYTE mbySecretKey[SECRETKEYLENGTH] = {0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA};

          twoFish.CreateInstance( CLSID_XceedTwofishEncryptionMethod ); 
          twoFish->SetSecretKey(mbySecretKey, SECRETKEYLENGTH);
          twoFish->EncryptionMode = emoFreeBlocks;
          twoFish->SetInitVectorFromPassPhrase(_bstr_t(TWOFISHVECTOR));

          piEnc->EncryptionMethod = IXceedEncryptDataPtr( twoFish ); 

          DWORD dwSourceSize = lstrlen( pszSource ) + 1; // Let's say we want to encrypt the null-char too
          BYTE* pcEncrypted = NULL;
          DWORD dwEncryptedSize = 0; 

          piEnc->Encrypt( ( BYTE* )pszSource, dwSourceSize, TRUE, &pcEncrypted, &dwEncryptedSize ); 
          '== !!!!!!!!!!!!!!!!!!!!   for the same source string dwEncryptedSize is equal only 1040 bytes and Len(sSource) is equal 1024 bytes as well.

          CoTaskMemFree( pcEncrypted );
        }
        catch( const _com_error& xErr )
        {
          char szMsg[50];
          wsprintf( szMsg, "Error %08x\n", xErr.Error() );
        }
        catch( ... )
        {
          ;//MessageBox( NULL, "Unknown error", "Error", MB_OK );
        }

        CoUninitialize();
    }

    Can you explain why Xceed encryption ActiveX works so differently for the different IDE?
    I use the same sekret key and initial vectors and algorithm and my goal is to generate the same cyphers from the both VB 6.0 and visual studio 2005 appliactions.

  •  11-03-2008, 3:39 PM Post no. 16602 in reply to 16533

    Re: I can't decrypt cipher created ny another application

    If you are testing this with a string, both will not produce the same result, since in C++ a string is ANSI, whereas in VB it is Unicode.  Make sure to use the same encoding, or test this with binary data, and it should work fine.
    André
    Software Developer
    Xceed Software Inc.
  •  11-05-2008, 3:20 AM Post no. 16651 in reply to 16602

    Re: I can't decrypt cipher created ny another application

    I see. Thank you.

    In c++ I use this way for the data encryption:

            LPOLESTR wsz;
            AnsiToUnicode(pszSource, &wsz);
            dwSourceSize = lstrlenW( wsz );

            BYTE* pcEncrypted = NULL;
            DWORD dwEncryptedSize = 0; 

            m_xceedEncryption->Encrypt( (BYTE*)wsz, 2 * dwSourceSize, TRUE, &pcEncrypted, &dwEncryptedSize );  

    And this way for decrypting data:

            LPCOLESTR pcDecrypted = NULL;
            DWORD dwDecryptedSize = 0; 

            m_xceedEncryption->Decrypt( sourceUnprotected, unprotectedSourceLen, TRUE, (BYTE**)&pcDecrypted, &dwDecryptedSize );

            LPSTR pcDecryptedStr = NULL;
            UnicodeToAnsi(pcDecrypted, &pcDecryptedStr);

View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.