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