Package COM. joye3g. ed; public class main {public static void main (string [] ARGs) throws exception {// initthreedes (); // initaes (); // initrsa (); // initmd5 (); // initsha (); inithmac ();} public static void initthreedes () {threedes = new threedes (); string MSG = "security programming technology _ encryption and decryption"; system. out. println ("plaintext is" + MSG); byte [] ENC = des. createencryptor (MSG); system. out. println ("ciphertext is" + new string (ENC); byte [] dec = des. createdecryptor (ENC); system. out. println ("the decrypted result is" + new string (DEC);} public static void initaes () {AES = new AES (); string MSG = "security programming technology _ encryption and decryption"; system. out. println ("plaintext is" + MSG); byte [] ENC = AES. createencryptor (MSG); system. out. println ("ciphertext is" + new string (ENC); byte [] dec = AES. createdecryptor (ENC); system. out. println ("the decrypted result is" + new string (DEC);} public static void initrsa () {RSA = New RSA (); string MSG = "security programming technology _ encryption and decryption"; system. out. println ("plaintext:" + MSG); // use the public key to encrypt byte [] srcbytes = MSG. getbytes (); byte [] resultbytes = RSA. encrypt (srcbytes); string result = new string (resultbytes); system. out. println ("the ciphertext after encryption with the public key is:" + result); // use the private key to decrypt byte [] decbytes = RSA. decrypt (resultbytes); string dec = new string (decbytes); system. out. println ("the result after decryption with the private key is:" + dec);} public static void initmd5 () {string MSG = "security programming technology _ encryption and decryption"; system. out. println ("plaintext" + MSG); MD5 MD5 = new MD5 (); byte [] resultbytes = md5.encrypt (MSG); string result = new string (resultbytes); system. out. println ("ciphertext is" + result);} public static void initsha () {string MSG = "security programming technology _ encryption and decryption"; system. out. println ("plaintext is" + MSG); Sha = new Sha (); byte [] resultbytes = Sha. encrypt (MSG); string result = new string (resultbytes); system. out. println ("ciphertext is" + result);} public static void inithmac () {// string STR = "security programming technology _ encryption and decryption"; system. out. println ("plaintext:" + Str); HMAC = new HMAC (); byte [] certifycode = HMAC. createencryptor (STR); system. out. println ("ciphertext:" + new string (certifycode ));}}
AES:
Package COM. joye3g. ed; import Java. security. nosuchalgorithmexception; import Java. security. security; import javax. crypto. cipher; import javax. crypto. keygenerator; import javax. crypto. nosuchpaddingexception; import javax. crypto. secretkey; public class AES {// keygenerator provides the symmetric key generator function and supports various algorithms: Private keygenerator; // secretkey stores the symmetric key private secretkey; // cipher is responsible for encryption or decryption. Private cipher; // This byte array is responsible for saving the encryption result private byte [] cipherbyte; @ suppresswarnings ("restriction") Public AES () {security. addprovider (new COM. sun. crypto. provider. sunjce (); // instantiate a key generator that supports the AES algorithm. The algorithm name is aestry {keygenerator = keygenerator. getinstance ("AES"); // generate secretkey = keygenerator. generatekey (); // generate the cipher object and specify that it supports the AES algorithm cipher = cipher. getinstance ("AES");} catch (nosuchalgorithmexception e) {e. printstacktrace ();} catch (nosuchpaddingexception e) {e. printstacktrace () ;}/ * encrypt string Str */Public byte [] createencryptor (string Str) {try {// initialize the cipher object based on the key, encrypt_mode indicates the encryption mode cipher. init (cipher. encrypt_mode, secretkey); byte [] src = Str. getbytes (); // Save the encryption result to cipherbytecipherbyte = cipher. dofinal (SRC);} catch (Java. security. invalidkeyexception ex) {ex. printstacktrace ();} catch (javax. crypto. badpaddingexception ex) {ex. printstacktrace ();} catch (javax. crypto. illegalblocksizeexception ex) {ex. printstacktrace ();} return cipherbyte;}/* decrypt the byte array buff */Public byte [] createdecryptor (byte [] buff) {try {// initialize the cipher object based on the key. encrypt_mode indicates the decryption mode cipher. init (cipher. decrypt_mode, secretkey); // Save the obtained plaintext to the herbyte character array cipherbyte = cipher. dofinal (buff);} catch (Java. security. invalidkeyexception ex) {ex. printstacktrace ();} catch (javax. crypto. badpaddingexception ex) {ex. printstacktrace ();} catch (javax. crypto. illegalblocksizeexception ex) {ex. printstacktrace ();} return cipherbyte ;}}
Des:
Package COM. joye3g. ed; import Java. security. nosuchalgorithmexception; import Java. security. security; import javax. crypto. cipher; import javax. crypto. keygenerator; import javax. crypto. nosuchpaddingexception; import javax. crypto. secretkey; public class des {// keygenerator provides the symmetric key generator function and supports various algorithms: Private keygenerator; // secretkey stores the symmetric key private secretkey; // cipher is responsible for encryption or decryption. Private cipher; // This byte array is responsible for saving the encryption result. Private byte [] cipherbyte; @ suppresswarnings ("restriction") Public des () {security. addprovider (new COM. sun. crypto. provider. sunjce (); try {// instantiate the key generator that supports the DES algorithm (the algorithm name must be specified; otherwise, an exception is thrown) keygenerator = keygenerator. getinstance ("des"); // generate secretkey = keygenerator. generatekey (); // generate the cipher object and specify that it supports the DES algorithm cipher = cipher. getinstance ("des");} catch (nosuchalgorithmexception ex) {ex. printstacktrace ();} catch (nosuchpaddingexception ex) {ex. printstacktrace () ;}/ * encrypt string Str */Public byte [] createencryptor (string Str) {try {// initialize the cipher object based on the key, encrypt_mode indicates the encryption mode cipher. init (cipher. encrypt_mode, secretkey); byte [] src = Str. getbytes (); // Save the encryption result to cipherbytecipherbyte = cipher. dofinal (SRC);} catch (Java. security. invalidkeyexception ex) {ex. printstacktrace ();} catch (javax. crypto. badpaddingexception ex) {ex. printstacktrace ();} catch (javax. crypto. illegalblocksizeexception ex) {ex. printstacktrace ();} return cipherbyte;}/* decrypt the byte array buff */Public byte [] createdecryptor (byte [] buff) {try {// initialize the cipher object based on the key. encrypt_mode indicates the decryption mode cipher. init (cipher. decrypt_mode, secretkey); // Save the obtained plaintext to the herbyte character array cipherbyte = cipher. dofinal (buff);} catch (Java. security. invalidkeyexception ex) {ex. printstacktrace ();} catch (javax. crypto. badpaddingexception ex) {ex. printstacktrace ();} catch (javax. crypto. illegalblocksizeexception ex) {ex. printstacktrace ();} return cipherbyte ;}}
HMAC:
Package COM. joye3g. ed; import Java. io. unsupportedencodingexception; import Java. security. invalidkeyexception; import Java. security. nosuchalgorithmexception; import javax. crypto. keygenerator; import javax. crypto. mac; import javax. crypto. secretkey; import javax. crypto. spec. secretkeyspec; public class HMAC {private Mac; Public HMAC () {try {// key keygenerator keygen = keygenerator used to calculate the verification code using the DES algorithm. getinstance ("desede"); secretkey key = keygen. generatekey (); byte [] keybyte = key. getencoded (); // generate the Mac object secretkeyspec SKS = new secretkeyspec (keybyte, "hmacmd5"); MAC = Mac. getinstance ("hmacmd5"); Mac. init (SKS);} catch (invalidkeyexception e) {e. printstacktrace ();} catch (nosuchalgorithmexception e) {e. printstacktrace () ;}} public byte [] createencryptor (string Str) {// input the string byte [] certifycode = NULL; try {Mac. update (Str. getbytes ("utf8"); // calculate the verification code certifycode = Mac. dofinal (); Return certifycode;} catch (illegalstateexception e) {e. printstacktrace ();} catch (unsupportedencodingexception e) {e. printstacktrace ();} return NULL ;}}
MD5:
Package COM. joye3g. ed; import Java. security. messagedigest; import Java. security. nosuchalgorithmexception; public class MD5 {private messagedigest MD5; Public MD5 () {// generate the messagedigest object based on the MD5 Algorithm try {MD5 = messagedigest. getinstance ("MD5");} catch (nosuchalgorithmexception e) {e. printstacktrace () ;}// MD5 encryption public byte [] encrypt (string MSG) {byte [] srcbytes = MSG. getbytes (); // use srcbytes to update the abstract md5.update (srcbytes); // complete hash calculation, and return the ciphertext return md5.digest ();}}
RSA:
Package COM. joye3g. ed; import Java. security. keypair; import Java. security. keypairgenerator; import Java. security. nosuchalgorithmexception; import Java. security. interfaces. rsw.vatekey; import Java. security. interfaces. rsapublickey; import javax. crypto. cipher; import javax. crypto. nosuchpaddingexception; public class RSA {private rsw.vatekey privatekey; private rsapublickey publickey; private cipher; P Ublic RSA () {try {// keypairgenerator class is used to generate public and private key pairs. The object keypairgenerator keypairgen = keypairgenerator is generated based on the RSA algorithm. getinstance ("RSA"); // initialize the key pair generator. The key size is 1024-bit keypairgen. initialize (1024); // generate a key pair and save it in keypair = keypairgen. generatekeypair (); // obtain the private key privatekey = (rs?vatekey) keypair. getprivate (); // obtain the Public Key publickey = (rsapublickey) keypair. getpublic (); // initialize the cipher object based on the private key. getinst Ance ("RSA");} catch (nosuchalgorithmexception e) {e. printstacktrace ();} catch (nosuchpaddingexception e) {e. printstacktrace () ;}/ * encrypt srcbytes */Public byte [] encrypt (byte [] srcbytes) {If (publickey! = NULL) {try {// initialize the cipher object based on the public key. init (cipher. encrypt_mode, publickey); // Save the encryption result to resultbytesbyte [] resultbytes = cipher. dofinal (srcbytes); Return resultbytes;} catch (exception e) {e. printstacktrace () ;}} return NULL;}/* decrypt encbytes */Public byte [] decrypt (byte [] encbytes) {If (privatekey! = NULL) {try {cipher. init (cipher. decrypt_mode, privatekey); // Save the decryption result to resultbytesbyte [] decbytes = cipher. dofinal (encbytes); Return decbytes;} catch (exception e) {e. printstacktrace () ;}} return NULL ;}}
SHA:
Package COM. joye3g. ed; import Java. security. messagedigest; import Java. security. nosuchalgorithmexception; public class Sha {private messagedigest MD5; Public Sha () {// generate the messagedigest object based on the MD5 Algorithm try {MD5 = messagedigest. getinstance ("MD5");} catch (nosuchalgorithmexception e) {e. printstacktrace () ;}// MD5 encryption public byte [] encrypt (string MSG) {byte [] srcbytes = MSG. getbytes (); // use srcbytes to update the abstract md5.update (srcbytes); // complete hash calculation, and return the ciphertext return md5.digest ();}}
3DES:
Package COM. joye3g. ed; import Java. security. nosuchalgorithmexception; import Java. security. security; import javax. crypto. cipher; import javax. crypto. keygenerator; import javax. crypto. nosuchpaddingexception; import javax. crypto. secretkey; public class threedes {// keygenerator provides the symmetric key generator function and supports various algorithms: Private keygenerator; // secretkey stores the symmetric key private secretkey; // cipher is responsible for encryption or decryption. Private cipher; // This byte array is responsible for saving the encryption result. Private byte [] cipherbyte; @ suppresswarnings ("restriction") Public threedes () {security. addprovider (new COM. sun. crypto. provider. sunjce (); // instantiate a key generator that supports the 3DES algorithm. The algorithm name is desedetry {keygenerator = keygenerator. getinstance ("desede"); // generate secret key secret = keygenerator. generatekey (); // generate the cipher object and specify that it supports the 3DES algorithm cipher = cipher. getinstance ("desede");} catch (nosuchalgorithmexception e) {e. printstacktrace ();} catch (nosuchpaddingexception e) {e. printstacktrace () ;}/ * encrypt string Str */Public byte [] createencryptor (string Str) {try {// initialize the cipher object based on the key, encrypt_mode indicates the encryption mode cipher. init (cipher. encrypt_mode, secretkey); byte [] src = Str. getbytes (); // Save the encryption result to cipherbytecipherbyte = cipher. dofinal (SRC);} catch (Java. security. invalidkeyexception ex) {ex. printstacktrace ();} catch (javax. crypto. badpaddingexception ex) {ex. printstacktrace ();} catch (javax. crypto. illegalblocksizeexception ex) {ex. printstacktrace ();} return cipherbyte;}/* decrypt the byte array buff */Public byte [] createdecryptor (byte [] buff) {try {// initialize the cipher object based on the key. encrypt_mode indicates the decryption mode cipher. init (cipher. decrypt_mode, secretkey); // Save the obtained plaintext to the herbyte character array cipherbyte = cipher. dofinal (buff);} catch (Java. security. invalidkeyexception ex) {ex. printstacktrace ();} catch (javax. crypto. badpaddingexception ex) {ex. printstacktrace ();} catch (javax. crypto. illegalblocksizeexception ex) {ex. printstacktrace ();} return cipherbyte ;}}