無為

          無為則可為,無為則至深!

            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 草兒 閱讀(2920) 評論(0)  編輯  收藏 所屬分類: Java編程經驗談
          主站蜘蛛池模板: 衡山县| 宁波市| 元氏县| 宜都市| 塘沽区| 云龙县| 昌平区| 红安县| 吉木萨尔县| 江达县| 揭西县| 深州市| 兴隆县| 维西| 芜湖市| 姜堰市| 阳新县| 利辛县| 全南县| 唐山市| 平南县| 五寨县| 石柱| 仪陇县| 井冈山市| 柳河县| 广昌县| 彭山县| 怀来县| 原阳县| 清水河县| 邻水| 内丘县| 通州市| 香格里拉县| 泰州市| 句容市| 利津县| 深圳市| 安岳县| 梧州市|