All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Alachisoft.NCache.Common.Protector Maven / Gradle / Ivy

package Alachisoft.NCache.Common;

//Basit: Commented: Helper class to encrypt and decrypt connection string info

/**
 * Helper class to encrypt and decrypt connection string info
 */
public class Protector {
//
//    public static String DecryptString(String txtString)
//    {
//        // If the variable is blank, return the input
//        if (txtString.equals(""))
//        {
//            return txtString;
//        }
//
//        // Create an instance of the encryption API
//        // We assume the key has been encrypted on this machine and not by a user
//        DataProtector dp = new DataProtector(Store.Machine);
//
//        // Use the API to decrypt the connection string
//        // API works with bytes so we need to convert to and from byte arrays
////C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
////ORIGINAL LINE: byte[] decryptedData = dp.Decrypt(Convert.FromBase64String(txtString), null);
//        byte[] decryptedData = dp.Decrypt(Convert.FromBase64String(txtString), null);
//
//        // Return the decyrpted data to the string
//        return Encoding.ASCII.GetString(decryptedData);
//    }
//
//    public static String EncryptString(String encryptedString)
//    {
//        // Create an instance of the encryption API
//        // We assume the key has been encrypted on this machine and not by a user
//        DataProtector dp = new DataProtector(Store.Machine);
//
//        // Use the API to encrypt the connection string
//        // API works with bytes so we need to convert to and from byte arrays
////C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
////ORIGINAL LINE: byte[] dataBytes = Encoding.ASCII.GetBytes(encryptedString);
//        byte[] dataBytes = Encoding.ASCII.GetBytes(encryptedString);
////C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
////ORIGINAL LINE: byte[] encryptedBytes = dp.Encrypt(dataBytes, null);
//        byte[] encryptedBytes = dp.Encrypt(dataBytes, null);
//
//        // Return the encyrpted data to the string
//        return Convert.ToBase64String(encryptedBytes);
//    }
//
//    private enum Store
//    {
//
//        Machine(1),
//        User(2);
//        private int intValue;
//        private static java.util.HashMap mappings;
//
//        private static java.util.HashMap getMappings()
//        {
//            if (mappings == null)
//            {
//                synchronized (Store.class)
//                {
//                    if (mappings == null)
//                    {
//                        mappings = new java.util.HashMap();
//                    }
//                }
//            }
//            return mappings;
//        }
//
//        private Store(int value)
//        {
//            intValue = value;
//            Store.getMappings().put(value, this);
//        }
//
//        public int getValue()
//        {
//            return intValue;
//        }
//
//        public static Store forValue(int value)
//        {
//            return getMappings().get(value);
//        }
//    }
//
//    /**
//     * The DSAPI wrapper To be released as part of the Microsoft Configuration Building Block
//     */
//    private static class DataProtector
//    {
////C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//        ///#region Constants
//
//        private static IntPtr NullPtr = ((IntPtr) ((int) (0)));
//        private static final int CRYPTPROTECT_UI_FORBIDDEN = 0x1;
//        private static final int CRYPTPROTECT_LOCAL_MACHINE = 0x4;
//        private Store store = Store.values()[0];
////C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//        ///#endregion
//
////C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//        ///#region P/Invoke structures
////C# TO JAVA CONVERTER WARNING: Java does not allow user-defined value types. The behavior of this class will differ from the original:
////ORIGINAL LINE: internal struct DATA_BLOB
////C# TO JAVA CONVERTER TODO TASK: Java annotations will not correspond to .NET attributes:
//        //[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
//        public final static class DATA_BLOB
//        {
//
//            public int cbData;
//            public IntPtr pbData = new IntPtr();
//
//            public DATA_BLOB clone()
//            {
//                DATA_BLOB varCopy = new DATA_BLOB();
//
//                varCopy.cbData = this.cbData;
//                varCopy.pbData = this.pbData;
//
//                return varCopy;
//            }
//        }
//
////C# TO JAVA CONVERTER WARNING: Java does not allow user-defined value types. The behavior of this class will differ from the original:
////ORIGINAL LINE: internal struct CRYPTPROTECT_PROMPTSTRUCT
////C# TO JAVA CONVERTER TODO TASK: Java annotations will not correspond to .NET attributes:
//        //[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
//        public final static class CRYPTPROTECT_PROMPTSTRUCT
//        {
//
//            public int cbSize;
//            public int dwPromptFlags;
//            public IntPtr hwndApp = new IntPtr();
//            public String szPrompt;
//
//            public CRYPTPROTECT_PROMPTSTRUCT clone()
//            {
//                CRYPTPROTECT_PROMPTSTRUCT varCopy = new CRYPTPROTECT_PROMPTSTRUCT();
//
//                varCopy.cbSize = this.cbSize;
//                varCopy.dwPromptFlags = this.dwPromptFlags;
//                varCopy.hwndApp = this.hwndApp;
//                varCopy.szPrompt = this.szPrompt;
//
//                return varCopy;
//            }
//        }
////C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//        ///#endregion
////C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
////#if !MONO
//
////C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//        ///#region External methods
//        private static native boolean CryptProtectData(tangible.RefObject pDataIn, String szDataDescr, tangible.RefObject pOptionalEntropy, IntPtr pvReserved, tangible.RefObject pPromptStruct, int dwFlags, tangible.RefObject pDataOut);
//
//        static
//        {
//            System.loadLibrary("Crypt32.dll");
//        }
//
//        private static native boolean CryptUnprotectData(tangible.RefObject pDataIn, String szDataDescr, tangible.RefObject pOptionalEntropy, IntPtr pvReserved, tangible.RefObject pPromptStruct, int dwFlags, tangible.RefObject pDataOut);
//
////ORIGINAL LINE: private unsafe static extern int FormatMessage(int dwFlags, ref IntPtr lpSource, int dwMessageId, int dwLanguageId, ref String lpBuffer, int nSize, IntPtr* Arguments);
////C# TO JAVA CONVERTER TODO TASK: There is no equivalent to the 'unsafe' modifier in Java:
//        private static native int FormatMessage(int dwFlags, tangible.RefObject lpSource, int dwMessageId, int dwLanguageId, tangible.RefObject lpBuffer, int nSize, IntPtr
//
//
//
//            *  Arguments);
//		static {
//			System.loadLibrary("kernel32.dll");
//        }
////C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//        ///#endregion
////#endif
////C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//        ///#region Constructor
//
//        public DataProtector(Store tempStore)
//        {
//            store = tempStore;
//        }
////C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//        ///#endregion
//
////C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//        ///#region Public methods
////C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
////ORIGINAL LINE: public byte[] Encrypt(byte[] plainText, byte[] optionalEntropy)
//        public final byte[] Encrypt(byte[] plainText, byte[] optionalEntropy)
//        {
////C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
////#if !MONO
//            boolean retVal = false;
//
//            DATA_BLOB plainTextBlob = new DATA_BLOB();
//            DATA_BLOB cipherTextBlob = new DATA_BLOB();
//            DATA_BLOB entropyBlob = new DATA_BLOB();
//
//            CRYPTPROTECT_PROMPTSTRUCT prompt = new CRYPTPROTECT_PROMPTSTRUCT();
//            tangible.RefObject tempRef_prompt = new tangible.RefObject(prompt);
//            InitPromptstruct(tempRef_prompt);
//            prompt = tempRef_prompt.argvalue;
//
//            int dwFlags;
//            try
//            {
//                try
//                {
//                    int bytesSize = plainText.length;
//                    plainTextBlob.pbData = Marshal.AllocHGlobal(bytesSize);
//                    if (IntPtr.Zero == plainTextBlob.pbData)
//                    {
//                        throw new RuntimeException("Unable to allocate plaintext buffer.");
//                    }
//                    plainTextBlob.cbData = bytesSize;
//                    Marshal.Copy(plainText, 0, plainTextBlob.pbData, bytesSize);
//                }
//                catch (RuntimeException ex)
//                {
//                    throw new RuntimeException("Exception marshalling data. " + ex.getMessage());
//                }
//                if (Store.Machine == store)
//                {
//                    //Using the machine store, should be providing entropy.
//                    dwFlags = CRYPTPROTECT_LOCAL_MACHINE | CRYPTPROTECT_UI_FORBIDDEN;
//                    //Check to see if the entropy is null
//                    if (null == optionalEntropy)
//                    {
//                        //Allocate something
//                        optionalEntropy = new byte[0];
//                    }
//                    try
//                    {
//                        int bytesSize = optionalEntropy.length;
//                        entropyBlob.pbData = Marshal.AllocHGlobal(optionalEntropy.length);
//                        if (IntPtr.Zero == entropyBlob.pbData)
//                        {
//                            throw new RuntimeException("Unable to allocate entropy data buffer.");
//                        }
//                        Marshal.Copy(optionalEntropy, 0, entropyBlob.pbData, bytesSize);
//                        entropyBlob.cbData = bytesSize;
//                    }
//                    catch (RuntimeException ex)
//                    {
//                        throw new RuntimeException("Exception entropy marshalling data. " + ex.getMessage());
//                    }
//                }
//                else
//                {
//                    //Using the user store
//                    dwFlags = CRYPTPROTECT_UI_FORBIDDEN;
//                }
//                tangible.RefObject tempRef_plainTextBlob = new tangible.RefObject(plainTextBlob);
//                tangible.RefObject tempRef_entropyBlob = new tangible.RefObject(entropyBlob);
//                tangible.RefObject tempRef_prompt2 = new tangible.RefObject(prompt);
//                tangible.RefObject tempRef_cipherTextBlob = new tangible.RefObject(cipherTextBlob);
//                retVal = CryptProtectData(tempRef_plainTextBlob, "", tempRef_entropyBlob, IntPtr.Zero, tempRef_prompt2, dwFlags, tempRef_cipherTextBlob);
//                plainTextBlob = tempRef_plainTextBlob.argvalue;
//                entropyBlob = tempRef_entropyBlob.argvalue;
//                prompt = tempRef_prompt2.argvalue;
//                cipherTextBlob = tempRef_cipherTextBlob.argvalue;
//                if (false == retVal)
//                {
//                    throw new RuntimeException("Encryption failed. " + GetErrorMessage(Marshal.GetLastWin32Error()));
//                }
//            }
//            catch (RuntimeException ex)
//            {
//                throw new RuntimeException("Exception encrypting. " + ex.getMessage());
//            }
////C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
////ORIGINAL LINE: byte[] cipherText = new byte[cipherTextBlob.cbData];
//            byte[] cipherText = new byte[cipherTextBlob.cbData];
//            Marshal.Copy(cipherTextBlob.pbData, cipherText, 0, cipherTextBlob.cbData);
//            return cipherText;
////#else
//            //@UH
//            return plainText;
////#endif
//        }
//
////C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
////ORIGINAL LINE: public byte[] Decrypt(byte[] cipherText, byte[] optionalEntropy)
//        public final byte[] Decrypt(byte[] cipherText, byte[] optionalEntropy)
//        {
////C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
////#if !MONO
//            boolean retVal = false;
//            DATA_BLOB plainTextBlob = new DATA_BLOB();
//            DATA_BLOB cipherBlob = new DATA_BLOB();
//            CRYPTPROTECT_PROMPTSTRUCT prompt = new CRYPTPROTECT_PROMPTSTRUCT();
//            tangible.RefObject tempRef_prompt = new tangible.RefObject(prompt);
//            InitPromptstruct(tempRef_prompt);
//            prompt = tempRef_prompt.argvalue;
//            try
//            {
//                try
//                {
//                    int cipherTextSize = cipherText.length;
//                    cipherBlob.pbData = Marshal.AllocHGlobal(cipherTextSize);
//                    if (IntPtr.Zero == cipherBlob.pbData)
//                    {
//                        throw new RuntimeException("Unable to allocate cipherText buffer.");
//                    }
//                    cipherBlob.cbData = cipherTextSize;
//                    Marshal.Copy(cipherText, 0, cipherBlob.pbData, cipherBlob.cbData);
//                }
//                catch (RuntimeException ex)
//                {
//                    throw new RuntimeException("Exception marshalling data. " + ex.getMessage());
//                }
//                DATA_BLOB entropyBlob = new DATA_BLOB();
//                int dwFlags;
//                if (Store.Machine == store)
//                {
//                    //Using the machine store, should be providing entropy.
//                    dwFlags = CRYPTPROTECT_LOCAL_MACHINE | CRYPTPROTECT_UI_FORBIDDEN;
//                    //Check to see if the entropy is null
//                    if (null == optionalEntropy)
//                    {
//                        //Allocate something
//                        optionalEntropy = new byte[0];
//                    }
//                    try
//                    {
//                        int bytesSize = optionalEntropy.length;
//                        entropyBlob.pbData = Marshal.AllocHGlobal(bytesSize);
//                        if (IntPtr.Zero == entropyBlob.pbData)
//                        {
//                            throw new RuntimeException("Unable to allocate entropy buffer.");
//                        }
//                        entropyBlob.cbData = bytesSize;
//                        Marshal.Copy(optionalEntropy, 0, entropyBlob.pbData, bytesSize);
//                    }
//                    catch (RuntimeException ex)
//                    {
//                        throw new RuntimeException("Exception entropy marshalling data. " + ex.getMessage());
//                    }
//                }
//                else
//                {
//                    //Using the user store
//                    dwFlags = CRYPTPROTECT_UI_FORBIDDEN;
//                }
//                tangible.RefObject tempRef_cipherBlob = new tangible.RefObject(cipherBlob);
//                tangible.RefObject tempRef_entropyBlob = new tangible.RefObject(entropyBlob);
//                tangible.RefObject tempRef_prompt2 = new tangible.RefObject(prompt);
//                tangible.RefObject tempRef_plainTextBlob = new tangible.RefObject(plainTextBlob);
//                retVal = CryptUnprotectData(tempRef_cipherBlob, null, tempRef_entropyBlob, IntPtr.Zero, tempRef_prompt2, dwFlags, tempRef_plainTextBlob);
//                cipherBlob = tempRef_cipherBlob.argvalue;
//                entropyBlob = tempRef_entropyBlob.argvalue;
//                prompt = tempRef_prompt2.argvalue;
//                plainTextBlob = tempRef_plainTextBlob.argvalue;
//                if (false == retVal)
//                {
//                    throw new RuntimeException("Decryption failed. " + GetErrorMessage(Marshal.GetLastWin32Error()));
//                }
//                //Free the blob and entropy.
//                if (IntPtr.Zero != cipherBlob.pbData)
//                {
//                    Marshal.FreeHGlobal(cipherBlob.pbData);
//                }
//                if (IntPtr.Zero != entropyBlob.pbData)
//                {
//                    Marshal.FreeHGlobal(entropyBlob.pbData);
//                }
//            }
//            catch (RuntimeException ex)
//            {
//                throw new RuntimeException("Exception decrypting. " + ex.getMessage());
//            }
////C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
////ORIGINAL LINE: byte[] plainText = new byte[plainTextBlob.cbData];
//            byte[] plainText = new byte[plainTextBlob.cbData];
//            Marshal.Copy(plainTextBlob.pbData, plainText, 0, plainTextBlob.cbData);
//            return plainText;
////#else
//            //@UH
//            return cipherText;
////#endif
//        }
////C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//        ///#endregion
//
////C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//        ///#region Private methods
//        private void InitPromptstruct(tangible.RefObject ps)
//        {
//            ps.argvalue.cbSize = Marshal.SizeOf(CRYPTPROTECT_PROMPTSTRUCT.class);
//            ps.argvalue.dwPromptFlags = 0;
//            ps.argvalue.hwndApp = NullPtr;
//            ps.argvalue.szPrompt = null;
//        }
//
////ORIGINAL LINE: private unsafe static String GetErrorMessage(int errorCode)
////C# TO JAVA CONVERTER TODO TASK: There is no equivalent to the 'unsafe' modifier in Java:
//        private static String GetErrorMessage(int errorCode)
//        {
////C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
////#if !MONO
//            int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;
//            int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
//            int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;
//            int messageSize = 255;
//            String lpMsgBuf = "";
//            int dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
//            IntPtr ptrlpSource = new IntPtr();
//            IntPtr prtArguments = new IntPtr();
//            tangible.RefObject tempRef_ptrlpSource = new tangible.RefObject(ptrlpSource);
//            tangible.RefObject tempRef_lpMsgBuf = new tangible.RefObject(lpMsgBuf);
//            int retVal = FormatMessage(dwFlags, tempRef_ptrlpSource, errorCode, 0, tempRef_lpMsgBuf, messageSize,  & prtArguments);
//            ptrlpSource = tempRef_ptrlpSource.argvalue;
//            lpMsgBuf = tempRef_lpMsgBuf.argvalue;
//            if (0 == retVal)
//            {
//                throw new RuntimeException("Failed to format message for error code " + errorCode + ". ");
//            }
//            return lpMsgBuf;
////#else
//            //@UH
//            return "";
////#endif
//        }
////C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//        ///#endregion
//    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy