Popular Posts
javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i...
Stats
Detect column exist
Sql server:
  1. -- Add new column if column not exists
  2. IF NOT EXISTS(SELECT * FROM sys.columns WHERE Name = N'ColumnName' AND Object_ID = Object_ID(N'TableName'))
  3. ALTER TABLE [TableName] ADD [ColumnName] VARCHAR(200)
  4.  
  5.  
  6. -- Modify column name if new column not exists
  7. IF NOT EXISTS(SELECT * FROM sys.columns WHERE Name = N'NewColumnName' AND Object_ID = Object_ID(N'TableName'))
  8. EXECUTE sp_rename N'TableName.OldColumnName', N'NewColumnName', 'COLUMN' 
Detect document size
  1. var JSUtil = {
  2.     filterResults: function (win, docel, body) {
  3.         var result = win ? win : 0;
  4.         if (docel && (!result || (result > docel))) result = docel;
  5.         return body && (!result || (result > body)) ? body : result;
  6.     },
  7.     clientSize: function () {
  8.         return {
  9.             w: this.filterResults(
  10.                 window.innerWidth ? window.innerWidth : 0,
  11.                 document.documentElement ? document.documentElement.clientWidth : 0,
  12.                 document.body ? document.body.clientWidth : 0
  13.                ),
  14.             h: this.filterResults(
  15.                 window.innerHeight ? window.innerHeight : 0,
  16.                 document.documentElement ? document.documentElement.clientHeight : 0,
  17.                 document.body ? document.body.clientHeight : 0
  18.                )
  19.         };
  20.     }
  21. };
Getting window size and scroll bars position in JavaScript/DHTML
Detect the condition is null or not
  1. UPDATE
  2.     [fl_news_content]
  3. SET
  4.     [nc_title] = @nc_title,
  5.     [nc_summary] = @nc_summary,
  6.     [nc_content] = @nc_content,
  7.     [nc_date] = @nc_date,
  8. WHERE
  9.     1 = 1
  10.     AND (@nc_create_oid IS NULL or [nc_create_oid] = @nc_create_oid)
OR
  1. UPDATE
  2.     [fl_news_content]
  3. SET
  4.     [nc_title] = @nc_title,
  5.     [nc_summary] = @nc_summary,
  6.     [nc_content] = @nc_content,
  7.     [nc_date] = @nc_date,
  8. WHERE
  9.     1 = 1
  10.     AND [nc_create_oid] = ISNULL(@nc_create_oid,[nc_create_oid])
