3DES encryption in Java

xiaoxiao2021-03-19  203

When you call Sun Company's 3DES encryption algorithm provided in Java, you need to use the following four JAR packages in $ java_home / jre / lib / directory: jce.jarsecurity / us_export_policy.jarsecurity / local_policy.jarext / sunjce_provider.jar

The Java runs automatically loads these packages, so there is no need to set to the ClassPath environment variable for applications with main functions. For web applications, you don't need to add these packages to the web-inf / lib directory.

The following is a sample code for the 3DES encryption decision algorithm provided by SUN in Java:

/ *

String DeSede (3DES) encryption

* /

Import java.security. *;

Import javax.crypto. *;

Import javax.crypto.spec.secretKeyspec;

Public class threede {

Private static final string algorithm = "desede"; // Define encryption algorithm, available with DES, DESEDE, BLOWFISH

// Keybyte is an encryption key, with a length of 24 bytes

// src is an encrypted data buffer (source)

Public static Byte [] encryptmode (byte [] keybyte, byte [] src) {

Try {

/ / Generate a 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 a encrypted buffer

Public static Byte [] DecryptMode (byte [] keybyte, byte [] src) {

Try {

/ / Generate a key

SecretKey Deskey = New SecretKeyspec (Keybyte, Algorithm);

// Decryption

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 hex string

Public static string byte2hex (byte [] b) {

String HS = "";

String stmp = ""; for (int N = 0; n

STMP = (java.lang.integer.tohexstring (b [n] & 0xff);

IF (stmp.length () == 1) HS = HS "0" STMP;

ELSE HS = HS STMP;

IF (n

}

Return hs.touppercase ();

}

Public static void main (string [] args)

{

// Add new security algorithm, if you use JCE to add it in

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 ("String before encryption:" szsrc);

Byte [] encoded = encryptmode (keybytes, szsrc.getbytes ());

System.out.println ("Encrypted string:" new string (encode));

Byte [] srcbytes = DecryPTMode (keybytes, eNCoded);

System.out.println ("Decoupted string:" (new string (srcbytes)))

}

}

转载请注明原文地址:https://www.9cbs.com/read-130096.html

New Post(0)