WEB开发网
开发学院WEB开发Jsp JAVA_CRYPTO测试例程:MD5/DES/RSA/SIGN 阅读

JAVA_CRYPTO测试例程:MD5/DES/RSA/SIGN

 2008-01-05 09:47:58 来源:WEB开发网   
核心提示:import java.lang.*;import java.util.*;import java.net.*;import java.io.*;import java.security.*;import java.security.interfaces.*;import sun.misc.*;import com.a

  import java.lang.*;
  import java.util.*;
  import java.net.*;
  import java.io.*;
  import java.security.*;
  import java.security.interfaces.*;
  import sun.misc.*;
  import com.apusic.*;
  import sun.security.x509.X509Key;
  public class JAVA_Crypto {
  
   public static void main(String[] args) {
   byte key_module[]=new byte[128];
   byte pukey[]=new byte[3];
   byte PRkey[]=new byte[128];
  
   byte src_byte[]="1234567812345678".getBytes();
  System.out.println(src_byte.length);
   byte key_byte[]="23456781".getBytes();
   int i=0;
  /*******************************************************************************
  * 加载JCE
  */
  // java.security.Security.removeProvider("SUN");
  // java.security.Security.removeProvider("SunRsaSign");
  // java.security.Security.removeProvider("SunJCE");
   java.security.Security.insertProviderAt(new com.apusic.security.provider.Provider(),1);
   java.security.Provider[] p_array=java.security.Security.getProviders();
   for (i=1;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

Tags:JAVA CRYPTO

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接