Monday, 24 June 2013

Set System Date& Time With Internet Date & Time Automatic...

Run This Program and set date & time your system......


:( you need internet Connection...!!!)


import java.io.IOException;
import java.net.InetAddress;
import java.util.Date;


import org.apache.commons.net.ntp.NTPUDPClient;

import org.apache.commons.net.ntp.TimeInfo;

//For this library you need commons-net-3.3 Jar File....

public class Data {
    public static final String TIME_SERVER = "in.pool.ntp.org";  
   
   
    public static void main(String...s) throws IOException{

//Code For gate Date & Time From Time Server....
 
    NTPUDPClient timeClient = new NTPUDPClient();  
    InetAddress inetAddress =

InetAddress.getByName(TIME_SERVER);
    TimeInfo timeInfo = timeClient.getTime(inetAddress);
    long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();
    Date time = new Date(returnTime);
    System.out.println(time);
    Date d = new Date();
       String strDateToSet = time.getDate()+"-"+(time.getMonth()+1)+"-"+(time.getYear()+1900);
       String strTimeToSet = time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
 

  //Code For Set Date & time.....

if(d!=time){
     Runtime.getRuntime().exec("cmd /C date " + strDateToSet); // dd-MM-yy
    Runtime.getRuntime().exec("cmd /C time " + strTimeToSet); // hh:mm:ss
   }
    }
}

Any Query ????

Contact ....

Karmadip Dodiya

+91 8980216689



Sunday, 16 June 2013

DES Encryption - Descryption code in JAVA CODE

Encrypt your String with DES Encryption Technic By Following Code...



1) Cryptography.java


package DESCode;

/**
 *
 */

import java.io.IOException;
import java.security.InvalidKeyException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * @author dharmvir.singh The class demonstrates the DES algorithm by using java
 *         security API
 */
public class Cryptography {
private static final String CRYPTOGRAPHY_ALGO_DES = "DES";

private static Cipher cipher = null;
private static DESKeySpec keySpec = null;
private static SecretKeyFactory keyFactory = null;

public static String encrypt(String inputString, String commonKey)
throws InvalidKeyException, IllegalBlockSizeException,
BadPaddingException {

String encryptedValue = "";
SecretKey key = getSecretKey(commonKey);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] inputBytes = inputString.getBytes();
byte[] outputBytes = cipher.doFinal(inputBytes);
encryptedValue = new BASE64Encoder().encode(outputBytes);
return encryptedValue;
}

public static String decrypt(String encryptedString, String commonKey)
throws InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, IOException {
String decryptedValue = "";
encryptedString = encryptedString.replace(' ', '+');
SecretKey key = getSecretKey(commonKey);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] recoveredBytes = cipher.doFinal(new BASE64Decoder()
.decodeBuffer(encryptedString));
decryptedValue = new String(recoveredBytes);
return decryptedValue;
}

private static SecretKey getSecretKey(String secretPassword) {
SecretKey key = null;
try {
cipher = Cipher.getInstance(CRYPTOGRAPHY_ALGO_DES);
keySpec = new DESKeySpec(secretPassword.getBytes("UTF8"));
keyFactory = SecretKeyFactory.getInstance(CRYPTOGRAPHY_ALGO_DES);
key = keyFactory.generateSecret(keySpec);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error in generating the secret Key");
}
return key;
}

}




2) TestCrypto.java




package DESCode;

/**
 *
 */
import DESCode.Cryptography;
/**
 * @author dharmvir.singh
 *
 */
public class TestCrypto {
public static final String DES_ENCRYPTION_KEY = "zastrart";
/**
* @param args
*/
public static void main(String[] args) {
try{
String input  = "Payal";
String encrypted = Cryptography.encrypt(input, DES_ENCRYPTION_KEY);
System.out.println(encrypted);
String decrypted = Cryptography.decrypt("Lvn9GkAMJZU0OXVizYVVeQ==", DES_ENCRYPTION_KEY);
System.out.println(decrypted);
}catch(Exception e){

}
}

}



thanks....
For more information contact
ktdodiya20@gmail.com
+91 8980216689