X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fcrypto%2FRSA.java;h=e3c179ce63427fb2349682879d9206b2ca578932;hb=HEAD;hp=31d613d1511f08c3c1dd235e85421beffcbce1e3;hpb=4c1f76f2d51e8c0a8465a96d34ee201e35b00475;p=org.ibex.crypto.git diff --git a/src/org/ibex/crypto/RSA.java b/src/org/ibex/crypto/RSA.java index 31d613d..e3c179c 100644 --- a/src/org/ibex/crypto/RSA.java +++ b/src/org/ibex/crypto/RSA.java @@ -1,6 +1,15 @@ -package com.brian_web.crypto; +// Copyright 2000-2005 the Contributors, as shown in the revision logs. +// 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 class RSA { private final BigInteger pq; @@ -16,16 +25,18 @@ public class RSA { 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 @@ -47,5 +58,30 @@ public class RSA { exponent = (BigInteger) seq.elementAt(1); } } - + + public static class PrivateKey { + public final int version; + public final BigInteger modulus; + public final BigInteger publicExponent; + public final BigInteger privateExponent; + public final BigInteger prime1; + public final BigInteger prime2; + public final BigInteger exponent1; + public final BigInteger exponent2; + public final BigInteger coefficient; + + public PrivateKey(Object o) throws DER.Exception { + Vector seq = (Vector) o; + version = ((Number)seq.elementAt(0)).intValue(); + if(version != 0) throw new DER.Exception("Only private key version 0 is supported"); + modulus = (BigInteger) seq.elementAt(1); + publicExponent = (BigInteger) seq.elementAt(2); + privateExponent = (BigInteger) seq.elementAt(3); + prime1 = (BigInteger) seq.elementAt(4); + prime2 = (BigInteger) seq.elementAt(5); + exponent1 = (BigInteger) seq.elementAt(6); + exponent2 = (BigInteger) seq.elementAt(7); + coefficient = (BigInteger) seq.elementAt(8); + } + } }