none
Encrypt at client side and decrypt at server using tripledes. RRS feed

  • Question

  • Hi There,

    I have already working server side code for encryption and decryption but now i want to use encryption at client side using Jquery  and so i have a jquery function written for but not working exactly.

    Server side code :

    public class Encryption64
        {
            private byte[] key = { };
            private byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };

            public string Decrypt(string stringToDecrypt, string sEncryptionKey)
            {
                byte[] inputByteArray = new byte[stringToDecrypt.Length + 1];
                try
                {

                    //key = System.Text.Encoding.UTF8.GetBytes(Left(SEncryptionKey, 8));
                    key = System.Text.Encoding.UTF8.GetBytes(sEncryptionKey.ToCharArray(), 0, 8);
                    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                    inputByteArray = Convert.FromBase64String(stringToDecrypt);
                    MemoryStream ms = new MemoryStream();
                    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    System.Text.Encoding encoding = System.Text.Encoding.UTF8;
                    return encoding.GetString(ms.ToArray());
                }
                catch (Exception ex)
                {
                    ExceptionManager.AppendLog(ex);
                    return ex.Message;
                }
            }

            public string Encrypt(string stringToEncrypt, string SEncryptionKey)
            {

                try
                {
                    key = System.Text.Encoding.UTF8.GetBytes(SEncryptionKey.ToCharArray(), 0, 8);
                    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                    byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
                    MemoryStream ms = new MemoryStream();
                    CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    return Convert.ToBase64String(ms.ToArray());
                }
                catch (Exception ex)
                {
                    ExceptionManager.AppendLog(ex);
                    return ex.Message;
                }
            }

    Client Side Jquery Function

    <script src="~/Scripts/DES/core-min.js"></script>
    <script src="~/Scripts/DES/tripledes.js"></script>

            

    function encryptMyParameters(message, key) {
            var keyWords = CryptoJS.enc.Utf8.parse(key);//CryptoJS.enc.Utf8.parse(key.split('').join(','), 0, 8);
            var messageWords = CryptoJS.enc.Utf8.parse(message);
            var ivWords = CryptoJS.lib.WordArray.create([18, 52, 86, 120, 144, 171, 205, 239]);
            var desEncryptor = CryptoJS.algo.DES.createEncryptor(keyWords, { iv: [18, 52, 86, 120, 144, 171, 205, 239] });
           // var encrypted = CryptoJS.DES.encrypt(messageWords, desEncryptor);
            var encrypted = CryptoJS.DES.encrypt(messageWords, keyWords, { iv: ivWords });  
            var ciphertext = CryptoJS.enc.Base64.stringify(encrypted.ciphertext);//encrypted.ciphertext.toString(CryptoJS.enc.Base64);
            return ciphertext;

        };

    I need help to know the issue in Jquery Encryption function.

    Thanks 


    Tribhuwan kushwaha

    Wednesday, April 18, 2018 9:38 AM

All replies

  • Hi Tribhuwan kushwaha,

    Thank you for posting here.

    I am confused about your question. In the title of this thread you want to encrypt and decrypt using tripledes. If you want to do this, please refer to the code below.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp
    {
        class TripleDES_encrypt_and_decrypt
        {
            static void Main(string[] args)
            {
                // define the local key and vector byte arrays
                byte[] key = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
                  13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
                byte[] iv = { 8, 7, 6, 5, 4, 3, 2, 1 };
    
                // instantiate the class with the arrays
                cTripleDES des = new cTripleDES(key, iv);
    
                // for the example, encrypted value
                string encryptedData = des.Encrypt("hello");
    
                // now, decrypt the data
                string decryptedData = des.Decrypt(encryptedData);
    
                // the value of decryptedData should be "test",
                // but for our example purposes, let's re-encrypt it
                string newEncryptedData = des.Encrypt(decryptedData);
    
    
            }
        }
        class cTripleDES
        {
            // define the triple des provider
            private TripleDESCryptoServiceProvider m_des =
                     new TripleDESCryptoServiceProvider();
    
            // define the string handler
            private UTF8Encoding m_utf8 = new UTF8Encoding();
    
            // define the local property arrays
            private byte[] m_key;
            private byte[] m_iv;
    
            public cTripleDES(byte[] key, byte[] iv)
            {
                this.m_key = key;
                this.m_iv = iv;
            }
    
            public byte[] Encrypt(byte[] input)
            {
                return Transform(input,
                       m_des.CreateEncryptor(m_key, m_iv));
            }
    
            public byte[] Decrypt(byte[] input)
            {
                return Transform(input,
                       m_des.CreateDecryptor(m_key, m_iv));
            }
    
            public string Encrypt(string text)
            {
                byte[] input = m_utf8.GetBytes(text);
                byte[] output = Transform(input,
                                m_des.CreateEncryptor(m_key, m_iv));
                return Convert.ToBase64String(output);
            }
    
            public string Decrypt(string text)
            {
                byte[] input = Convert.FromBase64String(text);
                byte[] output = Transform(input,
                                m_des.CreateDecryptor(m_key, m_iv));
                return m_utf8.GetString(output);
            }
    
            private byte[] Transform(byte[] input,
                           ICryptoTransform CryptoTransform)
            {
                // create the necessary streams
                MemoryStream memStream = new MemoryStream();
                CryptoStream cryptStream = new CryptoStream(memStream,
                             CryptoTransform, CryptoStreamMode.Write);
                // transform the bytes as requested
                cryptStream.Write(input, 0, input.Length);
                cryptStream.FlushFinalBlock();
                // Read the memory stream and
                // convert it back into byte array
                memStream.Position = 0;
                byte[] result = memStream.ToArray();
                // close and release the streams
                memStream.Close();
                cryptStream.Close();
                // hand back the encrypted buffer
                return result;
            }
        }
    }

    But, according to your description with code, you wan to encrypt in C# and decrypt in java script? If yes, please post your javascript question in StackOverFlow.

    https://stackoverflow.com/questions/tagged/javascript

    Best Regards,

    Wendy

     


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, April 20, 2018 8:51 AM
    Moderator