熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Javascript >> 正文

JAVA

2013-11-23 17:57:42  來源: Javascript 

  import javalang*;
  import javautil*;
  import *;
  import javaio*;
  import javasecurity*;
  import javasecurityinterfaces*;
  import sunmisc*;
  import comapusic*;
  import sunsecurityxXKey;
  public class JAVA_Crypto {
  
   public static void main(String[] args) {
   byte key_module[]=new byte[];
   byte pukey[]=new byte[];
   byte prkey[]=new byte[];
  
   byte src_byte[]=getBytes();
  Systemoutprintln(src_bytelength);
   byte key_byte[]=getBytes();
   int i=;
  /*******************************************************************************
  * 加載JCE
  */
  // javasecuritySecurityremoveProvider(SUN);
  // javasecuritySecurityremoveProvider(SunRsaSign);
  // javasecuritySecurityremoveProvider(SunJCE);
   javasecuritySecurityinsertProviderAt(new comapusicsecurityproviderProvider());
   javasecurityProvider[] p_array=javasecuritySecuritygetProviders();
   for (i=;i<=(p_array.length);i++) {
   System.out.println("Provider["+i+"]="+p_array[i-1].getName());
   }
  
   try {
  /*******************************************************************************
  * MD5摘要
  */
  /*
  System.out.println("MD5="+MD5(src_byte));
  System.out.println("MD5_show="+MD5_show(MD5(src_byte)));
  */
  
  /*******************************************************************************
  * 生成DES密鑰
  */
  // javax.crypto.SecretKey deskey;
  /*
   try {
  //讀系列化密鑰
   deskey=(javax.crypto.SecretKey)RWobject("deskey");
   System.out.println("Read DES key OK");
   }
   catch (Exception e) {
   System.out.println("Read Keyfile error,Generate DESkey...");
  //生成DES密鑰
   javax.crypto.KeyGenerator key=javax.crypto.KeyGenerator.getInstance("DES");
   key.init(56);
  // key.init(64);
   deskey=key.generateKey();
  System.out.println("Generator DES KEY OK");
  //寫系列化密鑰
  // RWobject("deskey",(Object)deskey);
   }
  */
  // deskey=DESKey(key_byte);
  
  /*******************************************************************************
  * DES加解密
  */
  /*
   byte[] encrypt,decrypt;
  //加密
   encrypt=DESEncrypt(deskey,src_byte);
  System.out.println("encrypt="+new String(encrypt));
  //解密
   decrypt=DESDecrypt(deskey,encrypt);
  System.out.println("decrypt="+new String(decrypt));
  */
  /*******************************************************************************
  * 生成RSA密鑰對
  */
  
   java.security.KeyPair kp;
   RSAPublicKey publickey;
   RSAPrivateKey privatekey;
   java.io.FileOutputStream fos;
   try {
  
  //讀系列化RSA密鑰
   kp=(java.security.KeyPair)RWobject("rsakey");
   System.out.println("Read RSA key OK");
   }
   catch (Exception e) {
   System.out.println("Read Keyfile Error,Generate RSAkey...");
  //生成RSA密鑰
   KeyPairGenerator kpg=KeyPairGenerator.getInstance("RSA");
   kpg.initialize(1024);
   kp=kpg.generateKeyPair();
  System.out.println("Generate RSA KEY OK="+kp.toString());
  //寫系列化密鑰
   RWobject("rsakey",(Object)kp);
   }
   publickey=(RSAPublicKey)kp.getPublic();
  //System.out.println("PublicKey="+publickey.toString());
   privatekey=(RSAPrivateKey)kp.getPrivate();
  //System.out.println("PrivateKey="+privatekey.toString());
  /*
   try {
   byte[] publickey_byte=publickey.getEncoded();
   publickey=PublicKey(publickey_byte);
  System.out.println("PublicKey="+publickey.toString());
   byte[] privatekey_byte=privatekey.getEncoded();
   privatekey=PrivateKey(privatekey_byte);
  System.out.println("PrivateKey="+privatekey.toString());
   }
   catch (Exception e) {
  System.out.println("Exception:"+e);
   }
  */
   byte[] publickey_byte=publickey.getEncoded();
   byte[] privatekey_byte=privatekey.getEncoded();
  
   fos=new java.io.FileOutputStream("publickey");
   fos.write(publickey_byte);
   fos=new java.io.FileOutputStream("privatekey");
   fos.write(privatekey_byte);
  
   System.arraycopy(publickey_byte,22+8-1,key_module,1-1,128);
   fos=new java.io.FileOutputStream("key_module");
   fos.write(key_module);
   System.arraycopy(publickey_byte,22+138-1,pukey,1-1,3);
   fos=new java.io.FileOutputStream("publickey");
   fos.write(pukey);
   System.arraycopy(privatekey_byte,32+138-1,prkey,1-1,128);
   fos=new java.io.FileOutputStream("privatekey");
   fos.write(prkey);
  
  /*******************************************************************************
  * RSA加解密`
  */
  
  //公鑰加密
   byte[] Endata=RSAEncrypt(publickey,src_byte);
  //System.out.println("Endata="+MD5_show(Endata));
   fos=new java.io.FileOutputStream("public_endata");
   fos.write(Endata);
  //私鑰解密
   byte[] Dedata=RSADecrypt(privatekey,Endata);
  System.out.println("Dedata="+new String(Dedata));
  
  //私鑰加密
   Endata=RSAEncrypt(privatekey,src_byte);
  //System.out.println("Endata="+MD5_show(Endata));
   fos=new java.io.FileOutputStream("private_endata");
   fos.write(Endata);
  //公鑰解密
   Dedata=RSADecrypt(publickey,Endata);
  System.out.println("Dedata="+new String(Dedata));
  
  /*******************************************************************************
  * MD5withRSA簽名
  */
  /*
  //私鑰簽名
   byte[] sign_byte=Sign(privatekey,src_byte);
  System.out.println("Sign="+new String(sign_byte));
  //公鑰驗證簽名
   if (VSign(publickey,src_byte,sign_byte)) {
  System.out.println("Sign OK");
   }
   else {
  System.out.println("Sign ERROR");
   }
  */
   }
   catch (Exception e) {
   System.out.println("Exception:"+e);
   }
  
  }
  
  /*******************************************************************************
  * 生成RSA Public Key
  */
  static java.security.interfaces.RSAPublicKey PublicKey(byte[] public_key_byte) throws Exception {
   java.security.spec.X509EncodedKeySpec ks=new java.security.spec.X509EncodedKeySpec(public_key_byte);
   java.security.KeyFactory kf=java.security.KeyFactory.getInstance("RSA");
   return (java.security.interfaces.RSAPublicKey)kf.generatePublic(ks);
  }
  
  /*******************************************************************************
  * 生成RSA Private Key
  */
  static java.security.interfaces.RSAPrivateKey PrivateKey(byte[] private_key_byte) throws Exception {
   java.security.spec.PKCS8EncodedKeySpec ks=new java.security.spec.PKCS8EncodedKeySpec(private_key_byte);
   java.security.KeyFactory kf=java.security.KeyFactory.getInstance("RSA");
   return (java.security.interfaces.RSAPrivateKey)kf.generatePrivate(ks);
  }
  
  /*******************************************************************************
  * RSA公鑰加密
  */
  static byte[] RSAEncrypt(java.security.interfaces.RSAPublicKey key,byte[] src) throws Exception {
   javax.crypto.Cipher cipher=javax.crypto.Cipher.getInstance("RSA");
   cipher.init(java
From:http://tw.wingwit.com/Article/program/Java/Javascript/201311/25435.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.