VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsLightCrypto"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
'=========================================
'=          Easy Emailer 2.4             =
'=---------------------------------------=
'=  Developed by P. G. B. Prasanna       =
'=  A Software Developer from Sri Lanka  =
'=  E-mail: pgbsoft@gmail.com            =
'=---------------------------------------=
'=        Copyright © 2015 PGBSoft       =
'=           All rights reserved         =
'=========================================

'Class Module for encryption/decryption of the application.

Option Explicit

Private mvarSkey As String

Public Property Let Key(ByVal vData As String)
    mvarSkey = vData
End Property

Public Property Get Key() As String
    Key = mvarSkey
End Property

'Encrypt/decrypt a given text.
Function EncryptDecrypt(ByVal strTxtVal As String, Optional blnCryptMode As Boolean = True) As String
    On Error GoTo Err
    
    'Dim intCount As Integer
    Dim intKeyPos As Integer
    Dim intKeyLen As Integer
    Dim intSrcAsc As Integer
    Dim strDest As String
    Dim intOffset As Integer
    Dim intTmpSrcAsc As Integer
    Dim intSrcPos As Integer
    Dim strKey As String

    strKey = mvarSkey

    intKeyLen = Len(strKey)

    If blnCryptMode Then
        Randomize
        intOffset = (Rnd * 10000 Mod 255) + 1

        strDest = Hex$(intOffset)
        If Len(strDest) = 1 Then strDest = "0" + strDest
        
        For intSrcPos = 1 To Len(strTxtVal)
            intSrcAsc = (Asc(Mid$(strTxtVal, intSrcPos, 1)) + intOffset) Mod 255
            If intKeyPos < intKeyLen Then intKeyPos = intKeyPos + 1 Else intKeyPos = 1
            intSrcAsc = intSrcAsc Xor Asc(Mid$(strKey, intKeyPos, 1))
            strDest = strDest + Format$(Hex$(intSrcAsc), "@@")
            intOffset = intSrcAsc
            intKeyPos = intKeyPos + 1

        Next
    Else
        intOffset = val("&H" + Left$(strTxtVal, 2))
        
        For intSrcPos = 3 To Len(strTxtVal) Step 2
            intSrcAsc = val("&H" + Trim(Mid$(strTxtVal, intSrcPos, 2)))
            If intKeyPos < intKeyLen Then intKeyPos = intKeyPos + 1 Else intKeyPos = 1
            intTmpSrcAsc = intSrcAsc Xor Asc(Mid$(strKey, intKeyPos, 1))
            If intTmpSrcAsc <= intOffset Then
                intTmpSrcAsc = 255 + intTmpSrcAsc - intOffset
            Else
                intTmpSrcAsc = intTmpSrcAsc - intOffset
            End If
            strDest = strDest + Chr(intTmpSrcAsc)
            intOffset = intSrcAsc
            intKeyPos = intKeyPos + 1
        Next
    End If
    EncryptDecrypt = strDest
    
    Exit Function
Err:
    'MsgBox "Unexpected error in applying security." & vbCrLf & "Please try again.", vbCritical
    End
End Function
