1 2 29 30 package com.jcraft.jsch.jce; 31 32 import com.jcraft.jsch.Cipher; 33 import javax.crypto.spec.*; 34 35 public class AES128CBC implements Cipher{ 36 private static final int ivsize=16; 37 private static final int bsize=16; 38 private javax.crypto.Cipher cipher; 39 public int getIVSize(){return ivsize;} 40 public int getBlockSize(){return bsize;} 41 public void init(int mode, byte[] key, byte[] iv) throws Exception { 42 String pad="NoPadding"; 43 byte[] tmp; 44 if(iv.length>ivsize){ 45 tmp=new byte[ivsize]; 46 System.arraycopy(iv, 0, tmp, 0, tmp.length); 47 iv=tmp; 48 } 49 if(key.length>bsize){ 50 tmp=new byte[bsize]; 51 System.arraycopy(key, 0, tmp, 0, tmp.length); 52 key=tmp; 53 } 54 55 try{ 56 SecretKeySpec keyspec=new SecretKeySpec(key, "AES"); 57 cipher=javax.crypto.Cipher.getInstance("AES/CBC/"+pad); 58 cipher.init((mode==ENCRYPT_MODE? 59 javax.crypto.Cipher.ENCRYPT_MODE: 60 javax.crypto.Cipher.DECRYPT_MODE), 61 keyspec, new IvParameterSpec(iv)); 62 } 63 catch(Exception e){ 64 cipher=null; 66 } 67 } 68 public void update(byte[] foo, int s1, int len, byte[] bar, int s2) throws Exception { 69 cipher.update(foo, s1, len, bar, s2); 70 } 71 } 72 | Popular Tags |