/**
* @ (#) Eryptogram. Java 1.00 04/03/11
*
* Copyright (c) 2003-2004 abacus, Ltd.
*
* Encryption and decryption
*
*
*
*/
Package cn.com. sinosoft. SMP. util;
Import javax. crypto. cipher;
Import javax. crypto. keygenerator;
Import javax. crypto. secretkey;
Import javax. servlet. http. httpservletrequest;
Import com. uncnet. Framework. utils. permission;
/**
* Encryption and decryption
*/
Public class eryptogram {
Private Static string algorithm = "des ";
// Defines the encryption algorithm. Des, desede, and blowfish are available.
Static Boolean DEBUG = false;
/**
* Construct a subannotation.
*/
Public eryptogram (){
}
/**
* Generate a key
*
* @ Return byte [] returns the generated key
* @ Throws exception
* Throw an exception.
*/
Public static byte [] getsecretkey () throws exception {
Keygenerator keygen = keygenerator. getinstance (algorithm );
Secretkey secret ey = keygen. generatekey ();
If (Debug)
System. Out. println ("generate key:" + byte2hex (Cipher ey. getencoded ()));
Return encryption ey. getencoded ();
}
/**
* Encrypt the specified data based on the provided key.
*
* @ Param Input
* Data to be encrypted
* @ Param key
* Key
* @ Return byte [] encrypted data
* @ Throws exception
*/
Public static byte [] encryptdata (byte [] input, byte [] Key) throws exception {
Secretkey secret ey = new javax. crypto. spec. secretkeyspec (Key, algorithm );
If (Debug ){
System. Out. println ("binary string before encryption:" + byte2hex (input ));
System. Out. println ("pre-encryption string:" + new string (input ));
}
Cipher C1 = cipher. getinstance (algorithm );
C1.init (Cipher. encrypt_mode, cipher ey );
Byte [] cipherbyte = c1.dofinal (input );
If (Debug)
System. Out. println ("encrypted binary string:" + byte2hex (cipherbyte ));
Return cipherbyte;
}
/**
* Decrypt the specified encrypted data with the specified key.
*
* @ Param Input
* Data to be decrypted
* @ Param key
* Key
* @ Return byte [] decrypted data
* @ Throws exception
*/
Public static byte [] decryptdata (byte [] input, byte [] Key) throws exception {
Secretkey secret ey = new javax. crypto. spec. secretkeyspec (Key, algorithm );
If (Debug)
System. Out. println ("pre-decryption information:" + byte2hex (input ));
Cipher C1 = cipher. getinstance (algorithm );
C1.init (Cipher. decrypt_mode, cipher ey );
Byte [] clearbyte = c1.dofinal (input );
If (Debug ){
System. Out. println ("decrypted binary string:" + byte2hex (clearbyte ));
System. Out. println ("decrypted string:" + (new string (clearbyte )));
}
Return clearbyte;
}
/**
* Convert bytecode to a hexadecimal string
*
* @ Param byte []
* B: Enter the bytecode to be converted.
* @ Return string returns the converted 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 String encryptdata1 (httpservletrequest request ){
Permission userperm =
(Permission) request. getsession (). getattribute ("permmgr ");
Eryptogram ETG = new eryptogram ();
String data1 = userperm. GetUserName () + "," + userperm. getuserid ();
Byte [] key;
Byte [] en = NULL ;;
Try {
Key = ETG. getsecretkey ();
Byte [] DATA = data1.getbytes ();
En = ETG. encryptdata (data, key );
} Catch (exception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Return new string (en );
}
Public byte [] encryptdata2 (httpservletrequest request ){
Permission userperm =
(Permission) request. getsession (). getattribute ("permmgr ");
Eryptogram ETG = new eryptogram ();
String data1 = userperm. GetUserName () + "," + userperm. getuserid ();
Byte [] key;
Byte [] en = NULL ;;
Try {
Key = ETG. getsecretkey ();
Byte [] DATA = data1.getbytes ();
En = ETG. encryptdata (data, key );
// Return byte2hex (en );
} Catch (exception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Return en;
}
Public static void main (string [] ARGs ){
Try {
DEBUG = false;
Eryptogram ETG = new eryptogram ();
Byte [] Key = ETG. getsecretkey ();
System. Out. println ("Key =" + key );
String AA = "Ck = 1234567 ";
Byte [] DATA = AA. getbytes ();
System. Out. println (data );
Byte [] en = ETG. encryptdata (data, key );
System. Out. println ("encryptdata =" + new string (En ));
Byte [] De = ETG. decryptdata (EN, key );
System. Out. println ("decryptdata =" + new string (de ));
} Catch (exception e ){
E. printstacktrace ();
}
}
}