Simple encrypting and decrypting data in C#
  1. /* 
  2.     This sample code is provided "AS IS" with no warranties,
  3.     and confers no rights. 
  4.  
  5.     ATTENTION: This sample is designed to be more of a
  6.     tutorial rather than something you can copy and paste in
  7.     the production code! 
  8. */
  9. using System;
  10. using System.IO;
  11. using System.Security.Cryptography;
  12. /* 
  13.     Sample encrypt/decrypt functions 
  14.     Parameter checks and error handling
  15.     are ommited for better readability 
  16. */
  17. public class EncDec
  18. {
  19.     // Encrypt a byte array into a byte array using a key and an IV 
  20.     public static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
  21.     {
  22.         // Create a MemoryStream to accept the encrypted bytes 
  23.         MemoryStream ms = new MemoryStream();
  24.         // Create a symmetric algorithm. 
  25.         // We are going to use Rijndael because it is strong and
  26.         // available on all platforms. 
  27.         // You can use other algorithms, to do so substitute the
  28.         // next line with something like 
  29.         //      TripleDES alg = TripleDES.Create(); 
  30.         Rijndael alg = Rijndael.Create();
  31.         // Now set the key and the IV. 
  32.         // We need the IV (Initialization Vector) because
  33.         // the algorithm is operating in its default 
  34.         // mode called CBC (Cipher Block Chaining).
  35.         // The IV is XORed with the first block (8 byte) 
  36.         // of the data before it is encrypted, and then each
  37.         // encrypted block is XORed with the 
  38.         // following block of plaintext.
  39.         // This is done to make encryption more secure. 
  40.         // There is also a mode called ECB which does not need an IV,
  41.         // but it is much less secure. 
  42.         alg.Key = Key;
  43.         alg.IV = IV;
  44.         // Create a CryptoStream through which we are going to be
  45.         // pumping our data. 
  46.         // CryptoStreamMode.Write means that we are going to be
  47.         // writing data to the stream and the output will be written
  48.         // in the MemoryStream we have provided. 
  49.         CryptoStream cs = new CryptoStream(ms,
  50.            alg.CreateEncryptor(), CryptoStreamMode.Write);
  51.         // Write the data and make it do the encryption 
  52.         cs.Write(clearData, 0, clearData.Length);
  53.         // Close the crypto stream (or do FlushFinalBlock). 
  54.         // This will tell it that we have done our encryption and
  55.         // there is no more data coming in, 
  56.         // and it is now a good time to apply the padding and
  57.         // finalize the encryption process. 
  58.         cs.Close();
  59.         // Now get the encrypted data from the MemoryStream.
  60.         // Some people make a mistake of using GetBuffer() here,
  61.         // which is not the right way. 
  62.         byte[] encryptedData = ms.ToArray();
  63.         return encryptedData;
  64.     }
  65.     // Encrypt a string into a string using a password 
  66.     //    Uses Encrypt(byte[], byte[], byte[]) 
  67.     public static string Encrypt(string clearText, string Password)
  68.     {
  69.         // First we need to turn the input string into a byte array. 
  70.         byte[] clearBytes =
  71.           System.Text.Encoding.Unicode.GetBytes(clearText);
  72.         // Then, we need to turn the password into Key and IV 
  73.         // We are using salt to make it harder to guess our key
  74.         // using a dictionary attack - 
  75.         // trying to guess a password by enumerating all possible words. 
  76.         PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
  77.             new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 
  78.             0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
  79.         // Now get the key/IV and do the encryption using the
  80.         // function that accepts byte arrays. 
  81.         // Using PasswordDeriveBytes object we are first getting
  82.         // 32 bytes for the Key 
  83.         // (the default Rijndael key length is 256bit = 32bytes)
  84.         // and then 16 bytes for the IV. 
  85.         // IV should always be the block size, which is by default
  86.         // 16 bytes (128 bit) for Rijndael. 
  87.         // If you are using DES/TripleDES/RC2 the block size is
  88.         // 8 bytes and so should be the IV size. 
  89.         // You can also read KeySize/BlockSize properties off
  90.         // the algorithm to find out the sizes. 
  91.         byte[] encryptedData = Encrypt(clearBytes,
  92.                  pdb.GetBytes(32), pdb.GetBytes(16));
  93.         // Now we need to turn the resulting byte array into a string. 
  94.         // A common mistake would be to use an Encoding class for that.
  95.         // It does not work because not all byte values can be
  96.         // represented by characters. 
  97.         // We are going to be using Base64 encoding that is designed
  98.         // exactly for what we are trying to do. 
  99.         return Convert.ToBase64String(encryptedData);
  100.     }
  101.     // Encrypt bytes into bytes using a password 
  102.     //    Uses Encrypt(byte[], byte[], byte[]) 
  103.     public static byte[] Encrypt(byte[] clearData, string Password)
  104.     {
  105.         // We need to turn the password into Key and IV. 
  106.         // We are using salt to make it harder to guess our key
  107.         // using a dictionary attack - 
  108.         // trying to guess a password by enumerating all possible words. 
  109.         PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
  110.             new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 
  111.             0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
  112.         // Now get the key/IV and do the encryption using the function
  113.         // that accepts byte arrays. 
  114.         // Using PasswordDeriveBytes object we are first getting
  115.         // 32 bytes for the Key 
  116.         // (the default Rijndael key length is 256bit = 32bytes)
  117.         // and then 16 bytes for the IV. 
  118.         // IV should always be the block size, which is by default
  119.         // 16 bytes (128 bit) for Rijndael. 
  120.         // If you are using DES/TripleDES/RC2 the block size is 8
  121.         // bytes and so should be the IV size. 
  122.         // You can also read KeySize/BlockSize properties off the
  123.         // algorithm to find out the sizes. 
  124.         return Encrypt(clearData, pdb.GetBytes(32), pdb.GetBytes(16));
  125.     }
  126.     // Encrypt a file into another file using a password 
  127.     public static void Encrypt(string fileIn,
  128.                 string fileOut, string Password)
  129.     {
  130.         // First we are going to open the file streams 
  131.         FileStream fsIn = new FileStream(fileIn,
  132.             FileMode.Open, FileAccess.Read);
  133.         FileStream fsOut = new FileStream(fileOut,
  134.             FileMode.OpenOrCreate, FileAccess.Write);
  135.         // Then we are going to derive a Key and an IV from the
  136.         // Password and create an algorithm 
  137.         PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
  138.             new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 
  139.             0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
  140.         Rijndael alg = Rijndael.Create();
  141.         alg.Key = pdb.GetBytes(32);
  142.         alg.IV = pdb.GetBytes(16);
  143.         // Now create a crypto stream through which we are going
  144.         // to be pumping data. 
  145.         // Our fileOut is going to be receiving the encrypted bytes. 
  146.         CryptoStream cs = new CryptoStream(fsOut,
  147.             alg.CreateEncryptor(), CryptoStreamMode.Write);
  148.         // Now will will initialize a buffer and will be processing
  149.         // the input file in chunks. 
  150.         // This is done to avoid reading the whole file (which can
  151.         // be huge) into memory. 
  152.         int bufferLen = 4096;
  153.         byte[] buffer = new byte[bufferLen];
  154.         int bytesRead;
  155.         do
  156.         {
  157.             // read a chunk of data from the input file 
  158.             bytesRead = fsIn.Read(buffer, 0, bufferLen);
  159.             // encrypt it 
  160.             cs.Write(buffer, 0, bytesRead);
  161.         } while (bytesRead != 0);
  162.         // close everything 
  163.         // this will also close the unrelying fsOut stream
  164.         cs.Close();
  165.         fsIn.Close();
  166.     }
  167.     // Decrypt a byte array into a byte array using a key and an IV 
  168.     public static byte[] Decrypt(byte[] cipherData,
  169.                                 byte[] Key, byte[] IV)
  170.     {
  171.         // Create a MemoryStream that is going to accept the
  172.         // decrypted bytes 
  173.         MemoryStream ms = new MemoryStream();
  174.         // Create a symmetric algorithm. 
  175.         // We are going to use Rijndael because it is strong and
  176.         // available on all platforms. 
  177.         // You can use other algorithms, to do so substitute the next
  178.         // line with something like 
  179.         //     TripleDES alg = TripleDES.Create(); 
  180.         Rijndael alg = Rijndael.Create();
  181.         // Now set the key and the IV. 
  182.         // We need the IV (Initialization Vector) because the algorithm
  183.         // is operating in its default 
  184.         // mode called CBC (Cipher Block Chaining). The IV is XORed with
  185.         // the first block (8 byte) 
  186.         // of the data after it is decrypted, and then each decrypted
  187.         // block is XORed with the previous 
  188.         // cipher block. This is done to make encryption more secure. 
  189.         // There is also a mode called ECB which does not need an IV,
  190.         // but it is much less secure. 
  191.         alg.Key = Key;
  192.         alg.IV = IV;
  193.         // Create a CryptoStream through which we are going to be
  194.         // pumping our data. 
  195.         // CryptoStreamMode.Write means that we are going to be
  196.         // writing data to the stream 
  197.         // and the output will be written in the MemoryStream
  198.         // we have provided. 
  199.         CryptoStream cs = new CryptoStream(ms,
  200.             alg.CreateDecryptor(), CryptoStreamMode.Write);
  201.         // Write the data and make it do the decryption 
  202.         cs.Write(cipherData, 0, cipherData.Length);
  203.         // Close the crypto stream (or do FlushFinalBlock). 
  204.         // This will tell it that we have done our decryption
  205.         // and there is no more data coming in, 
  206.         // and it is now a good time to remove the padding
  207.         // and finalize the decryption process. 
  208.         cs.Close();
  209.         // Now get the decrypted data from the MemoryStream. 
  210.         // Some people make a mistake of using GetBuffer() here,
  211.         // which is not the right way. 
  212.         byte[] decryptedData = ms.ToArray();
  213.         return decryptedData;
  214.     }
  215.     // Decrypt a string into a string using a password 
  216.     //    Uses Decrypt(byte[], byte[], byte[]) 
  217.     public static string Decrypt(string cipherText, string Password)
  218.     {
  219.         // First we need to turn the input string into a byte array. 
  220.         // We presume that Base64 encoding was used 
  221.         byte[] cipherBytes = Convert.FromBase64String(cipherText);
  222.         // Then, we need to turn the password into Key and IV 
  223.         // We are using salt to make it harder to guess our key
  224.         // using a dictionary attack - 
  225.         // trying to guess a password by enumerating all possible words. 
  226.         PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
  227.             new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 
  228.             0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
  229.         // Now get the key/IV and do the decryption using
  230.         // the function that accepts byte arrays. 
  231.         // Using PasswordDeriveBytes object we are first
  232.         // getting 32 bytes for the Key 
  233.         // (the default Rijndael key length is 256bit = 32bytes)
  234.         // and then 16 bytes for the IV. 
  235.         // IV should always be the block size, which is by
  236.         // default 16 bytes (128 bit) for Rijndael. 
  237.         // If you are using DES/TripleDES/RC2 the block size is
  238.         // 8 bytes and so should be the IV size. 
  239.         // You can also read KeySize/BlockSize properties off
  240.         // the algorithm to find out the sizes. 
  241.         byte[] decryptedData = Decrypt(cipherBytes,
  242.             pdb.GetBytes(32), pdb.GetBytes(16));
  243.         // Now we need to turn the resulting byte array into a string. 
  244.         // A common mistake would be to use an Encoding class for that.
  245.         // It does not work 
  246.         // because not all byte values can be represented by characters. 
  247.         // We are going to be using Base64 encoding that is 
  248.         // designed exactly for what we are trying to do. 
  249.         return System.Text.Encoding.Unicode.GetString(decryptedData);
  250.     }
  251.     // Decrypt bytes into bytes using a password 
  252.     //    Uses Decrypt(byte[], byte[], byte[]) 
  253.     public static byte[] Decrypt(byte[] cipherData, string Password)
  254.     {
  255.         // We need to turn the password into Key and IV. 
  256.         // We are using salt to make it harder to guess our key
  257.         // using a dictionary attack - 
  258.         // trying to guess a password by enumerating all possible words. 
  259.         PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
  260.             new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 
  261.             0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
  262.         // Now get the key/IV and do the Decryption using the 
  263.         // function that accepts byte arrays. 
  264.         // Using PasswordDeriveBytes object we are first getting
  265.         // 32 bytes for the Key 
  266.         // (the default Rijndael key length is 256bit = 32bytes)
  267.         // and then 16 bytes for the IV. 
  268.         // IV should always be the block size, which is by default
  269.         // 16 bytes (128 bit) for Rijndael. 
  270.         // If you are using DES/TripleDES/RC2 the block size is
  271.         // 8 bytes and so should be the IV size. 
  272.         // You can also read KeySize/BlockSize properties off the
  273.         // algorithm to find out the sizes. 
  274.         return Decrypt(cipherData, pdb.GetBytes(32), pdb.GetBytes(16));
  275.     }
  276.     // Decrypt a file into another file using a password 
  277.     public static void Decrypt(string fileIn,
  278.                 string fileOut, string Password)
  279.     {
  280.         // First we are going to open the file streams 
  281.         FileStream fsIn = new FileStream(fileIn,
  282.                     FileMode.Open, FileAccess.Read);
  283.         FileStream fsOut = new FileStream(fileOut,
  284.                     FileMode.OpenOrCreate, FileAccess.Write);
  285.         // Then we are going to derive a Key and an IV from
  286.         // the Password and create an algorithm 
  287.         PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
  288.             new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 
  289.             0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
  290.         Rijndael alg = Rijndael.Create();
  291.         alg.Key = pdb.GetBytes(32);
  292.         alg.IV = pdb.GetBytes(16);
  293.         // Now create a crypto stream through which we are going
  294.         // to be pumping data. 
  295.         // Our fileOut is going to be receiving the Decrypted bytes. 
  296.         CryptoStream cs = new CryptoStream(fsOut,
  297.             alg.CreateDecryptor(), CryptoStreamMode.Write);
  298.         // Now will will initialize a buffer and will be 
  299.         // processing the input file in chunks. 
  300.         // This is done to avoid reading the whole file (which can be
  301.         // huge) into memory. 
  302.         int bufferLen = 4096;
  303.         byte[] buffer = new byte[bufferLen];
  304.         int bytesRead;
  305.         do
  306.         {
  307.             // read a chunk of data from the input file 
  308.             bytesRead = fsIn.Read(buffer, 0, bufferLen);
  309.             // Decrypt it 
  310.             cs.Write(buffer, 0, bytesRead);
  311.         } while (bytesRead != 0);
  312.         // close everything 
  313.         cs.Close(); // this will also close the unrelying fsOut stream 
  314.         fsIn.Close();
  315.     }
  316. }
