无为

无为则可为,无为则至深!

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  190 Posts :: 291 Stories :: 258 Comments :: 0 Trackbacks

import javax.crypto.*;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;

/**
 * Created by IntelliJ IDEA.
 * User: LEO
 * Date: 2005-11-30
 * Time: 11:19:23
  */
public class EncryptDecryptData{
    static String str = "南京金脉博通";
    public static void main(String[] args)  {
        System.out.println("明文");
        System.out.println(str);
        byte[] str1 = new byte[0];

        KeyGenerator keygen = null;
        SecretKey secretkey = null;
        try {
            str1 = str.getBytes("gb2312");
            keygen = KeyGenerator.getInstance("DESede");
            keygen.init(168);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        secretkey = keygen.generateKey();
        byte[] kb = secretkey.getEncoded();
        int i=0;
        System.out.println("密钥");
        for(i=0;i<kb.length;i++)
        {
            System.out.print(kb[i]+",");
        }
        Cipher cipher = null;
        byte[] encrypted = new byte[0];
        try {
            cipher = Cipher.getInstance("DESede");
            cipher.init(Cipher.ENCRYPT_MODE,secretkey);
            encrypted = cipher.doFinal(str1);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        System.out.println();
        System.out.println("密文");
        for(i=0;i<encrypted.length;i++)
        {
            System.out.print(encrypted[i]+",");
        }
        byte[] decrypted = new byte[0];
        try {
            cipher.init(Cipher.DECRYPT_MODE,secretkey);
            decrypted = cipher.doFinal(encrypted);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        String str2 = new String(decrypted);
        System.out.println("解密后的原文");
        System.out.println(str2);
    }

}



凡是有该标志的文章,都是该blog博主Caoer(草儿)原创,凡是索引、收藏
、转载请注明来处和原文作者。非常感谢。

posted on 2006-01-06 17:08 草儿 阅读(2911) 评论(0)  编辑  收藏 所属分类: Java编程经验谈

只有注册用户登录后才能发表评论。


网站导航: