Kamis, 15 Desember 2016
RC4 Java Code
package lp2maray.com;
import javax.crypto.spec.*;
import java.security.*;
import javax.crypto.*;
public class RC4{
private static String algorithm = "RC4";
public static void main(String []args) throws Exception {
String toEncrypt = "apa kabar bro";
System.out.println("Encrypting...");
byte[] encrypted = encrypt(toEncrypt, "123");//password
System.out.println("Encrypted text : "+toEncrypt);
String hasil="";
String[] data1 = new String[encrypted.length];
for(int i=0; i < encrypted.length; i++) {
data1[i] = String.valueOf(encrypted[i] & 0xff); //unsigned integer
hasil+=data1[i];
}
System.out.println("Decrypting...");
String decrypted = decrypt(encrypted, "123");//password
System.out.println("Decrypted text: " + decrypted);
}
//
// void intis() throws Exception{
// String toEncrypt = "The shorter you live, the longer you're dead!";
//
// System.out.println("Encrypting...");
// byte[] encrypted = encrypt(toEncrypt, "password");
// System.out.println("Decrypting...");
// String decrypted = decrypt(encrypted, "password");
// System.out.println("Decrypted text: " + decrypted);
// }
public static byte[] encrypt(String toEncrypt, String key) throws Exception {
// create a binary key from the argument key (seed)
SecureRandom sr = new SecureRandom(key.getBytes());
KeyGenerator kg = KeyGenerator.getInstance(algorithm);
kg.init(sr);
SecretKey sk = kg.generateKey();
// create an instance of cipher
Cipher cipher = Cipher.getInstance(algorithm);
// initialize the cipher with the key
cipher.init(Cipher.ENCRYPT_MODE, sk);
// enctypt!
byte[] encrypted = cipher.doFinal(toEncrypt.getBytes());
return encrypted;
}
public static String decrypt(byte[] toDecrypt, String key) throws Exception {
// create a binary key from the argument key (seed)
SecureRandom sr = new SecureRandom(key.getBytes());
KeyGenerator kg = KeyGenerator.getInstance(algorithm);
kg.init(sr);
SecretKey sk = kg.generateKey();
// do the decryption with that key
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE, sk);
byte[] decrypted = cipher.doFinal(toDecrypt);
return new String(decrypted);
}
String decrypt(String gabEnkrip, String PASS) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
RC6 Java Code
package lp2maray.com;
public class RC6 {
private int w=32, r=20;
private int Pw=0xb7e15163, Qw=0x9e3779b9;
private int[] S;
// private String filename;
// private String directory;
// private String fileNameEncrypt;
// private String extension;
// public void setFile(String directory, String filename, String extens){
// this.fileNameEncrypt = directory + "/" + "en." + filename + "." + extens;
// System.out.println("fileNameEncrypt="+fileNameEncrypt);
// }
private int[] convBytesWords(byte[] key, int u, int c) {
int[] tmp = new int[c];
for (int i = 0; i < tmp.length; i++)
tmp[i] = 0;
for (int i = 0, off = 0; i < c; i++)
tmp[i] = ((key[off++] & 0xFF)) | ((key[off++] & 0xFF) << 8)
| ((key[off++] & 0xFF) << 16) | ((key[off++] & 0xFF) << 24);
return tmp;
}
//penjadwalan kunci RC 6 mencampurkan aray S dengan L
private int[] generateSubkeys(byte[] key) {
int u = w / 8;
int c = key.length / u;
int t = 2 * r + 4;
int[] L = convBytesWords(key, u, c);
int[] S = new int[t];
S[0] = Pw;
for (int i = 1; i < t; i++)
S[i] = S[i - 1] + Qw;
int A = 0;
int B = 0;
int k = 0, j = 0;
int v = 3 * Math.max(c, t);
for (int i = 0; i < v; i++) {
A = S[k] = rotl((S[k] + A + B), 3);
B = L[j] = rotl(L[j] + A + B, A + B);
k = (k + 1) % t;
j = (j + 1) % c;
}
return S;
}
//penggeseran bit kekiri
private int rotl(int val, int pas) {
return (val << pas) | (val >>> (32 - pas));
}
//penggeseran bit ke kanan
private int rotr(int val, int pas) {
return (val >>> pas) | (val << (32-pas));
}
//memecah blok cipertext kedalam 4 register
private byte[] decryptBloc(byte[] input) {
byte[] tmp = new byte[input.length];
int t,u;
int aux;
int[] data = new int[input.length/4];
for(int i =0;i<data.length;i++)
data[i] = 0;
int off = 0;
for(int i=0;i<data.length;i++){
data[i] = ((input[off++]&0xff))|
((input[off++]&0xff) << 8) |
((input[off++]&0xff) << 16) |
((input[off++]&0xff) << 24);
}
int A = data[0],B = data[1],C = data[2],D = data[3];
C = C - S[2*r+3];
A = A - S[2*r+2];
for(int i = r;i>=1;i--) {
aux = D;
D = C;
C = B;
B = A;
A = aux;
u = rotl(D*(2*D+1),5);
t = rotl(B*(2*B + 1),5);
C = rotr(C-S[2*i + 1],t) ^ u;
A = rotr(A-S[2*i],u) ^ t;
}
D = D - S[1];
B = B - S[0];
data[0] = A;data[1] = B;data[2] = C;data[3] = D;
for(int i = 0;i<tmp.length;i++) {
tmp[i] = (byte)((data[i/4] >>> (i%4)*8) & 0xff);
}
return tmp;
}
//memecah blok plaintext kedalam 4 register
private byte[] encryptBloc(byte[] input) {
byte[] tmp = new byte[input.length];
int t,u;
int aux;
int[] data = new int[input.length/4];
for(int i =0;i<data.length;i++)
data[i] = 0;
int off = 0;
for(int i=0;i<data.length;i++) {
data[i] = ((input[off++]&0xff))|
((input[off++]&0xff) << 8) |
((input[off++]&0xff) << 16) |
((input[off++]&0xff) << 24);
}
int A = data[0],B = data[1],C = data[2],D = data[3];
B = B + S[0];
D = D + S[1];
for(int i = 1;i<=r;i++) {
t = rotl(B*(2*B+1),5);
u = rotl(D*(2*D+1),5);
A = rotl(A^t,u)+S[2*i];
C = rotl(C^u,t)+S[2*i+1];
aux = A;
A = B;
B = C;
C = D;
D = aux;
}
A = A + S[2*r+2];
C = C + S[2*r+3];
data[0] = A;data[1] = B;data[2] = C;data[3] = D;
for(int i = 0;i<tmp.length;i++) {
tmp[i] = (byte)((data[i/4] >>> (i%4)*8) & 0xff);
}
return tmp;
}
//proses padding awal dalam penjadwalan kunci
private static byte[] paddingKey(byte[] key){
int l=0;
if(key.length==1){
l=3;
}
else
l=key.length%4;
byte[]key2=new byte[key.length+l];
for(int i=0;i<key2.length;i++){
if(i<key.length){
key2[i]=key[i];
}
else{
key2[i]=0;
}
}
return key2;
}
//fungsi untuk melakukan enkripsi RC6
public byte[] encrypt(byte[] data, byte[] key) {
byte[] bloc = new byte[16];
key = paddingKey(key);
S = generateSubkeys(key);
int lenght = 16 - data.length % 16;
byte[] padding = new byte[lenght];
padding[0] = (byte) 0x80;
for (int i = 1; i < lenght; i++)
padding[i] = 0;
int count = 0;
byte[] tmp = new byte[data.length+lenght];
int i;
for(i=0;i<data.length+lenght;i++) {
if(i>0 && i%16 == 0) {
bloc = encryptBloc(bloc);
System.arraycopy(bloc, 0, tmp, i-16, bloc.length);
}
if (i < data.length)
bloc[i % 16] = data[i];
else {
bloc[i % 16] = padding[count];
count++;
if(count>lenght-1) count = 1;
}
}
bloc = encryptBloc(bloc);
System.arraycopy(bloc, 0, tmp, i - 16, bloc.length);
return tmp;
}
//fungsi untuk melakukan dekripsi RC6
public byte[] decrypt(byte[] data, byte[] key) {
byte[] tmp = new byte[data.length];
byte[] bloc = new byte[16];
key = paddingKey(key);
S = generateSubkeys(key);
int i;
for(i=0;i<data.length;i++) {
if(i>0 && i%16 == 0) {
bloc = decryptBloc(bloc);
System.arraycopy(bloc, 0, tmp, i-16, bloc.length);
}
if (i < data.length)
bloc[i % 16] = data[i];
}
bloc = decryptBloc(bloc);
System.arraycopy(bloc, 0, tmp, i - 16, bloc.length);
tmp = deletePadding(tmp);
return tmp;
}
//proses penghilangan padding pada key
private byte[] deletePadding(byte[] input) {
int count = 0;
int i = input.length - 1;
while (input[i] == 0) {
count++;
i--;
}
byte[] tmp = new byte[input.length - count - 1];
System.arraycopy(input, 0, tmp, 0, tmp.length);
return tmp;
}
public static String byteArrayToHexString(byte[] b) {
StringBuffer sb = new StringBuffer(b.length * 2);
for (int i = 0; i < b.length; i++) {
int v = b[i] & 0xff;
if (v < 16) {
sb.append('0');
}
sb.append(Integer.toHexString(v));
}
return sb.toString().toUpperCase();
}
// public byte[] encrypt(String content, byte[] bytes) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
// }
//
// public String decrypt(byte[] bcs, String kunci) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
// }
}
Source Code Kriptografi
String doc2String(String fileName) {
BufferedReader be = null;
String data = "";
try {
String current;
int s;
be = new BufferedReader(new FileReader(fileName));
while ((current = be.readLine()) != null) {
data += current;
}
}
catch(Exception ee){}
return data;
}
String doc2String(String fileName) throws IOException{
String gab="";
Path path = Paths.get(fileName);
byte[] data = Files.readAllBytes(path);
for(int i=0; i < data.length; i++) {
char c=(char)(int)data[i];
gab=gab+c;
}
return gab;
}
byte[]doc2Byte(String fileName){
Path path = Paths.get(fileName);
byte[] data = Files.readAllBytes(path); //membaca semua bytes yang ada di object
String[] data1 = new String[data.length];
for(int i=0; i < data.length; i++) {
data1[i] = String.valueOf(data[i] & 0xff); //unsigned integer
}
return data1;
}
byte[] doc2Byte(String al)throws IOException{
Path path = Paths.get(al);
byte[] data = Files.readAllBytes(path);
return data;
}
+++++++++++++++++++++++++++++++++++
void saveByte(byte[] data, String namaFile) throws IOException {
Path path = Paths.get(namaFile);
Files.write(path, dataDecrypt);
}
void saveString(String data,String namaFile) {
try {
String content = data;
File file = new File(namaFile);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
===========================
String byte2String(byte[]data){
String gab="";
for(int i=0; i < data.length; i++) {
char c=(char)(int)data[i];
gab=gab+c;
}
return gab;
}
byte[] string2Byte(String data){
char[]ar=data.toCharArray();
byte[]bit=new byte[ar.length];
for(int i=0; i < ar.length; i++) {
int c=(int)ar[i];
bit[i]=(byte)c;
}
return bit;
}
++++++++++++++++++++++++++++++
String getNamaFile(String fileName){
int dot = fileName.lastIndexOf(".");
int sep = fileName.replace("\\", "/").lastIndexOf("/");
String NF=fileName.substring(sep + 1, dot);
String extendeed= fileName.substring(dot + 1);
NF=NF+"."+extendeed;
return NF;
}
String getExt(String fileName){
int dot = fileName.lastIndexOf(".");
String extendeed= fileName.substring(dot + 1);
return extendeed;
}
boolean cekFile(String nf){
boolean sudah=true;
File file = new File(nf);
if (!file.exists()) {
sudah=false;
}
return sudah;
}
boolean cekFileDel(String nf){
boolean sudah=false;
File file = new File(nf);
if (file.exists()) {
file.delete();
sudah=true;
}
return sudah;
}
++++++++++++++
String sTB(byte[]result){
String str = Base64.encode(result);//encodeBase64String
return str;
}
byte [] sTB2(String s){
char [] x = s.toCharArray();
byte [] hs = new byte[x.length];
for (int i = 0;i<x.length;i++){
hs [i] = (byte) (int)x[i];
}
return hs;
}
byte[] sTB (String s) throws Base64DecodingException{
byte [] hs = Base64.decode(s);
return hs;
}
byte[]btS(String content) throws Base64DecodingException{
byte[]dika=Base64.decode(content);
return dika;
}
String bTS(byte[]result){
String str = Base64.encode(result);//encodeBase64String
return str;
}
private void saveData(String dataEncrypt,String mpath) {
String kompress=dataEncrypt;//lz.compress(dataEncrypt, null);
char[]ar2=kompress.toCharArray();
byte[]data3=new byte[ar2.length];
for(int i=0;i<ar2.length;i++){
data3[i]=(byte)(int)ar2[i];
}
try{
this.saveByte(data3,mpath);
} catch (Exception e) {
e.printStackTrace();
}
}
private void saveByte(byte[] dataDecrypt,String fileNameEncrypt) throws IOException {
Path path = Paths.get(fileNameEncrypt);
Files.write(path, dataDecrypt); //creates, overwrites
System.out.println("File Berhasil di dekripsi ! lihat di : " +fileNameEncrypt);
}
static void saveString(String dataEncrypt,String namaFile) {
try {
String content = dataEncrypt;
File file = new File(namaFile);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Senin, 28 November 2016
Android Login Using .txt
package com.example.lp2maray;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.content.Context;
import android.util.Log;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Menu_Login extends ActionBarActivity {
EditText edPass,edUser;
private static final String TAG = Menu_Login.class.getName();
private static final String FILENAME = "myFiles.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
String textFromFileString = "";
try{
textFromFileString=readFromFile();
if(textFromFileString.length()<1){
String passDefault = "admin#admin#";
writeToFile(passDefault);
}
}catch(Exception ee){}
edUser=(EditText)findViewById(R.id.editText);
edPass=(EditText)findViewById(R.id.editText2);
Button btnLogin=(Button)findViewById(R.id.button);
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
String user=edUser.getText().toString();
String pass=edPass.getText().toString();
if(user.length()<1){
lengkapi("User");
}
else if(pass.length()<1){
lengkapi("Pass");
}
else{
String textToSaveString=readFromFile();
String[]ar=textToSaveString.split("#");
if ( user.equalsIgnoreCase(ar[0]) && pass.equalsIgnoreCase(ar[1]) )
sukses();
else
gagal();
}
}});
}
private void writeToFile(String data) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput(FILENAME, Context.MODE_PRIVATE));
outputStreamWriter.write(data);
outputStreamWriter.close();
}
catch (IOException e) {
Log.e(TAG, "File write failed: " + e.toString());
}
}
private String readFromFile() {
String ret = "";
try {
InputStream inputStream = openFileInput(FILENAME);
if ( inputStream != null ) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
}
}
catch (FileNotFoundException e) {
Log.e(TAG, "File Tidak Ditemukan : " + e.toString());
} catch (IOException e) {
Log.e(TAG, "Tidak Dapat Membaca File: " + e.toString());
}
return ret;
}
public void keluar(){
new AlertDialog.Builder(this)
.setTitle("Menutup Aplikasi")
.setMessage("Terimakasih... Anda Telah Menggunakan Aplikasi Ini")
.setNeutralButton("Tutup", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
finish();
}})
.show();
}
public void sukses(){
new AlertDialog.Builder(this)
.setTitle("Sukses Masuk")
.setMessage("Berhasil Masuk...")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
edPass.setText("");
edUser.setText("");
Intent i = new Intent(Menu_Login.this, Menu_Utama.class);
startActivity(i);
}})
.show();
}
public void gagal(){
new AlertDialog.Builder(this)
.setTitle("Login Gagal")
.setMessage("Maaf, Anda Gagal Tidak Bisa Masuk.. Silakan Cek Kembali")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}})
.show();
}
public void lengkapi(String u){
new AlertDialog.Builder(this)
.setTitle("Login Gagal")
.setMessage("Maaf, Silakan Isi Data "+u+" Anda")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}})
.show();
}
public void keluarYN(){
AlertDialog.Builder ad=new AlertDialog.Builder(Menu_Login.this);
ad.setTitle("Konfirmasi");
ad.setMessage("Apakah Benar Ingin Keluar?");
ad.setPositiveButton("Yes",new OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}});
ad.setNegativeButton("No",new OnClickListener(){
public void onClick(DialogInterface arg0, int arg1) {
}});
ad.show();
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
keluarYN();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Intent io = this.getIntent();
myLati=io.getStringExtra("myLati");
myLongi=io.getStringExtra("myLongi");
myPosisi=io.getStringExtra("myPosisi");
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.content.Context;
import android.util.Log;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Menu_Login extends ActionBarActivity {
EditText edPass,edUser;
private static final String TAG = Menu_Login.class.getName();
private static final String FILENAME = "myFiles.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
String textFromFileString = "";
try{
textFromFileString=readFromFile();
if(textFromFileString.length()<1){
String passDefault = "admin#admin#";
writeToFile(passDefault);
}
}catch(Exception ee){}
edUser=(EditText)findViewById(R.id.editText);
edPass=(EditText)findViewById(R.id.editText2);
Button btnLogin=(Button)findViewById(R.id.button);
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
String user=edUser.getText().toString();
String pass=edPass.getText().toString();
if(user.length()<1){
lengkapi("User");
}
else if(pass.length()<1){
lengkapi("Pass");
}
else{
String textToSaveString=readFromFile();
String[]ar=textToSaveString.split("#");
if ( user.equalsIgnoreCase(ar[0]) && pass.equalsIgnoreCase(ar[1]) )
sukses();
else
gagal();
}
}});
}
private void writeToFile(String data) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput(FILENAME, Context.MODE_PRIVATE));
outputStreamWriter.write(data);
outputStreamWriter.close();
}
catch (IOException e) {
Log.e(TAG, "File write failed: " + e.toString());
}
}
private String readFromFile() {
String ret = "";
try {
InputStream inputStream = openFileInput(FILENAME);
if ( inputStream != null ) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
}
}
catch (FileNotFoundException e) {
Log.e(TAG, "File Tidak Ditemukan : " + e.toString());
} catch (IOException e) {
Log.e(TAG, "Tidak Dapat Membaca File: " + e.toString());
}
return ret;
}
public void keluar(){
new AlertDialog.Builder(this)
.setTitle("Menutup Aplikasi")
.setMessage("Terimakasih... Anda Telah Menggunakan Aplikasi Ini")
.setNeutralButton("Tutup", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
finish();
}})
.show();
}
public void sukses(){
new AlertDialog.Builder(this)
.setTitle("Sukses Masuk")
.setMessage("Berhasil Masuk...")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
edPass.setText("");
edUser.setText("");
Intent i = new Intent(Menu_Login.this, Menu_Utama.class);
startActivity(i);
}})
.show();
}
public void gagal(){
new AlertDialog.Builder(this)
.setTitle("Login Gagal")
.setMessage("Maaf, Anda Gagal Tidak Bisa Masuk.. Silakan Cek Kembali")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}})
.show();
}
public void lengkapi(String u){
new AlertDialog.Builder(this)
.setTitle("Login Gagal")
.setMessage("Maaf, Silakan Isi Data "+u+" Anda")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}})
.show();
}
public void keluarYN(){
AlertDialog.Builder ad=new AlertDialog.Builder(Menu_Login.this);
ad.setTitle("Konfirmasi");
ad.setMessage("Apakah Benar Ingin Keluar?");
ad.setPositiveButton("Yes",new OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}});
ad.setNegativeButton("No",new OnClickListener(){
public void onClick(DialogInterface arg0, int arg1) {
}});
ad.show();
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
keluarYN();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Intent io = this.getIntent();
myLati=io.getStringExtra("myLati");
myLongi=io.getStringExtra("myLongi");
myPosisi=io.getStringExtra("myPosisi");
String message = "Text I want to share.";
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(share, "Title of the dialog the system will open"));
exact package name dari aplikasi
Facebook - "com.facebook.katana"
Twitter - "com.twitter.android"
Instagram - "com.instagram.android"
Pinterest - "com.pinterest"
====
Intent intent = context.getPackageManager().getLaunchIntentForPackage(application);
if (intent != null) {
// The application exists
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setPackage(application);
shareIntent.putExtra(android.content.Intent.EXTRA_TITLE, title);
shareIntent.putExtra(Intent.EXTRA_TEXT, description);
// Start the specific social application
context.startActivity(shareIntent);
} else {
// The application does not exist
// Open GooglePlay or use the default system picker
}
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is the text that will be shared.");
startActivity(Intent.createChooser(sharingIntent,"Share using"));
Sharing binary objects (Images, videos etc.)
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse(path);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
http://stackoverflow.com/questions/6814268/android-share-on-facebook-twitter-mail-ecc
Minggu, 27 November 2016
Caesar Chipper
package com.sanfoundry.setandstring;
import java.util.Scanner;
public class CaesarCipher
{
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
public static String encrypt(String plainText, int shiftKey)
{
plainText = plainText.toLowerCase();
String cipherText = "";
for (int i = 0; i < plainText.length(); i++)
{
int charPosition = ALPHABET.indexOf(plainText.charAt(i));
int keyVal = (shiftKey + charPosition) % 26;
char replaceVal = ALPHABET.charAt(keyVal);
cipherText += replaceVal;
}
return cipherText;
}
public static String decrypt(String cipherText, int shiftKey)
{
cipherText = cipherText.toLowerCase();
String plainText = "";
for (int i = 0; i < cipherText.length(); i++)
{
int charPosition = ALPHABET.indexOf(cipherText.charAt(i));
int keyVal = (charPosition - shiftKey) % 26;
if (keyVal < 0)
{
keyVal = ALPHABET.length() + keyVal;
}
char replaceVal = ALPHABET.charAt(keyVal);
plainText += replaceVal;
}
return plainText;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String for Encryption: ");
String message = new String();
message = sc.next();
System.out.println(encrypt(message, 3));
System.out.println(decrypt(encrypt(message, 3), 3));
sc.close();
}
}
Langganan:
Postingan (Atom)