日志文章

2007年08月30日 17:49:05

安全 加密 DES

安全概述:
http://www.blogjava.net/security/archive/2006/09/21/java_security_summary.html

http://www.itebook.net/article/view/Article_13269.htm


from bouncy.... public class DESSecurity extends Object
{
// Encrypting or decrypting ?
private boolean encrypt = true;   // To hold the initialised DESede cipher
private PaddedBufferedBlockCipher cipher = null;   // The input stream of bytes to be processed for encryption
private BufferedInputStream in = null;   // The output stream of bytes to be procssed
private BufferedOutputStream out = null;   // The key
private byte[] key = null;   /*
* start the application
*/
public static void main(String[] args)
{   System.out.print( getSecurity("1", true) );
}   public static String getSecurity(String inputstr, boolean ifencry)
{
  String keyfile = "deskey.dat";
  ByteArrayOutputStream outstream = new ByteArrayOutputStream();
  DESSecurity de = null;
  if(ifencry){
    de = new DESSecurity( inputstr, outstream, keyfile, true, "ZonySoft_");
  }else{
    de = new DESSecurity(inputstr, outstream, keyfile, false, "ZonySoft_");
  }
  de.process();
  //System.out.println( outstream.toString() );
  return outstream.toString() ;
}   // Default constructor, used for the usage message
public DESSecurity()
{
}   public DESSecurity(
      String infile, ByteArrayOutputStream outstream, String keyfile, boolean encrypt,
      String strSeed)
{   this.encrypt = encrypt;   in = new BufferedInputStream(new StringBufferInputStream(infile));
  /**
  *
  */   out = new BufferedOutputStream( outstream );
//   try {     SecureRandom sr = null;
    try {
    sr = new SecureRandom();       sr.setSeed(strSeed.getBytes());
    }
    catch (Exception nsa) {
    System.err.println("Hmmm, no SHA1PRNG, you need the " +
              "Sun implementation");
    System.exit(1);
    }
    KeyGenerationParameters kgp = new KeyGenerationParameters(
    sr,
    DESedeParameters.DES_EDE_KEY_LENGTH * 8);     /*
    * Second, initialise the key generator with the parameters
    */
    DESedeKeyGenerator kg = new DESedeKeyGenerator();
    kg.init(kgp);     /*
    * Third, and finally, generate the key
    */
    key = kg.generateKey();     /*
    * We can now output the key to the file, but first
    * hex encode the key so that we can have a look
    * at it with a text editor if we so desire
    */
//     BufferedOutputStream keystream =
//       new BufferedOutputStream(new FileOutputStream(keyfile));
    /**
    * not to generate the file .
      byte[] keyhex = Hex.encode(key);
      keystream.write(keyhex, 0, keyhex.length);
      keystream.flush();
      keystream.close();
    */
}   private final void process()
{
  /*
    * Setup the DESede cipher engine, create a PaddedBufferedBlockCipher
    * in CBC mode.
    */
  cipher = new PaddedBufferedBlockCipher(
                new CBCBlockCipher(new DESedeEngine()));     if (encrypt)
  {
    performEncrypt(key);
  }
  else
  {
    performDecrypt(key);
  }   // after processing clean up the files
  try
  {
    in.close();
    out.flush();
    out.close();
  }
  catch (IOException closing)
  {   }
}
private final void performEncrypt(byte[] key)
{
  // initialise the cipher with the key bytes, for encryption
  cipher.init(true, new KeyParameter(key));
  // int inBlockSize = cipher.getBlockSize() * 5;
  int inBlockSize = 47;
  int outBlockSize = cipher.getOutputSize(inBlockSize);   byte[] inblock = new byte[inBlockSize];
  byte[] outblock = new byte[outBlockSize];   /*
    * now, read the file, and output the chunks
    */
  try
  {
    int inL;
    int outL;
    byte[] rv = null;
    while ((inL=in.read(inblock, 0, inBlockSize)) > 0)
    {
      outL = cipher.processBytes(inblock, 0, inL, outblock, 0);
      /*
      * Before we write anything out, we need to make sure
      * that we've got something to write out.
      */
      if (outL > 0)
      {
        rv = Hex.encode(outblock, 0, outL);
        out.write(rv, 0, rv.length);
        //out.write('\n');
      }
    }       try
    {
      /*
      * Now, process the bytes that are still buffered
      * within the cipher.
      */
      outL = cipher.doFinal(outblock, 0);
      if (outL > 0)
      {
        rv = Hex.encode(outblock, 0, outL);
        out.write(rv, 0, rv.length);
        //out.write('\n');
      }
    }
    catch (CryptoException ce)
    {       }
  }
  catch (IOException ioeread)
  {
    ioeread.printStackTrace();
  }
}
private final void performDecrypt(byte[] key)
{
  // initialise the cipher for decryption
  cipher.init(false, new KeyParameter(key));   BufferedReader br = new BufferedReader(new InputStreamReader(in));   try
  {
    int outL;
    byte[] inblock = null;
    byte[] outblock = null;
    String rv = null;
    while ((rv = br.readLine()) != null)
    {
      inblock = Hex.decode(rv);
      outblock = new byte[cipher.getOutputSize(inblock.length)];       outL = cipher.processBytes(inblock, 0, inblock.length,       if (outL > 0)
      {
        out.write(outblock, 0, outL);
      }
    }       try
    {       outL = cipher.doFinal(outblock, 0);
      if (outL > 0)
      {
        out.write(outblock, 0, outL);
      }
    }
    catch (CryptoException ce)
    {       }
  }
  catch (IOException ioeread)
  {
    ioeread.printStackTrace();
  }
}}

Tags: JAVA  

类别: 安全 |  评论(0) |  浏览(659) |  收藏
发表评论
看不清楚,换一张