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