TripleDESCBC.java

Go to the documentation of this file.
00001 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
00002 /*
00003 Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008   1. Redistributions of source code must retain the above copyright notice,
00009      this list of conditions and the following disclaimer.
00010 
00011   2. Redistributions in binary form must reproduce the above copyright 
00012      notice, this list of conditions and the following disclaimer in 
00013      the documentation and/or other materials provided with the distribution.
00014 
00015   3. The names of the authors may not be used to endorse or promote products
00016      derived from this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
00019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
00021 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
00022 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00026 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00027 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 */
00029 
00030 package com.jcraft.jsch.jce;
00031 
00032 import com.jcraft.jsch.Cipher;
00033 import javax.crypto.*;
00034 import javax.crypto.spec.*;
00035 
00036 public class TripleDESCBC implements Cipher{
00037   private static final int ivsize=8;
00038   private static final int bsize=24;
00039   private javax.crypto.Cipher cipher;    
00040   public int getIVSize(){return ivsize;} 
00041   public int getBlockSize(){return bsize;}
00042   public void init(int mode, byte[] key, byte[] iv) throws Exception{
00043     String pad="NoPadding";      
00044     //if(padding) pad="PKCS5Padding";
00045     byte[] tmp;
00046     if(iv.length>ivsize){
00047       tmp=new byte[ivsize];
00048       System.arraycopy(iv, 0, tmp, 0, tmp.length);
00049       iv=tmp;
00050     }
00051     if(key.length>bsize){
00052       tmp=new byte[bsize];
00053       System.arraycopy(key, 0, tmp, 0, tmp.length);
00054       key=tmp;
00055     }
00056 
00057     try{
00058       cipher=javax.crypto.Cipher.getInstance("DESede/CBC/"+pad);
00059 /*
00060       // The following code does not work on IBM's JDK 1.4.1
00061       SecretKeySpec skeySpec = new SecretKeySpec(key, "DESede");
00062       cipher.init((mode==ENCRYPT_MODE?
00063            javax.crypto.Cipher.ENCRYPT_MODE:
00064            javax.crypto.Cipher.DECRYPT_MODE),
00065           skeySpec, new IvParameterSpec(iv));
00066 */
00067       DESedeKeySpec keyspec=new DESedeKeySpec(key);
00068       SecretKeyFactory keyfactory=SecretKeyFactory.getInstance("DESede");
00069       SecretKey _key=keyfactory.generateSecret(keyspec);
00070       cipher.init((mode==ENCRYPT_MODE?
00071            javax.crypto.Cipher.ENCRYPT_MODE:
00072            javax.crypto.Cipher.DECRYPT_MODE),
00073           _key, new IvParameterSpec(iv));
00074     }
00075     catch(Exception e){
00076       cipher=null;
00077       throw e;
00078     }
00079   }
00080   public void update(byte[] foo, int s1, int len, byte[] bar, int s2) throws Exception{
00081     cipher.update(foo, s1, len, bar, s2);
00082   }
00083   public boolean isCBC(){return true; }
00084 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1