Cryptography

Security is not a Product, its a Process

JaVa Crypto

/*
* Created on Apr 5, 2005
*/
package kripto;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.Key;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.CertificateException;
import java.nio.charset.Charset;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;

/**
* @author bangamri
*/
public class Asimetrik {
private static void pembangkitanPasanganKunci(String alias, String namaDepan, String namaBelakang, String unit, String organisasi, String negara, String passwordKunci, String keyStore) {
try{
String perintah=”E://jdk1.5.0/bin/keytool -genkey -alias “+alias+” -keyalg RSA -dname \”cn=”+namaDepan+” “+namaBelakang+”, ou=”+unit+”, o=”+organisasi+”, c=”+negara+”\” -keysize 1024 -keypass “+passwordKunci+” -keystore “+keyStore+” -storepass Ultimate10″;
System.out.println(perintah);
Process proses=Runtime.getRuntime().exec(perintah);
proses.waitFor();

KeyStore ks=KeyStore.getInstance(“JKS”);
FileInputStream is=new FileInputStream(“KeyStore.jks”);
ks.load(is,new String(“Ultimate10”).toCharArray());

Certificate cert=ks.getCertificate(alias);
byte[] buffer=cert.getEncoded();
FileOutputStream os=new FileOutputStream(alias+”.crt”);

Writer wr = new OutputStreamWriter(os, Charset.forName(“UTF-8”));
wr.write(“—–BEGIN CERTIFICATE—–\n”);
wr.write(new sun.misc.BASE64Encoder().encode(buffer));
wr.write(“\n—–END CERTIFICATE—–\n”);
wr.flush();

os.close();

}catch(IOException e){
}catch(InterruptedException e){
}catch(CertificateException e){
}catch(KeyStoreException e){
}catch(NoSuchAlgorithmException e){
}finally{
System.out.println(“Pembangkitan kunci berhasil.”);
}
}

private static void enkrip(String alias, String fileSertifikat, String filePlain, String fileCipher) {
try{
FileInputStream is=new FileInputStream(fileSertifikat);
CertificateFactory cf=CertificateFactory.getInstance(“X.509”);
Certificate cert=cf.generateCertificate(is);
PublicKey pub=cert.getPublicKey();

File pla=new File(filePlain);
File cip=new File(fileCipher);

Cipher enkripsi=Cipher.getInstance(“RSA”);
enkripsi.init(Cipher.ENCRYPT_MODE,pub);
InputStream isPla=new FileInputStream(pla);
OutputStream osCip=new FileOutputStream(cip);
osCip=new CipherOutputStream(osCip,enkripsi);
byte[] buffer=new byte[1024];
int n=0;
while((n=isPla.read(buffer))>=0) {
osCip.write(buffer,0,n);
}
osCip.close();
isPla.close();
}catch(Exception e){
System.out.println(e);
}finally{
System.out.println(“Enkripsi sukses”);
}
}

private static void dekrip(String alias, String fileKeystore, String fileCipher, String filePlain) {
try {
FileInputStream is=new FileInputStream(fileKeystore);
KeyStore ks=KeyStore.getInstance(“JKS”);
ks.load(is, new String(“Ultimate10”).toCharArray());
Key kunci=ks.getKey(alias,new String(“Ultimate10”).toCharArray());
PrivateKey priv=(PrivateKey)kunci;

File fileCip=new File(fileCipher);
File filePla=new File(filePlain);

Cipher dekripsi=Cipher.getInstance(“RSA”);
dekripsi.init(Cipher.DECRYPT_MODE,priv);
InputStream isCip=new FileInputStream(fileCip);
OutputStream osPla=new FileOutputStream(filePla);
isCip=new CipherInputStream(isCip,dekripsi);
byte[] buffer=new byte[1024];
int n=0;
while ((n=isCip.read(buffer))>=0) {
osPla.write(buffer,0,n);
}
isCip.close();
osPla.close();
}catch(Exception e) {
System.out.println(e);
}finally{
System.out.println(“Dekripsi sukses”);
}
}
public static void main(String[] args) {
//pembangkitanPasanganKunci(“alice”,”Alice”,”Silverstone”,”Espionage”,”CIA”,”US”,
“Ultimate10″,”KeyStore.jks”);
enkrip(“alice”,”alice.crt”,”pesan.txt”,”pesan.enc”);
dekrip(“alice”,”KeyStore.jks”,”pesan.enc”,”pesan2.txt”);
}
}

taken from : javatingkir.blogspot.com

January 14, 2007 - Posted by | Cryptography

6 Comments »

  1. cara memakai sertifikat tu gimana ya mas..
    bisa ajarin ga…

    Comment by bayu | August 10, 2008

  2. ajarin donk javascrip..

    Comment by geng | October 5, 2009

  3. Have you ever thought about publishing an ebook or guest authoring on
    other blogs? I have a blog based on the same topics you discuss and would
    really like to have you share some stories/information.
    I know my visitors would enjoy your work. If you’re even remotely interested, feel free to shoot me an e mail.

    Comment by Dental Implants | October 18, 2012

  4. It’s a genuine remedy for infertility concerns.

    Comment by Pregnancy Miracle System | August 2, 2013

  5. Rudraprayag is one particular of the Panch Prayags or
    five confluences of Alaknanda River. It is an ancient temple of beheaded
    Ganesha.

    Comment by Dwain | August 2, 2013

  6. Proteins, vitamins, minerals and amino acids play a large function
    in helping you grow taller.

    Comment by Grow Taller 4 Idiots Results | August 8, 2013


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: