bouncy castle Cipher IDEA - Java Security

Java examples for Security:Key

Description

bouncy castle Cipher IDEA

Demo Code


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

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

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class ImoocIDEA {

    private static String src = "imooc security idea";

    public static void main(String[] args) {
        bcIDEA();/*  ww w.  ja  v a 2s  .co m*/
    }

    public static void bcIDEA() {
        try {
            Security.addProvider(new BouncyCastleProvider());

            KeyGenerator keyGenerator = KeyGenerator.getInstance("IDEA");
            keyGenerator.init(128);
            SecretKey secretKey = keyGenerator.generateKey();
            byte[] keyBytes = secretKey.getEncoded();

            Key key = new SecretKeySpec(keyBytes, "IDEA");

            Cipher cipher = Cipher.getInstance("IDEA/ECB/ISO10126Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] result = cipher.doFinal(src.getBytes());
            System.out.println("bc idea encrypt : "
                    + Base64.encodeBase64String(result));

            cipher.init(Cipher.DECRYPT_MODE, key);
            result = cipher.doFinal(result);
            System.out.println("bc idea decrypt : " + new String(result));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

Related Tutorials