WDSSOFT free source code, Java encryption decryption programming common toolkit JCT 1.0 (finishing)

xiaoxiao2021-03-06  50

Package JCT;

Import java.security. *;

/ ***

Calculated string and byte [] Digital summary * @copyright: wdssoft * @ad: wdssoft "Enterprise Multi-level Digital Signature System" - Best Enterprise Electronic Document Multi-Grade Digital Signature Program * / public class digest {

/ *** Calculate the SHA number summary of the string, return ** / public static Byte [] mdigestsha (string source) {byte [] nullreturn = {0}; try {messageDigest thismd = MessageDigest.getInstance "SHA"); byte [] Digest = thismd.digest ("UTF8")); return digest;} catch (Exception E) {return null;}} / *** calculate the SHA number of BYTE [] Summary, return ** / public static byte [] mdigestsha in the form of Byte [],} {byte [] nullreturn = {0}; try {messageDigest thismd = messageDigest.getInstance ("sha"); byte [] Digest = thismd.digest (es);}}} / *** Calculate the MD5 digital summary of the string, return ** / public static byte in byte [] [] MdigestMD5 (String source) {byte [] nullreturn = {0}; try {MessageDigest thisMD = MessageDigest.getInstance ( "MD5"); byte [] digest = thisMD.digest (source.getBytes ( "UTF8")); return digest } Catch (Exception E) {Return NULL;}} / *** Calculate Byte [] MD5 Word Summary, return ** / public static byte in byte [] [] mdigestmd5 (byte [] Source) {byte [] nullreturn = {0}; try {messagedigest thismd = messageDigest.getInstance ("MD5"); byte [] Digest = thismd.digest (Source); return Digest;} catch (Exc) EPTION E) {return null;}} public static void main (string test = "wdssoft";

Byte [] T1 = mdigestsha (TEST); for (int i = 0; i

}

Package JCT;

import javax.crypto *;. import javax.crypto.spec *;. import java.security.Key; import java.security.KeyFactory; import java.security.spec.EncodedKeySpec; import java.security.Security; import java.util .Random; import java.util.vector; / ***

Package the method related to PBE (password-based encryption) algorithm, available using PBE algorithm encryption and decrypt * @copyright: wdssoft * /

Public class pbetool {

Public pbetool () {} / *** Test * / public static void main (string "args) {string tt = new string (" java "); char [] password = tt.tochararray (); byte [] PP Pbetool PBE = New Pbetool (); TRY {

PP = PBE.PBeunwrap (PBE.Pbewrap (Tt.getbytes ("UTF8"), Password), Password); System.out.Print (New String (PP));

} Catch (Exception E) {}} / *** Using PBE encryption Byte [], then Salting salt, and unwrap method to use * @Param Byte [] Target: PBE encryption object * @Param char [] Password: PBE encryption Password * @Return Object [] Object [0] 1: Byte []: Cipheet; Object [1]: Byte []: salt * / public object [] pbewrap (byte [] target, char [] password) {Try {

PBEKeySpec keySpec = new PBEKeySpec (password); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance ( "PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret (keySpec); Cipher cipher = Cipher.getInstance ( "PBEWithMD5AndDES"); byte [] salt = new byte [8]; Random random = new Random (); random.nextBytes (salt); PBEParameterSpec paramSpec = new PBEParameterSpec (salt, 100); cipher.init (cipher.ENCRYPT_MODE, key, paramSpec); Object [] encryptedByte = new Object [2]; EncryptedByte [0] = Cipher.dofinal (Target); EncryptedByte [1] = SALT; RETURN EncryptedByte;

} catCH (Exception E) {E.PrintStackTrace (); return null;}} / *** Using PBE encryption Byte [], fixed salt, and unwrape_2 support usage * @Param Byte [] Target: PBE encryption object * @Param Char [] Password: PBE Encryption Pass * @return Byte []: Encrypted ciphertext * / public Byte [] pbewrap_2 (byte [] target, char [] password) {pbekeySpec keySpec = new PbekeySpec (Password ); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance ( "PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret (keySpec); cipher cipher = Cipher.getInstance ( "PBEWithMD5AndDES"); // fixing salts, may change when the actual trial itself salts, But as unwrape_2, byte [] salt = new byte [8]; Salt [2] = Salt [4] = Salt [7] = 11; Salt [1] = Salt [3] = SALT [5] = 36; salt [0] = 10; PBEParameterSpec paramSpec = new PBEParameterSpec (salt, 100); cipher.init (cipher.ENCRYPT_MODE, key, paramSpec); return cipher.doFinal (target);} catch (Exception e) {e.printStackTrace ( Return null;}}}

/ *** PBE decryption, and wrap support usage * @Param object [] encrypted: object [0] byte []: cixed Object [1]: Byte []: salt * @Param char [] password: password * @ Return Byte [] PBEUNWRAP (Object [] encrypted, char [] password) {Try {

byte [] wraped = (byte []) encrypted [0]; byte [] salt = (byte []) encrypted [1]; PBEKeySpec keySpec = new PBEKeySpec (password); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance ( "PBEWithMD5AndDES") ; SecretKey key = keyFactory.generateSecret (keySpec); cipher cipher = Cipher.getInstance ( "PBEWithMD5AndDES"); PBEParameterSpec paramSpec = new PBEParameterSpec (salt, 100); cipher.init (cipher.DECRYPT_MODE, key, paramSpec); return cipher. DOFINAL (WRAPED);

} catch (exception e) {

Return NULL;}}

/ *** PBE decryption, and wrap_2 support usage * @Param Byte [] encrypted: ciphertext * @Param Char [] password: password * @return Byte [] 明文 * /

Public Byte [] pbeunwrap_2 (byte [] encrypted, char [] password) {try {byte [] wrad = encrypted

/ / Fixed salt, can change the salt at the time of actual trial, but the unwrap_2 consistent BYTE [] Salt = new byte [8]; Salt [2] = Salt [4] = Salt [7] = 11; SALT [1 ] = salt [3] = salt [5] = 36; salt [0] = 10; PBEKeySpec keySpec = new PBEKeySpec (password); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance ( "PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret (keySpec ); cipher cipher = Cipher.getInstance ( "PBEWithMD5AndDES"); PBEParameterSpec paramSpec = new PBEParameterSpec (salt, 100); cipher.init (cipher.DECRYPT_MODE, key, paramSpec); return cipher.doFinal (wraped);

} catch (exception e) {

Return NULL;}}}

Package JCT;

import java.security.KeyFactory; import java.security.PrivateKey; import java.security.PublicKey; import java.security.Signature; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.SecureRandom; import Java.security.spec.pkcs8encodedKeyspec; import java.security.spec.x509encodedKeyspec; import java.security.security; import javax.crypto.cipher;

/ ***

Package the method related to the RSA asymmetric encryption algorithm, can be used in digital signatures, RSA encryption decryption * @copyright: wdssoft * /

Public class rsatool {

Public {}

/ *** Use a private key encrypted data that has been packaged into Byte [], ie the digital signature * @Param KeyinByte packagebred byte [] private key * @Param Source To sign the data, it should generally be a digital summary * @ return signature byte [] * / public static byte [] sign (byte [] keyInByte, byte [] source) {try {PKCS8EncodedKeySpec priv_spec = new PKCS8EncodedKeySpec (keyInByte); KeyFactory mykeyFactory = KeyFactory.getInstance ( "RSA"); PrivateKey privKey = mykeyFactory.generatePrivate (priv_spec); Signature sig = Signature.getInstance ( "SHA1withRSA"); sig.initSign (privKey); sig.update (source); return sig.sign ();} catch (Exception e) {return null }} / *** Verify the digital signature * @Param KeyinByte Pack Byte [] Form public key * @Param Source Original Digital Summary * @Param Sign Signature (signature of the digital summary for the original text) * @return confirmed boolean * / public static boolean verify (byte [] keyInByte, byte [] source, byte [] sign) {try {KeyFactory mykeyFactory = KeyFactory.getInstance ( "RSA"); Signature sig = Signature.getInstance ( "SHA1withRSA"); X509EncodedKeySpec pub_spec = new X509EncodedKeySpec (keyInByte); PublicKey pubKey = mykeyFactory.generatePublic (pub_spec); sig.initVerify (pubKey); sig.update (source); return sig.verify (sign);} catch (excep TION E) {RETURN FALSE;}} / *** Establish a new key pair, return the packaged BYTE [] form private key and public key * @return contains the private key and public key to package Byte [] form [], Object [0] is private key BYTE [], Object [1] is worker byte [] * / public static object [] giversakeypairinbyte () {keypair newkeypair = CreatmyKey (); if (newkeypair == null ) return null; Object [] re = new Object [2]; if (newKeyPair = null) {PrivateKey priv = newKeyPair.getPrivate ();! byte [] b_priv = priv.getEncoded (); PublicKey pub = newKeyPair.getPublic ( ); byte [] b_pub = pub.getencoded (); re [0] = b_priv; re [1] = b_pub; returnire;} Return NULL;}

/ *** * @return KeyPair new key pair objects * / public static KeyPair creatmyKey () {KeyPair myPair; long mySeed; mySeed = System.currentTimeMillis (); try {KeyPairGenerator keyGen = KeyPairGenerator.getInstance ( "RSA"); SecureRandom random = SecureRandom.getInstance ( "SHA1PRNG", "SUN"); random.setSeed (mySeed); keyGen.initialize (1024, random); myPair = keyGen.generateKeyPair ();} catch (Exception e1) {return null; } RETURN MYPAIR;} / *** Using RSA Public Key Encrypted Data * @Param Pubkeyinbyte Package Byte [] Formal Public Key * @Param Data To Encrypted Data * @return Encrypted Data * / Public Static Byte [] EncryptbyRsa (Byte [] pubKeyInByte, byte [] data) {try {KeyFactory mykeyFactory = KeyFactory.getInstance ( "RSA"); X509EncodedKeySpec pub_spec = new X509EncodedKeySpec (pubKeyInByte); PublicKey pubKey = mykeyFactory.generatePublic (pub_spec); cipher cipher = Cipher.getInstance ( "RSA / ECB / PKCS1PADDING"); cipher.init (cipher.encrypt_mode, pubkey); return cipher.dofinal (data);} catch (exception e) {return null;} / *** Decryption with RSA private key Decryption * @Param PrivKeyinbyte private key Pack BYTE [] Form * @Param Data To decrypt data * @return decrypt data * / public static Byte [] DecryPTBYR SA (byte [] privKeyInByte, byte [] data) {try {PKCS8EncodedKeySpec priv_spec = new PKCS8EncodedKeySpec (privKeyInByte); KeyFactory mykeyFactory = KeyFactory.getInstance ( "RSA"); PrivateKey privKey = mykeyFactory.generatePrivate (priv_spec); Cipher cipher = Cipher .GetInstance ("RSA / ECB / PKCS1PADDING"); cipher.init (cipher.decrypt_mode, privkey); returnified;} catch (Exception E) {return null;}

} / *** Test * / public static void main (string [] args) {TRY {

Object [] v = giversakeypairinbyte (); byte [] Source = Digest.mdigestsha ("haha"); byte [] Sign = Sign ((byte []) v [0], source); boolean yes = verify ((Byte) []) V [1], Source, Sign); if (yes) system.out.print ("verify / n");} catch (exception e) {E.PrintStackTrace ();}}}

Package JCT;

import javax.crypto *;. import javax.crypto.spec *;. import java.security.Key; import java.security.KeyFactory; import java.security.spec.EncodedKeySpec; import java.security.Security; import java.security Importom; import java.util.vector;

/ ***

encapsulates the method associated with the blowfish symmetric encryption algorithm, including using the creation of a Blowfish password, uses blowfish encryption, decrypts, using PBE (password-based encryption) Access BlowFiWsh Password * @copyright: WDSSOFT * /

Public class blowfishtool {

Public blowfishtool () {} / *** Using Blowfish Encryption * @Param Key Key: Password * @Param Byte [] Text: 明文 * @return Byte []: Ciphertext * / Public Static Byte [] Encryptreturnbyte (Key Key, Byte [] text) {Try {copher copher = copher.getInstance ("BLOWFISH / ECB / PKCS5PADDING"); copher.init (cipher.encrypt_mode, key); returnified;} catch (exception e) { Return null;}} / *** blowfish decryption * @Param key key: password * @Param Byte [] text: cixed * @return Byte []: 明文 * / public static Byte [] DecryptreturnByte (Key Key, BYTE " ] text) {try {cipher cipher = Cipher.getInstance ( "Blowfish / ECB / PKCS5Padding"); cipher.init (Cipher.DECRYPT_MODE, key); return cipher.doFinal (text);} catch (Exception e) {return null }} / *** Using PBE Blyfish Password * @Param Key TargetKey: Blowfish Password * @Param Char [] Password: Password * @return Vector-Element1: Byte []: Cipheet - Element2: Byte []: Salt * /

public static Vector wrapKey (Key targetKey, char [] password) {try {PBEKeySpec keySpec = new PBEKeySpec (password); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance ( "PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret (keySpec); Cipher cipher = Cipher.getinstance ("pbewithmd5anddes"); byte [] salt = new byte [8]; random random = new random (); random.nextBytes (salt); pbeparameterspec paramspec = new PBEParameterspec (Salt, 100); cipher.init cipher.WRAP_MODE, key, paramSpec); Vector encryptedKey = new Vector (); encryptedKey.addElement (cipher.wrap (targetKey)); encryptedKey.addElement (salt); return encryptedKey;} catch (Exception e) {e.printStackTrace ( Return null;}} / *** Remove the PBE encrypted key * @Param Vector EncryptedKey-Element1: Byte []: Ciphertext -Element2: Byte []: Salt * @Param Char [] Password: Password * @ Return Key: blowfish password * / public static key unwrapkey (Vector encryptedkey, char [] password) {Try {

byte [] wrapedKey = (byte []) encryptedKey.elementAt (0); byte [] salt = (byte []) encryptedKey.elementAt (1); PBEKeySpec keySpec = new PBEKeySpec (password); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance ( "PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret (keySpec); cipher cipher = Cipher.getInstance ( "PBEWithMD5AndDES"); PBEParameterSpec paramSpec = new PBEParameterSpec (salt, 100); cipher.init (cipher.UNWRAP_MODE, key, paramSpec) Return Cipher.unwrap (WrapedKey, "Blowfish", Cipher.secret_key);} catch (Exception E) {Return Null;}} / *** Generate a Blowfish Password * @return Key Blowfish Password * / Public Static Key CreatKey ) {Try {keygenerator keygenerator = keygenerator.getinstance ("blowfish"); keygenerator.init (128); key key = keygenerator.generateKey (); return key;} catch (Exception E) {return null;}} / ** * Transfer Blowfish password key object to Byte [] Form * @Param Key Blowfish Password KEY Object * @return Byte [] Formal BLOWFISH Password * / Public Static Byte [] EncodeKey () {Return Key.Getencoded ()

/ *** The byte [] in the form of the Blowfish cipher Key Switch Object * @param byte [] in the form of the Blowfish cipher password * @return Key Blowfish Key object * / public static Key decodeKey (byte [] keyByteCode) {SecretKeySpec myKey = new SecretKeySpec KeyBytecode, "blowfish"); return mykey;}

}

Package JCT;

/ ***

wdssoft free source code, Java encryption decryption programming Common toolkit JCT 1.0


* WDSSoft's Enterprise Multi-level Digital Signature System (MDSS) - Best Corporate Document Digital Signature System < BR> * /

Public class _readme {

}

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

New Post(0)