/*************************************** * ********************* 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