Monday, March 16, 2015

AES Encryption / Decryption for JAVA


 package org.ferris.aes.crypto;  
 import java.io.UnsupportedEncodingException;  
 import java.security.Key;  
 import java.security.spec.KeySpec;  
 import javax.crypto.Cipher;  
 import javax.crypto.SecretKey;  
 import javax.crypto.SecretKeyFactory;  
 import javax.crypto.spec.IvParameterSpec;  
 import javax.crypto.spec.PBEKeySpec;  
 import javax.crypto.spec.SecretKeySpec;  
 import org.apache.commons.codec.binary.Base64;  
 /**  
  *  
  *  
  */  
 public class AesBase64Wrapper {  
   private static String IV = "IV_VALUE_16_BYTE";   
   private static String PASSWORD = "PASSWORD_VALUE";   
   private static String SALT = "SALT_VALUE";   
   public String encryptAndEncode(String raw) {  
     try {  
       Cipher c = getCipher(Cipher.ENCRYPT_MODE);  
       byte[] encryptedVal = c.doFinal(getBytes(raw));  
       String s = getString(Base64.encodeBase64(encryptedVal));  
       return s;  
     } catch (Throwable t) {  
       throw new RuntimeException(t);  
     }  
   }  
   public String decodeAndDecrypt(String encrypted) throws Exception {  
     byte[] decodedValue = Base64.decodeBase64(getBytes(encrypted));  
     Cipher c = getCipher(Cipher.DECRYPT_MODE);  
     byte[] decValue = c.doFinal(decodedValue);  
     return new String(decValue);  
   }  
   private String getString(byte[] bytes) throws UnsupportedEncodingException {  
     return new String(bytes, "UTF-8");  
   }  
   private byte[] getBytes(String str) throws UnsupportedEncodingException {  
     return str.getBytes("UTF-8");  
   }  
   private Cipher getCipher(int mode) throws Exception {  
     Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");  
     byte[] iv = getBytes(IV);  
     c.init(mode, generateKey(), new IvParameterSpec(iv));  
     return c;  
   }  
   private Key generateKey() throws Exception {  
     SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");  
     char[] password = PASSWORD.toCharArray();  
     byte[] salt = getBytes(SALT);  
     KeySpec spec = new PBEKeySpec(password, salt, 65536, 128);  
     SecretKey tmp = factory.generateSecret(spec);  
     byte[] encoded = tmp.getEncoded();  
     return new SecretKeySpec(encoded, "AES");  
   }  
 }  

AES Encryption / Decryption for .NET(C#)


 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 using System.Security.Cryptography;  
 namespace EncryptDecryptTest  
 {  
   class Program  
   {  
     class AesBase64Wrapper  
     {  
       private static string IV = "IV_VALUE_16_BYTE";  
       private static string PASSWORD = "PASSWORD_VALUE";  
       private static string SALT = "SALT_VALUE";  
       public static string EncryptAndEncode(string raw)  
       {  
         using (var csp = new AesCryptoServiceProvider())  
         {  
           ICryptoTransform e = GetCryptoTransform(csp, true);  
           byte[] inputBuffer = Encoding.UTF8.GetBytes(raw);  
           byte[] output = e.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length);  
           string encrypted = Convert.ToBase64String(output);  
           return encrypted;  
         }  
       }  
       public static string DecodeAndDecrypt(string encrypted)  
       {  
         using (var csp = new AesCryptoServiceProvider())  
         {  
           var d = GetCryptoTransform(csp, false);  
           byte[] output = Convert.FromBase64String(encrypted);  
           byte[] decryptedOutput = d.TransformFinalBlock(output, 0, output.Length);  
           string decypted = Encoding.UTF8.GetString(decryptedOutput);  
           return decypted;  
         }  
       }  
       private static ICryptoTransform GetCryptoTransform(AesCryptoServiceProvider csp, bool encrypting)  
       {  
         csp.Mode = CipherMode.CBC;  
         csp.Padding = PaddingMode.PKCS7;  
         var spec = new Rfc2898DeriveBytes(Encoding.UTF8.GetBytes(PASSWORD), Encoding.UTF8.GetBytes(SALT), 65536);  
         byte[] key = spec.GetBytes(16);  
         csp.IV = Encoding.UTF8.GetBytes(IV);  
         csp.Key = key;  
         if (encrypting)  
         {  
           return csp.CreateEncryptor();  
         }  
         return csp.CreateDecryptor();  
       }  
     }  
     static void Main(string[] args)  
     {  
       string encryptMe;  
       string encrypted;  
       string decrypted;  
       encryptMe = "please encrypt me";  
       Console.WriteLine("encryptMe = " + encryptMe);  
       encrypted = AesBase64Wrapper.EncryptAndEncode(encryptMe);  
       Console.WriteLine("encypted: " + encrypted);  
       decrypted = AesBase64Wrapper.DecodeAndDecrypt(encrypted);  
       Console.WriteLine("decrypted: " + decrypted);  
       Console.WriteLine("press any key to exit....");  
       Console.ReadKey();  
     }  
   }  
 }  

Thursday, February 26, 2015

SQL Query to Backup all the databases in SQL 2008


 DECLARE @name VARCHAR(50) -- database name   
 DECLARE @path VARCHAR(256) -- path for backup files   
 DECLARE @fileName VARCHAR(256) -- filename for backup   
 DECLARE @fileDate VARCHAR(20) -- used for file name   
 SET @path = 'C:\Backup\'   
 SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)   
 DECLARE db_cursor CURSOR FOR   
 SELECT name   
 FROM master.dbo.sysdatabases   
 WHERE name NOT IN ('master','model','msdb','tempdb')   
 OPEN db_cursor   
 FETCH NEXT FROM db_cursor INTO @name   
 WHILE @@FETCH_STATUS = 0   
 BEGIN   
 SET @fileName = @path + @name + '_' + @fileDate + '.BAK'   
 BACKUP DATABASE @name TO DISK = @fileName WITH COMPRESSION  
 FETCH NEXT FROM db_cursor INTO @name   
 END   
 CLOSE db_cursor   
 DEALLOCATE db_cursor   

Thursday, February 19, 2015

Disable and Enable all the constraints in a SQL database


 Disable all the constraint in database

 EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"  


Enable all the constraint in database

 EXEC sp_msforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all"