Ceriwis  

Go Back   Ceriwis > HOBI > Komputer & Teknologi > Programming

Programming Share, tanya jawab, saling bantu antar programmer dengan berbagai macam bahasa pemrograman.

Reply
 
Thread Tools
  #1  
Old 11th November 2011
Braincode's Avatar
Braincode Braincode is offline
Ceriwis Addicted
 
Join Date: Nov 2011
Posts: 4,638
Rep Power: 20
Braincode mempunyai hidup yang Normal
Default [SHARE] Key Generator java

Setelah Libur ane memutuskan untuk fokus lg di bidang programing...

hehe

di tengah jalan ane nemu ini nih


Code:


import java.security.Key;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class MainClass {
public static void main(String[] args) throws Exception {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider ());
byte[] input = "www.HAHAHIHI.com".getBytes();

byte[] ivBytes = new byte[] { 0x00, 0x00, 0x00, 0x01, 0x04, 0x05, 0x06, 0x07, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01 };

Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC");
KeyGenerator generator = KeyGenerator.getInstance("AES", "BC");

generator.init(192);

Key encryptionKey = generator.generateKey();

System.out.println("key : " + Utils.toHex(encryptionKey.getEncoded()));
System.out.println("input : " + new String(input));

// encryption pass
cipher.init(Cipher.ENCRYPT_MODE, encryptionKey, new IvParameterSpec(ivBytes));
byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
ctLength += cipher.doFinal(cipherText, ctLength);

// decryption pass
Key decryptionKey = new SecretKeySpec(encryptionKey.getEncoded(), encryptionKey.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, decryptionKey, new IvParameterSpec(ivBytes));
byte[] plainText = new byte[cipher.getOutputSize(ctLength)];
int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
ptLength += cipher.doFinal(plainText, ptLength);
System.out.println("plain : " + new String(plainText) + " bytes: " + ptLength);
}
}


class Utils
{
private static String digits = "0123456789abcdef";


public static String toHex(byte[] data, int length)
{
StringBuffer buf = new StringBuffer();

for (int i = 0; i != length; i++)
{
int v = data[i] & 0xff;

buf.append(digits.charAt(v >> 4));
buf.append(digits.charAt(v & 0xf));
}

return buf.toString();
}


public static String toHex(byte[] data)
{
return toHex(data, data.length);
}
}

Untuk KEy Generator MAC


Code:

import java.security.Security;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;

public class MainClass {
public static void main(String args[]) throws Exception {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider ());
String inputString = "Stringnya disini";

KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5");
SecretKey secretKey = keyGen.generateKey();

Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);

byte[] byteData = inputString.getBytes("UTF8");

byte[] macBytes = mac.doFinal(byteData);

String macAsString = new sun.misc.BASE64Encoder().encode(macBytes);

System.out.println("Authentication code is: " + macAsString);
}
}

UNTUK ASYMETHRIC KEY




Code:

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class Main {
public static void main(String[] argv) throws Exception {
// Generate a DES key
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
SecretKey key = keyGen.generateKey();

// Generate a Blowfish key
keyGen = KeyGenerator.getInstance("Blowfish");
key = keyGen.generateKey();

// Generate a triple DES key
keyGen = KeyGenerator.getInstance("DESede");
key = keyGen.generateKey();
}
}

moga2 bisa ngebantu agan2 disini



Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


 


All times are GMT +7. The time now is 08:29 PM.


no new posts