Java

xiaoxiao2021-03-06  47

Import java.LANG. *; import java.util. *; 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 [] = new byte powder [] = new byte [128];

BYTE SRC_BYTE [] = "1234567812345678" .GetBytes (); system.out.println (src_byte.Length); Byte key_byte [] = "23456781" .GetBytes (); int i = 0; / ******* *********************************************************** ********************** Load 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 summary * // * system.out.println ("md5 =" MD5 (src_byte)); System.out.Println ("MD5_SHOW =" MD5_SHOW (MD5 (SRC_BYTE))); * /

/ ************************************************** ****************************** Generate DES Key * /// javax.crypto.secretkey deskey; / * try { // Read Serialization Key Deskey = (Javax.crypto.secretkey) RWObject ("deskey"); System.Out.println ("Read des Key OK");} catch (exception e) {system.out.println "Read Keyfile Error, Generate Deskey ..."); // Generates DES Key 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"); // Write a series of key // rwobject ("deskey", (Object) deskey);} * /// deskey = deskey (key_byte); / ************************************************ ***************************************** DES add credibility * // * Byte [ ] encrypt, decrypt; // encryption encrypt = DESEncrypt (deskey, src_byte); System.out.println ( "encrypt =" new String (encrypt)); // decryption decrypt = DESDecrypt (deskey, encrypt); System.out .Println ("decrypt =" new string (decrypt)); * // **************************************** *** ******************************************** Born RSA Key Pair * /

Java.security.keypair kp; rsapublicKey publickey; java.io.fileoutputStream fos; try {

// Read Serialization RSA Key KP = (Java.Security.Keypair) RWObject ("Rsakey"); System.out.Println ("Read RSA Key OK");} catch (Exception E) {system.out.println ("Read Keyfile Error, Generate Rsakey ..."); // Generate RSA Key KeypairGenerator KPG = KeypairGenerator.getInstance ("RSA"); kpg.initialize (1024); kp = kpg.GenerateKeyPair (); system.out .println ("Generate RSA Key OK =" Kp.toString ()); // Write Serialization Key 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; 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 addresses` * /

// Public key encryption byte [] endata = rsAencrypt (publickey, src_byte); // system.out.println ("endata =" md5_show (endata)); fos = new java.io.fileoutputstream ("public_endata"); Fos.write (endata); // private key decryption Byte [] deData = rsadecrypt (privatekey, endata); system.out.println ("deData =" new string (dedata));

// private key encryption endata = rsaencrypt (privatekey, src_byte); // system.out.println ("endata =" md5_show (endata)); fos = new java.io.fileoutputStream ("private_endata"); fos.write (Endata); // public key decryption DEDATA = rsadecrypt (publickey, endata); System.out.Println ("Dedata =" New String (Dedata));

/ ************************************************** ******************************* MD5WithRSA signature * // * // private key signature BYTE [] Sign_BYTE = SIGN (PrivateKey) , SRC_BYTE); System.out.Println ("Sign =" New String (Sign_Byte)); // Public Key Verification Signature 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);

}

/ ************************************************** ****************************** Generate 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);} / **************************************************** ****************************************************** Generate 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 public key encryption * / 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 private key decryption * / 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 private key encryption * / 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 public key decryption * / 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);

/ ************************************************** ******************************* Write the series of objects * / 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 ();}

/ ************************************************** ********************************** THROWS Excert {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 summary / handle * / static string md5_show (byte [] md5_byte) throws exception {java .math.biginteger big = new java.math.biginteger (md5_byte); return big.tostring (16);}

/ ************************************************** ******************************* Generate DES Key * / 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);} / *************************************************** **************************************************** DEES Decryption * / 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 encryption * / 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);

/ ************************************************** ******************************* Digital signature * / 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 ();}

转载请注明原文地址:https://www.9cbs.com/read-117478.html

New Post(0)