reference
Call validator on client
  1. var Flood = {
  2.     confirmUpdate: function(validateGroup, confirmMessage, fn) {
  3.         if (typeof (fn) == "undefined") fn = true;
  4.         if (Page_ClientValidate(validateGroup)) {
  5.             return fn && confirm(confirmMessage);
  6.         }
  7.         return true;
  8.     },
  9.     isAllNullOrEmpty: function(clientIDs) {
  10.         switch (clientIDs.constructor) {
  11.             case String:
  12.                 return !(document.getElementById(clientIDs) && document.getElementById(clientIDs).value.length > 0);
  13.             case Array:
  14.                 for (var i = 0; i < clientIDs.length; i++) {
  15.                     if (document.getElementById(clientIDs[i]) && document.getElementById(clientIDs[i]).value.length > 0)
  16.                         return false;
  17.                 }
  18.                 break;
  19.             default:
  20.                 throw new Exception("Invalid parameters");
  21.         }
  22.         return true;
  23.     }
  24. };
Read only field
  1. <asp:TextBox ID="TextBox1" runat="server" onkeypress="return false;"/>
OR
  1. TextBox1.Attributes["readonly"] = "true";
update optional memo
  1. UPDATE
  2.     [fl_news_content]
  3. SET
  4.     [nc_title] = @nc_title,
  5.     [nc_summary] = @nc_summary,
  6.     [nc_content] = @nc_content,
  7.     [nc_date] = @nc_date,
  8.     [nc_data_source] = @nc_data_source,
  9.     [nc_data_source_link] = @nc_data_source_link,
  10.     [nc_sort] = @nc_sort,
  11.     [nc_first_record] = @nc_first_record,
  12.     [nc_status] = @nc_status,
  13.     [nc_deleted] = @nc_deleted,
  14.     [nc_start_date] = @nc_start_date,
  15.     [nc_end_date] = @nc_end_date,
  16.     [nc_lastfix_time] = GETDATE(),
  17.     [nc_lastfix_user] = @nc_lastfix_user,
  18.     [nc_lastfix_user_ip] = @nc_lastfix_user_ip
  19. WHERE
  20.     [nc_id] = @nc_id;
  21.  
  22.  
  23. UPDATE
  24.     [fl_news_content]
  25. SET
  26.     [nc_img_bin] = @nc_img_bin,
  27.     [nc_img_type] = @nc_img_type,
  28.     [nc_img_isnew] = @nc_img_isnew
  29. WHERE
  30.     [nc_id] = @nc_id AND @nc_img_bin IS NOT NULL;