Java encryption algorithm Summary

Source: Internet
Author: User
Tags modulus

/*************************************** * ********************* Function: // DES_EN (byte [] src, byte [] key) Description: // DES encryption algorithm Input: // byte [] src encryption source, byte [] key encryption key Output: // encrypted src Return: // byte [] ********************************* * ***********************/public static byte [] DES_EN (byte [] src, byte [] key) {try {SecretKey secret ey = new SecretKeySpec (key, "DES"); // generate key 21 Cipher c1 = Cipher. getInstance ("DES/CBC/NoPadding"); // instantiate the Cipher tool Class 22 c1.init (Cipher. ENCRYPT_MODE, encryption ey, iv); // initialize to encryption mode 23 return c1.doFinal (src);} catch (java. security. noSuchAlgorithmException e1) {e1.printStackTrace ();} catch (javax. crypto. noSuchPaddingException e2) {e2.printStackTrace ();} catch (java. lang. exception e3) {e3.printStackTrace ();} return null ;}

/*************************************** * ********************* Function: // DES_DE (byte [] src, byte [] key) Description: // DES decryption algorithm Input: // byte [] src decryption source, byte [] key decryption key Output: // decrypted src Return: // byte [] ********************************* * ***********************/public static byte [] DES_DE (byte [] src, byte [] key) {try {SecretKey secret ey = new SecretKeySpec (key, "DES"); Cipher c1 = Cipher. getInstance ("DES/CBC/NoPadding"); c1.init (Cipher. DECRYPT_MODE, cipher ey, iv); // initialize to the decryption mode 44 return c1.doFinal (src);} catch (java. security. noSuchAlgorithmException e1) {e1.printStackTrace ();} catch (javax. crypto. noSuchPaddingException e2) {e2.printStackTrace ();} catch (java. lang. exception e3) {e3.printStackTrace ();} return null ;}
/*************************************** * ********************* Function: // encryptMode (byte [] src, byte [] key) Description: // 3DES_CBC_EN Input: // src-source data (byte []) key-Encrypted key (byte []) Output: // encrypted data Return: // byte [] ********************************* * ************************/public static byte [] encryptMode (byte [] src, byte [] key) {try {System. out. println ("not 8 bytes:" + Util. byteArrayToHexString (src); SecretKey secret ey = new SecretKeySpec (key, Algorithm); // generate key 21 Cipher c1 = Cipher. getInstance ("DESede/CBC/NoPadding"); // instantiate the Cipher tool Class 22 c1.init (Cipher. ENCRYPT_MODE, encryption ey, iv); // initialize to encryption mode 23 return c1.doFinal (src);} catch (java. security. noSuchAlgorithmException e1) {e1.printStackTrace ();} catch (javax. crypto. noSuchPaddingException e2) {e2.printStackTrace ();} catch (java. lang. exception e3) {e3.printStackTrace ();} return null ;}
/*************************************** * Function: // decryptMode (byte [] src, byte [] key) Description: // * 3DES_CBC_DE Input: // src-source data (byte []) key-decryption key (byte []) Output: // * Return: // byte [] ********************************* * *********************************/public static byte [] decryptMode (byte [] src, byte [] key) {try {SecretKey secret ey = new SecretKeySpec (key, Algorithm); Cipher c1 = Cipher. getInstance ("DESede/CBC/NoPadding"); c1.init (Cipher. DECRYPT_MODE, cipher ey, iv); // initialize to the decryption mode 44 return c1.doFinal (src);} catch (java. security. noSuchAlgorithmException e1) {e1.printStackTrace ();} catch (javax. crypto. noSuchPaddingException e2) {e2.printStackTrace ();} catch (java. lang. exception e3) {e3.printStackTrace ();} return null ;}
// ===================================== RSA public key encryption ============== =================================== public static String rsa_pk_encrypt (byte [] data) {byte [] result = null; PublicKey publicKey = null; // obtain it based on the generated key and enter BigInteger modulus = new BigInteger ("107387874390505784524513973877097626636040930323785063417203429329049380012116062713367963534546558364338266011147415609622592361642249862575535940504286965419678075850829850981611449282370578971183932155637562314983750327067652855947799699126317486874317221419492450317503254553770513863888939550973346592793 "); bigInteger publicExponent = new BigInteger ("65537"); RSAPublicKeySpec rsaPublicKeySpec = new RSAPublicKeySpec (modulus, publicExponent); KeyFactory keyFactory = null; try {keyFactory = KeyFactory. getInstance ("RSA");} catch (NoSuchAlgorithmException e1) {e1.printStackTrace ();} try {publicKey = keyFactory. generatePublic (rsaPublicKeySpec);} catch (InvalidKeySpecException e) {e. printStackTrace ();} try {Cipher c1 = Cipher. getInstance ("RSA/ECB/PKCS1Padding"); c1.init (Cipher. ENCRYPT_MODE, publicKey); result = c1.doFinal (data);} catch (NoSuchAlgorithmException e) {e. printStackTrace ();} catch (NoSuchPaddingException e) {e. printStackTrace ();} catch (InvalidKeyException e) {e. printStackTrace ();} catch (IllegalBlockSizeException e) {e. printStackTrace ();} catch (BadPaddingException e) {e. printStackTrace ();} return dumpByte (result, result. length );}



This article from the "Java practical development accumulation" blog, please be sure to keep this source http://yufenghang.blog.51cto.com/6014821/1287102

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.