1 64 65 package com.jcorporate.expresso.core.security.weakencryption; 66 67 import com.jcorporate.expresso.core.misc.ByteArrayCounter; 68 import com.jcorporate.expresso.core.security.AbstractStringEncryption; 69 import com.jcorporate.expresso.kernel.exception.ChainedException; 70 71 72 84 public class StringEncryption 85 extends AbstractStringEncryption { 86 static final private String thisClass = "com.jcorporate.expresso.core.security.weakencryption.RandomNumber"; 87 static protected ByteArrayCounter ivCounter = new ByteArrayCounter(8); 88 89 92 public StringEncryption() 93 throws ChainedException { 94 super(); 95 } 96 97 104 public byte[] decrypt(byte[] inputData) 105 throws ChainedException, IllegalArgumentException { 106 if (inputData.length < 8) { 107 108 109 return inputData; 110 111 } 114 115 byte[] ivData = new byte[8]; 116 byte[] rawData = new byte[inputData.length - 8]; 117 118 int pwCounter = 0; 120 byte[] passKey = this.getPreparedPassKey(); 121 122 for (int i = 0; i < inputData.length; i++) { 123 inputData[i] ^= passKey[pwCounter]; 124 pwCounter++; 125 126 if (pwCounter == passKey.length) { 127 pwCounter = 0; 128 } 129 } 130 for (int i = 0; i < inputData.length; i++) { 132 if (i < 8) { 133 ivData[i] = inputData[i]; 134 } else { 135 rawData[i - 8] = inputData[i]; 136 } 137 } 138 139 return rawData; 140 } 141 142 143 150 public byte[] encrypt(byte[] inputData) 151 throws ChainedException, IllegalArgumentException { 152 final String myName = thisClass + ".encrypt(byte)"; 153 154 if (inputData.length == 0) { 155 throw new IllegalArgumentException (myName + 156 ":inputData must not be zero length"); 157 } 158 159 ivCounter.increment(); 160 161 byte[] ivData = ivCounter.getBytes(); 162 int arrayLength = 8 + inputData.length; 163 byte[] finalData = new byte[arrayLength]; 164 165 for (int i = 0; i < arrayLength; i++) { 168 if (i < 8) { 169 finalData[i] = ivData[i]; 170 } else { 171 finalData[i] = inputData[i - 8]; 172 } 173 } 174 175 int pwCounter = 0; 177 byte[] passKey = this.getPreparedPassKey(); 178 179 for (int i = 0; i < finalData.length; i++) { 180 finalData[i] ^= passKey[pwCounter]; 181 pwCounter++; 182 183 if (pwCounter == passKey.length) { 184 pwCounter = 0; 185 } 186 } 187 188 return finalData; 189 } 190 191 192 } 193 194 195 | Popular Tags |