// Licensed under the Apache Public Source License 2.0 ("the License").
// You may not use this file except in compliance with the License.
+/*
+ * org.ibex.RSA - By Brian Alliet
+ * Copyright (C) 2004 Brian Alliet
+ */
+
package org.ibex.crypto;
import java.math.BigInteger;
import java.util.*;
public int getInputBlockSize() { return (pq.bitLength()+7) / 8 - (reverse ? 0 : 1); }
public int getOutputBlockSize() { return (pq.bitLength()+7) / 8 - (reverse ? 1 : 0); }
+ // FEATURE: Check that in.length is within the expected range
public byte[] process(byte[] in) {
// output block is the same size as the modulus (rounded up)
int outSize = getOutputBlockSize();
BigInteger t = new BigInteger(1,in);
BigInteger c = t.modPow(e,pq);
byte[] cbytes = c.toByteArray();
- if(cbytes.length > outSize || (reverse && cbytes[0] == 0)) {
+ if(cbytes.length > outSize + 1) throw new RuntimeException("should never happen");
+ if(reverse ? cbytes[0] == 0 : cbytes.length > outSize) {
if(cbytes[0] != 0) throw new RuntimeException("should never happen");
- byte[] buf = new byte[outSize];
- System.arraycopy(cbytes,1,buf,0,outSize);
+ byte[] buf = new byte[cbytes.length-1];
+ System.arraycopy(cbytes,1,buf,0,buf.length);
return buf;
} else if(!reverse && cbytes.length < outSize) {
// output needs to be exactly outSize in length