Hasil dari lihat javadoc class MessageDigest, java.sun.com/j2se/1.4.2/docs/api /java/security/MessageDigest.html, ada beberapa algoritma yang bisa dipakai langsung:
1. MD2: berdasarkan RFC 1319.
2. MD5: berdasarkan RFC 1321.
3. SHA-1: berdasarkan Secure Hash Standard, NIST FIPS 180-1.
4. SHA-256, SHA-384, and SHA-512: algoritma baru berdasarkan draft Federal Information Processing Standard 180-2, Secure Hash Standard (SHS).
Pertamanya mencoba implementasi dengan kode berikut:
public class md5enc {
public md5enc(String inp,String enkripsi) {
byte[]input,output;
java.security.MessageDigest md=null;
input = str2byte(inp);
try{
md = java.security.MessageDigest.getInstance(enkripsi);
}catch(java.security.NoSuchAlgorithmException ex){System.out.println(ex);}
output = md.digest(input);
System.out.println("input:"+byte2str(input));
System.out.println("Enkripsi:"+enkripsi);
System.out.println("output:"+byte2hex(output));
System.out.println("");
}
private byte[] str2byte(String b){
return b.getBytes();
}
private String byte2str(byte[]b){
String o="";
for(int i=0;i<b.length;i++){
char c=(char)b[i];
o+=c;
}
return o;
}
private String byte2hex(byte[]b){
String o="";
for(int i=0;i<b.length;i++){
char c=Long.toHexString(new Byte(b[i]).longValue()).charAt(0);
o+=c;
}
return o;
}
public static void main(String[]a){
new md5enc("Input data","MD5");
new md5enc("Input data","SHA");
}
}
Seperti yang sudah diduga, hasilnya berantakan. Karena salah konversi dari byte ke hexdesimal, sehingga menghasilkan 16, bukan 32, karakter hexadesimal.
input:Input data
Enkripsi:MD5
output:ff1ff7f5553f3f7f
input:Input data
Enkripsi:SHA
output:34fff3f3ffffff5f52f6
Dengan mencari lewat google, akhirnya tinggal memakai yang sudah jadi, lengkap dengan konversi hexa, dari www.anyexample.com, dengan sedikit modifikasi :
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.NoSuchAlgorithmException;
public class AeSimpleMD5 {
private static String convertToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
int halfbyte = (data[i] >>> 4) & 0x0F;
int two_halfs = 0;
do {
if ((0 <= halfbyte) && (halfbyte <= 9))
buf.append((char) ('0' + halfbyte));
else
buf.append((char) ('a' + (halfbyte - 10)));
halfbyte = data[i] & 0x0F;
} while(two_halfs++ < 1);
}
return buf.toString();
}
public static String MD5(String text)
throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md;
md = MessageDigest.getInstance("MD5");
byte[] md5hash = new byte[32];
md.update(text.getBytes("iso-8859-1"), 0, text.length());
md5hash = md.digest();
return convertToHex(md5hash);
}
public static void main(String[] args) throws IOException {
BufferedReader userInput = new BufferedReader (new InputStreamReader(System.in));
System.out.print("Enter string:");
String rawString = userInput.readLine();
try {
System.out.println("MD5 hash of string: " + AeSimpleMD5.MD5(rawString));
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Hasilnya:
===============================================================
Enter string:Hallo
MD5 hash of string: d1bf93299de1b68e6d382c893bf1215f
===============================================================
=======================================================================
berikut ini adalah scrip dari pembuatan aplikasi enkripsi dan dekripsi dengan memanfaatkan metode Caesar Cipher :
==============================================================
package kripto;
/**
*
* @author yoga
*/
public class CaesarCipher
{
public static void main(String[] args) {
String str = "yoga permana";
int key = 3;
String encrypted = encrypt(str, key);
System.out.println(encrypted);
String decrypted = decrypt(encrypted, key);
System.out.println(decrypted);
}
public static String encrypt(String str, int key) {
String encrypted = "";
for(int i = 0; i < str.length(); i++) {
int c = str.charAt(i);
if (Character.isUpperCase(c)) {
c = c + (key % 26);
if (c > 'Z')
c = c - 26;
} else if (Character.isLowerCase(c)) {
c = c + (key % 26);
if (c > 'z')
c = c - 26;
}
encrypted += (char) c;
}
return encrypted;
}
public static String decrypt(String str, int key)
{
String decrypted = "";
for(int i = 0; i < str.length(); i++) {
int c = str.charAt(i);
if (Character.isUpperCase(c)) {
c = c - (key % 26);
if (c < 'A')
c = c + 26;
} else if (Character.isLowerCase(c)) {
c = c - (key % 26);
if (c < 'a')
c = c + 26;
}
decrypted += (char) c;
}
return decrypted;
}
}
SELAMAT MENCOBA
Tidak ada komentar:
Posting Komentar