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