[J2SE] 3DES encryption and decryption in Java Call Sample _jsp programming

Source: Internet
Author: User
Jce.jar
Security/us_export_policy.jar
Security/local_policy.jar
Ext/sunjce_provider.jar
These packages are loaded automatically by the Java runtime, so 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 is a sample code for the 3DES encryption decryption algorithm that is invoked in Java from the Sun company:
Copy Code code 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"; Define cryptographic algorithms, available des,desede,blowfish

Keybyte is an 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 key
Secretkey Deskey = new Secretkeyspec (keybyte, algorithm);
Encryption
Cipher C1 = cipher.getinstance (algorithm);
C1.init (Cipher.encrypt_mode, Deskey);
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 an encryption key with a length of 24 bytes
SRC is an encrypted buffer
public static byte[] Decryptmode (byte[] keybyte, byte[] src) {
try {
Generate key
Secretkey Deskey = new Secretkeyspec (keybyte, algorithm);
Decrypt
Cipher C1 = cipher.getinstance (algorithm);
C1.init (Cipher.decrypt_mode, Deskey);
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 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 you want to add it in JCE
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-encrypted 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));
}
}

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.