Jce. jar
Security/US_export_policy.jar
Security/local_policy.jar
Ext/sunjce_provider.jar
These packages are automatically loaded during Java runtime. Therefore, applications with the main function do not need to be set to the CLASSPATH environment variable. For WEB applications, you do not need to add these packages to the WEB-INF/lib directory.
The following sample code calls the 3DES encryption and decryption algorithm provided by sun IN java:
Copy codeThe Code is as follows:
/* String DESede (3DES) encryption */
Import java. security .*;
Import javax. crypto .*;
Import javax. crypto. spec. SecretKeySpec;
Public class ThreeDes {
Private static final String Algorithm = "DESede"; // defines the encryption Algorithm. DES, DESede, and Blowfish can be used.
// Keybyte is the encryption key, with a length of 24 bytes
// Src is the encrypted data buffer (source)
Public static byte [] encryptMode (byte [] keybyte, byte [] src ){
Try {
// Generate the key
SecretKey secret ey = new SecretKeySpec (keybyte, Algorithm );
// Encryption
Cipher c1 = Cipher. getInstance (Algorithm );
C1.init (Cipher. ENCRYPT_MODE, Cipher ey );
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;
}
// Keybyte is the encryption key, with a length of 24 bytes
// Src is the encrypted Buffer
Public static byte [] decryptMode (byte [] keybyte, byte [] src ){
Try {
// Generate the key
SecretKey secret ey = new SecretKeySpec (keybyte, Algorithm );
// Decrypt
Cipher c1 = Cipher. getInstance (Algorithm );
C1.init (Cipher. DECRYPT_MODE, Cipher ey );
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;
}
// Convert to a hexadecimal string
Public static String byte2hex (byte [] B ){
String hs = "";
String stmp = "";
For (int n = 0; n <B. length; n ++ ){
Stmp = (java. lang. Integer. toHexString (B [n] & 0XFF ));
If (stmp. length () = 1) hs = hs + "0" + stmp;
Else hs = hs + stmp;
If (n <B. length-1) hs = hs + ":";
}
Return hs. toUpperCase ();
}
Public static void main (String [] args ){
// Add a new security algorithm. If JCE is used, add it.
Security. addProvider (new com. sun. crypto. provider. SunJCE ());
Final byte [] keyBytes = {0x11, 0x22, 0x4F, 0x58,
(Byte) 0x88, 0x10, 0x40, 0x38, 0x28, 0x25, 0x79, 0x51,
(Byte) 0xCB, (byte) 0xDD, 0x55, 0x66, 0x77, 0x29, 0x74,
(Byte) 0x98, 0x30, 0x40, 0x36, (byte) 0xE2
}; // 24-byte key
String szSrc = "This is a 3DES test. test ";
System. out. println ("pre-encryption string:" + szSrc );
Byte [] encoded = encryptMode (keyBytes, szSrc. getBytes ());
System. out. println ("encrypted String:" + new String (encoded ));
Byte [] srcBytes = decryptMode (keyBytes, encoded );
System. out. println ("decrypted String:" + (new String (srcBytes )));
}
}