Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaapplication1;
- import java.security.*;
- import javax.crypto.*;
- public class SymmetricApp {
- public void demoEncryption(){
- try {
- String message = "Selamat datang Java";
- byte[] plainText = message.getBytes("UTF8");
- //get a DES private key
- System.out.print("Generating DES key...");
- KeyGenerator keyGen = KeyGenerator.getInstance("DES");
- keyGen.init(56);
- Key key = keyGen.generateKey();
- System.out.println("Done");
- //get a DES cipher object and print the provider
- Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
- //encypt using the key and the plaintext
- System.out.printf("Encryption...");
- cipher.init(Cipher.ENCRYPT_MODE,key);
- byte[] cipherText = cipher.doFinal(plainText);
- System.out.println("Done");
- // decrypt the ciphertext using the same key
- System.out.print ("Decryption...");
- cipher.init(Cipher.DECRYPT_MODE, key);
- byte[] newPlainText=cipher.doFinal(cipherText);
- System.out.println("Done");
- System.out.println("Message: "+message);
- System.out.println("Encrypted: "+new String(cipherText,"UTF8"));
- System.out.println("Encrypted Hex: "+StringUtils.getHexString(cipherText));
- System.out.println("Plaintex: "+new String (newPlainText,"UTF8"));
- }catch (Exception e){
- System.out.println("Error: "+e.getMessage());
- }
- }
- public static void main(String[] args) {
- SymmetricApp app=new SymmetricApp();
- app.demoEncryption();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement