1

I have a project to make an encryption and decryption for string input in java. I've been stucked for a week doing some research in it. I really appreciate if you have sample source code or function method for Algorithm AES and Algorithm Twofish in java that I may use in my project. I really need your help ... hope someone out there could be my savior. Thanks very much.

Michael Barz
  • 276
  • 2
  • 11
user3823965
  • 21
  • 1
  • 2
  • 1
    You need to show what you have written, explain your difficulties, give example input and output and many more things than just ask for a complete solution. Stack Overflow is not a free coding service or a debugging tool. It is expected that you put many times more effort into a question than one might to offer a solution. – indivisible Jul 10 '14 at 05:25

1 Answers1

1

For AES you can use java's libraries.

The fallowing code will give you an idea to start.

import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AES {
     public void run() {
         try {
             String text = "Hello World";
             String key = "1234567891234567";
             // Create key and cipher
             Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
             Cipher cipher = Cipher.getInstance("AES");

         // encrypt the text
         cipher.init(Cipher.ENCRYPT_MODE, aesKey);
         byte[] encrypted = cipher.doFinal(text.getBytes());
         System.out.println("Encrypted text: " + new String(encrypted));

         // decrypt the text
         cipher.init(Cipher.DECRYPT_MODE, aesKey);
         String decrypted = new String(cipher.doFinal(encrypted));
         System.out.println("Decrypted text: " + decrypted);
      }catch(Exception e) {
         e.printStackTrace();
      }
    }

    public static void main(String[] args) {
        AES app = new AES();
       app.run();
    }
}
user2640782
  • 1,074
  • 8
  • 15