HelloWorld 善战者,求之于势,不责于人;故能择人而任势。

知止而后有定,定而后能静,静而后能安,安而后能虑,虑而后能得。物有本末,事有终始。知所先后,则近道矣。

  BlogJava :: 首页 ::  :: 联系 ::  :: 管理 ::
  167 随笔 :: 1 文章 :: 40 评论 :: 0 Trackbacks

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class JcaTest {
 public static void main(String args[]) throws Exception {
  SecureRandom sr = new SecureRandom();
  KeyGenerator kg = KeyGenerator.getInstance("DES");
  kg.init(sr);
  SecretKey key = kg.generateKey();
  // 获得密匙数据
  byte rawKeyData[] = key.getEncoded();
  Cipher cipher = Cipher.getInstance("DES");
  cipher.init(Cipher.ENCRYPT_MODE, key, sr);

  String src = "你好吗测试加密";
  byte[] dest = cipher.doFinal(src.getBytes());
  System.out.println("加密后的数据:"+new String(dest));

  // 解密 从原始密匙数据创建一个DESKeySpec对象
  sr = new SecureRandom();
  DESKeySpec dks = new DESKeySpec(rawKeyData);
  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  SecretKey keys = keyFactory.generateSecret(dks);
  Cipher ciphers = Cipher.getInstance("DES");
  ciphers.init(   Cipher.DECRYPT_MODE,keys,sr);  
  byte[] srcs = ciphers.doFinal(dest);
  System.out.println("解密后的数据:"+new String(srcs));
 }
}



</script>

posted on 2007-09-03 09:29 helloworld2008 阅读(391) 评论(0)  编辑  收藏 所属分类: java - jca

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


网站导航: