1 2 29 30 package com.jcraft.jsch.jce; 31 32 import com.jcraft.jsch.Cipher; 33 import javax.crypto.*; 34 import javax.crypto.spec.*; 35 36 public class TripleDESCBC implements Cipher{ 37 private static final int ivsize=8; 38 private static final int bsize=24; 39 private javax.crypto.Cipher cipher; 40 public int getIVSize(){return ivsize;} 41 public int getBlockSize(){return bsize;} 42 public void init(int mode, byte[] key, byte[] iv) throws Exception { 43 String pad="NoPadding"; 44 byte[] tmp; 46 if(iv.length>ivsize){ 47 tmp=new byte[ivsize]; 48 System.arraycopy(iv, 0, tmp, 0, tmp.length); 49 iv=tmp; 50 } 51 if(key.length>bsize){ 52 tmp=new byte[bsize]; 53 System.arraycopy(key, 0, tmp, 0, tmp.length); 54 key=tmp; 55 } 56 57 try{ 58 cipher=javax.crypto.Cipher.getInstance("DESede/CBC/"+pad); 59 67 DESedeKeySpec keyspec=new DESedeKeySpec(key); 68 SecretKeyFactory keyfactory=SecretKeyFactory.getInstance("DESede"); 69 SecretKey _key=keyfactory.generateSecret(keyspec); 70 cipher.init((mode==ENCRYPT_MODE? 71 javax.crypto.Cipher.ENCRYPT_MODE: 72 javax.crypto.Cipher.DECRYPT_MODE), 73 _key, new IvParameterSpec(iv)); 74 } 75 catch(Exception e){ 76 System.err.println(e); 77 cipher=null; 78 } 79 } 80 public void update(byte[] foo, int s1, int len, byte[] bar, int s2) throws Exception { 81 cipher.update(foo, s1, len, bar, s2); 82 } 83 } 84 | Popular Tags |