1 29 package Acme.Crypto; 30 31 import java.io.*; 32 33 43 public abstract class Cipher extends CryptoUtils 44 { 45 46 public Cipher( int keySize ) 48 { 49 this.keySize = keySize; 50 } 51 52 public int keySize; 55 56 public int keySize() 58 { 59 return keySize; 60 } 61 62 public abstract void setKey( byte[] key ); 64 65 66 68 public void setKey( String keyStr ) 70 { 71 setKey( makeKey( keyStr ) ); 72 } 73 74 public byte[] makeKey( String keyStr ) 76 { 77 byte[] key; 78 if ( keySize == 0 ) 79 key = new byte[keyStr.length()]; 80 else 81 key = new byte[keySize]; 82 int i, j; 83 84 for ( j = 0; j < key.length; ++j ) 85 key[j] = 0; 86 87 for ( i = 0, j = 0; i < keyStr.length(); ++i, j = (j+1) % key.length ) 88 key[j] ^= (byte) keyStr.charAt( i ); 89 90 return key; 91 } 92 93 } 94 | Popular Tags |