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.
    }
}

Tidak ada komentar:

Posting Komentar