Tutorials to .com

Tutorials to .com » Jsp » Skills » Written in cipher with the Java classes

Written in cipher with the Java classes

Print View , by: iSee ,Total views: 12 ,Word Count: 475 ,Date: Mon, 24 Aug 2009 Time: 4:06 PM

package data;

import java.security .*;
import javax.crypto .*;
import javax.crypto.spec .*;
import java.io. *;

/ **
* Security provides a security algorithms class, including a symmetric cryptographic algorithm, and hash algorithm
* /
public final class Security
(
/ **
* Symmetric encryption method
* @ Param byteSource need to encrypt the data
* @ Return the encrypted data
* @ Throws Exception
* /
public static byte [] symmetricEncrypto (byte [] byteSource) throws Exception
(
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
try
(
int mode = Cipher.ENCRYPT_MODE;
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance ( "DES");
byte [] keyData = (1, 9, 8, 2, 0, 8, 2, 1);
DESKeySpec keySpec = new DESKeySpec (keyData);
Key key = keyFactory.generateSecret (keySpec);
Cipher cipher = Cipher.getInstance ( "DES");
cipher.init (mode, key);
int blockSize = cipher.getBlockSize ();
int position = 0;
int length = byteSource.length;
boolean more = true;
while (more)
(
if (position + blockSize <= length)
(
baos.write (cipher.update (byteSource, position, blockSize));
position + = blockSize;
)
else
(
more = false;
)
)
if (position <length)
(
baos.write (cipher.doFinal (byteSource, position, length - position));
)
else
(
baos.write (cipher.doFinal ());
)
return baos.toByteArray ();
)
catch (Exception e)
(
throw e;
)
finally
(
baos.close ();
)
)
/ **
* Symmetric Decryption Method
* @ Param byteSource need to decrypt the data
* @ Return the data after decryption
* @ Throws Exception
* /
public static byte [] symmetricDecrypto (byte [] byteSource) throws Exception
(
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
try
(
int mode = Cipher.DECRYPT_MODE;
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance ( "DES");
byte [] keyData = (1, 9, 8, 2, 0, 8, 2, 1);
DESKeySpec keySpec = new DESKeySpec (keyData);
Key key = keyFactory.generateSecret (keySpec);
Cipher cipher = Cipher.getInstance ( "DES");
cipher.init (mode, key);
int blockSize = cipher.getBlockSize ();
int position = 0;
int length = byteSource.length;
boolean more = true;
while (more)
(
if (position + blockSize <= length)
(
baos.write (cipher.update (byteSource, position, blockSize));
position + = blockSize;
)
else
(
more = false;
)
)
if (position <length)
(
baos.write (cipher.doFinal (byteSource, position, length - position));
)
else
(
baos.write (cipher.doFinal ());
)
return baos.toByteArray ();
)
catch (Exception e)
(
throw e;
)
finally
(
baos.close ();
)
)
/ **
* Hash Algorithm
* @ Param byteSource need to calculate the hash of data
* @ Return the data through the hash calculation of
* @ Throws Exception
* /
public static byte [] hashMethod (byte [] byteSource) throws Exception
(
try
(
MessageDigest currentAlgorithm = MessageDigest.getInstance ( "SHA-1");
currentAlgorithm.reset ();
currentAlgorithm.update (byteSource);
return currentAlgorithm.digest ();
)
catch (Exception e)
(
throw e;
)
)
)



Java skills and code Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.