Friday, February 14, 2014

java


import java.io.BufferedReader;
import java.io.InputStreamReader;

public class MainClass {

public static void main(String args[]) {
try {
System.out.println("enter your name");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String str = br.readLine();

System.out.println("You entered "+str);
return;

} catch (Exception e) {
e.printStackTrace();
}
}
}

Tuesday, January 21, 2014


Custom Radio Button GKIndia



                 

 <RadioButton
                        android:id="@+id/rb1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:button="@drawable/button_radio"
                        android:gravity="center_vertical"
                        android:text="" />

Tuesday, November 12, 2013

Encrypt and Decrypt any File IN Android or Java

Encrypt and Decrypt any File IN Android or Java



package com.example.encryption;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String key = "squirrel123"; // needs to be at least 8 characters for DES
try{ Log.e("started", "");
FileInputStream fis = new FileInputStream("sdcard/raj.jpg");

FileOutputStream fos = new FileOutputStream("sdcard/c.txt");
encrypt(key, fis, fos);

FileInputStream fis2 = new FileInputStream("sdcard/c.txt");
FileOutputStream fos2 = new FileOutputStream("sdcard/c.jpg");
decrypt(key, fis2, fos2);
}catch(Exception e){e.printStackTrace();} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public static void encrypt(String key, InputStream is, OutputStream os) throws Throwable {
encryptOrDecrypt(key, Cipher.ENCRYPT_MODE, is, os);
}

public static void decrypt(String key, InputStream is, OutputStream os) throws Throwable {
encryptOrDecrypt(key, Cipher.DECRYPT_MODE, is, os);
}

public static void encryptOrDecrypt(String key, int mode, InputStream is, OutputStream os) throws Throwable {

DESKeySpec dks = new DESKeySpec(key.getBytes());
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey desKey = skf.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES"); // DES/ECB/PKCS5Padding for SunJCE

if (mode == Cipher.ENCRYPT_MODE) {
cipher.init(Cipher.ENCRYPT_MODE, desKey);
CipherInputStream cis = new CipherInputStream(is, cipher);
doCopy(cis, os);
} else if (mode == Cipher.DECRYPT_MODE) {
cipher.init(Cipher.DECRYPT_MODE, desKey);
CipherOutputStream cos = new CipherOutputStream(os, cipher);
doCopy(is, cos);
}
}

public static void doCopy(InputStream is, OutputStream os) throws IOException {
byte[] bytes = new byte[64];
int numBytes;
while ((numBytes = is.read(bytes)) != -1) {
os.write(bytes, 0, numBytes);
}
os.flush();
os.close();
is.close();
}
}

Monday, November 11, 2013

List Of Remote Folders

Create a List Of Remote Folders using JSCH for android, Java in a ListView In Dialog Box.




import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

import com.example.newsync.R;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Listremote extends Activity{
ChannelSftp sftpChannel;
ListAdapter adapter;
ArrayAdapter<String> adaptor;
com.jcraft.jsch.ChannelSftp.LsEntry lsEntry ;
private Item[] fileList;
ArrayList<String> list = new ArrayList<String>();
String path;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


JSch jsch = new JSch();
Session session = null;
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
session = jsch.getSession("USER Name", "Host Address", 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword("Password");
session.connect();
Channel channel = session.openChannel("sftp");
Handler h = new Handler(Listremote.this.getMainLooper());
h.post(new Runnable() {
@Override
public void run() {
Toast.makeText(Listremote.this, "Connecting",
Toast.LENGTH_LONG).show();
}
});
channel.connect();
h.post(new Runnable() {
@Override
public void run() {
Toast.makeText(Listremote.this, "Connected",
Toast.LENGTH_LONG).show();
}
});
sftpChannel = (ChannelSftp) channel;
sftpChannel.cd(".");


try {

if(path==null){
Log.e("path null", "path null");
path=".";

}
@SuppressWarnings("rawtypes")
Vector files = sftpChannel.ls(path);
if (files.size() == 0) {
Toast.makeText(getApplicationContext(),
"No files are available for download.", 4)
.show();
} else {
for (int i = 0; i < files.size(); i++) {
lsEntry = (com.jcraft.jsch.ChannelSftp.LsEntry) files
.get(i);
if (lsEntry.getAttrs().isDir()&& !lsEntry.getFilename().equals(".")
&& !lsEntry.getFilename().equals("..")&&!lsEntry.getFilename().startsWith(".")&&!lsEntry.getFilename().startsWith("..")){
// Log.e(lsEntry.toString(), "files");
//List items = Arrays.asList(lsEntry.getFilename());
list.add(lsEntry.getFilename());

//Log.e(""+items.toString(), "items");
// Log.e(lsEntry.getAttrs().getSize()+"","lsentry Size");
// Log.e(lsEntry+"","list Size");
fileList = new Item[list.size()];

for (int j = 0; j < list.size(); j++) {
fileList[j] = new Item(list.toString(), R.drawable.directory_icon);

}
Log.e("file list "+fileList, "list "+list.toString());
//List<String> colorChoices = list;
//Log.e(colorChoices+"","list ");
adaptor = new ArrayAdapter<String>(
this,android.R.layout.select_dialog_item, android.R.id.text1, list){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// creates view
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view
.findViewById(android.R.id.text1);

// put the image on the text view
textView.setCompoundDrawablesWithIntrinsicBounds(
fileList[position].icon, 0, 0, 0);

// add margin between image and text (support various screen
// densities)
int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
textView.setCompoundDrawablePadding(dp5);

return view;
}
};


}

}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Remote Files");

builder.setAdapter(adaptor, new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int item) {
    String itemname = adaptor.getItem(item);
    Log.e(""+itemname,"file Clicked");
   // String chosenFile = fileList[item].file;
    path=itemname;

   }
});
builder.show();}
}catch(Exception e){e.printStackTrace();}

//sftpChannel.put("" + sel, "Desktop/");

//String r = userId + "." + sel.getName();
//sftpChannel.rename("Desktop/" + sel.getName(),
// "Desktop/" + r);
h.post(new Runnable() {
@Override
public void run() {
try {
/*
AlertDialog.Builder builder = new AlertDialog.Builder(
Listremote.this);
builder.setMessage(
"File Sent Successfully")
.setCancelable(false)
.setPositiveButton(
"Listremote More",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
//loadFileList();
//showDialog(1);

}
});
builder.setNegativeButton(
"Not now",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
//dialog.cancel();
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
*/
} catch (Exception e) {
e.printStackTrace();
}
}
});
sftpChannel.exit();
session.disconnect();
//Log.e("" + sel, "FileClicked");
} catch (final JSchException e) {
runOnUiThread(new Runnable() {
@SuppressLint("ShowToast")
@Override
public void run() {
Toast.makeText(
Listremote.this,
"Check Host, Username and Password "
+ e.getMessage().toString(),
10).show();
}
});

e.printStackTrace();
} catch (final SftpException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(
Listremote.this,
"error - server not responding"
+ e.getMessage().toString(),
Toast.LENGTH_LONG).show();
}
});
e.printStackTrace();

}


}private class Item {
public String file;
public int icon;

public Item(String file, Integer icon) {
this.file = file;
this.icon = icon;
}

@Override
public String toString() {
return file;
}
}

}