CSDN博客

img kenly_zhang

JAVA_CRYPTO测试例程:MD5/DES/RSA/SIGN(转)

发表于2004/9/13 17:36:00  1285人阅读

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(javax.crypto.Cipher.ENCRYPT_MODE,key);
  return cipher.doFinal(src);
}

/*******************************************************************************
* RSA私钥解密
*/
static byte[] RSADecrypt(java.security.interfaces.RSAPrivateKey key,byte[] crypt) throws Exception  {
  javax.crypto.Cipher cipher=javax.crypto.Cipher.getInstance("RSA");
  cipher.init(javax.crypto.Cipher.DECRYPT_MODE,key);
  return cipher.doFinal(crypt,0,crypt.length);
}

/*******************************************************************************
* RSA私钥加密
*/
static byte[] RSAEncrypt(java.security.interfaces.RSAPrivateKey key,byte[] src) throws Exception  {
  javax.crypto.Cipher cipher=javax.crypto.Cipher.getInstance("RSA");
  cipher.init(javax.crypto.Cipher.ENCRYPT_MODE,key);
  return cipher.doFinal(src);
}

/*******************************************************************************
* RSA公钥解密
*/
static byte[] RSADecrypt(java.security.interfaces.RSAPublicKey key,byte[] crypt) throws Exception  {
  javax.crypto.Cipher cipher=javax.crypto.Cipher.getInstance("RSA");
  cipher.init(javax.crypto.Cipher.DECRYPT_MODE,key);
  return cipher.doFinal(crypt,0,crypt.length);
}

/*******************************************************************************
* 写系列化对象
*/
 static void RWobject(String filename,Object obj) throws Exception  {
   java.io.FileOutputStream outfile=new java.io.FileOutputStream(filename);
   java.io.ObjectOutputStream outobj=new java.io.ObjectOutputStream(outfile);
   outobj.writeObject(obj);
   outobj.flush();
 }

/*******************************************************************************
* 读系列化对象
*/
 static Object RWobject(String filename) throws Exception  {
   java.io.FileInputStream infile=new java.io.FileInputStream(filename);
   java.io.ObjectInputStream inobj=new java.io.ObjectInputStream(infile);
   Object obj=inobj.readObject();
   return obj;
 }

/*******************************************************************************
* MD5摘要/处理
*/
 static String MD5_show(byte[] md5_byte) throws Exception  {
   java.math.BigInteger big=new java.math.BigInteger(md5_byte);
   return big.toString(16);
 }

/*******************************************************************************
* 生成DES密钥
*/
 static javax.crypto.SecretKey DESKey(byte[] key_byte) throws Exception {
   javax.crypto.spec.DESKeySpec deskeyspec=new javax.crypto.spec.DESKeySpec(key_byte);
   javax.crypto.SecretKeyFactory skf=javax.crypto.SecretKeyFactory.getInstance("DES","SunJCE");
   return (javax.crypto.SecretKey)skf.generateSecret(deskeyspec);
 }

/*******************************************************************************
* DES解密
*/
 static byte[] DESDecrypt(javax.crypto.SecretKey key,byte[] crypt) throws Exception  {
   javax.crypto.Cipher cipher=javax.crypto.Cipher.getInstance("DES/ECB/NoPadding","SunJCE");
   cipher.init(javax.crypto.Cipher.DECRYPT_MODE,key);
   return cipher.doFinal(crypt);
 }

/*******************************************************************************
* DES加密
*/
 static byte[] DESEncrypt(javax.crypto.SecretKey key,byte[] src) throws Exception  {
   javax.crypto.Cipher cipher=javax.crypto.Cipher.getInstance("DES/ECB/NoPadding","SunJCE");
   cipher.init(javax.crypto.Cipher.ENCRYPT_MODE,key);
   return cipher.doFinal(src);
 }

/*******************************************************************************
* 数字签名
*/
 static byte[] Sign(java.security.interfaces.RSAPrivateKey key,byte[] src_byte) throws Exception  {
   java.security.Signature sign=java.security.Signature.getInstance("MD5withRSA");
   sign.initSign(key);
   sign.update(src_byte);
   return sign.sign();
 }

/*******************************************************************************
* 验证签名
*/
 static boolean VSign(java.security.interfaces.RSAPublicKey key,byte[] src_byte,byte[] sign_byte) throws Exception  {
   java.security.Signature sign=java.security.Signature.getInstance("MD5withRSA");
   sign.initVerify(key);
   sign.update(src_byte);
   return sign.verify(sign_byte);
 }
}

0 0

相关博文

我的热门文章

img
取 消
img