FileDocCategorySizeDatePackage
RSAKey.javaAPI DocphoneME MR2 API (J2ME)5821Wed May 02 18:00:24 BST 2007com.sun.midp.crypto

RSAKey

public class RSAKey extends Object implements Key
Implements RSAKey with methods to set and get RSA exponent and modulus.

Fields Summary
byte[]
exp
Local variable to hold the exponent.
byte[]
mod
The number of bytes allocated to the modulus is always a multiple of 8.
Constructors Summary
RSAKey(byte[] modulus, int modOffset, int modLen, byte[] exponent, int expOffset, int expLen)
Constructor for RSA public key.

param
modulus modulus of key to process
param
modOffset offset of the modulus
param
modLen length of modulus in bytes
param
exponent exponent the key
param
expOffset offset of the exponent
param
expLen length of the exponent in bytes

 

                                                     
         
                 
        setModulus(modulus, modOffset, modLen);
        setExponent(exponent, expOffset, expLen);
    
Methods Summary
public booleanequals(RSAPrivateKey k)
Compare the current key as a RSA private key.

param
k the key to compare
return
true if the kesy are the same.

	return equals((RSAKey) k);
    
public booleanequals(RSAPublicKey k)
Compare the current key as a RSA public key.

param
k the key to compare
return
true if the keys are the same.

	return equals((RSAKey) k);
    
public booleanequals(com.sun.midp.crypto.RSAKey k)
Compare the current key as a generic RSA key.

param
k the key to compare
return
true if the keys are the same.

	byte[] kexp = new byte[exp.length];
	byte[] kmod = new byte[mod.length];

	if (this.getClass() != k.getClass())
	    return false;
	if (k.getExponent(kexp, (short) 0) != exp.length)
	    return false;
	if (k.getModulus(kmod, (short) 0) != mod.length)
	    return false;
	for (int i = 0; i < exp.length; i++)
	    if (kexp[i] != exp[i])
		return false;
	for (int i = 0; i < mod.length; i++)
	    if (kmod[i] != mod[i])
		return false;
	return true;
    
public java.lang.StringgetAlgorithm()
Gets the type of the current key.

return
the type of the key

	return "RSA";
    
public byte[]getEncoded()
Returns the encoding of key.

return
null because there is no encoding

 
        return null;
    
public shortgetExponent(byte[] buf, short off)
Get the exponent for the key.

param
buf output buffer to hold the exponent
param
off offset in the buffer to copy the exponent
return
length of the data copied

	if (off + exp.length > buf.length)
	    return 0;

	System.arraycopy(exp, 0, buf, off, exp.length);
	return ((short) exp.length);
    
public java.lang.StringgetFormat()
Returns the name of the encoding format for this key.

return
the string "RAW".

	
        return "RAW"; 
    
public shortgetModulus(byte[] buf, short off)
Get the modulus for the key.

param
buf output buffer to hold the modulus
param
off offset in the buffer to copy the modulus
return
length of the data copied
see
#setModulus

	if (off + mod.length > buf.length)
	    return ((short) 0);
	System.arraycopy(mod, 0, buf, off, mod.length);
	return ((short) mod.length);
    
public intgetModulusLen()
Get the length of modulus for the key.

return
length of the modulus in bytes

        return mod.length;
    
private voidsetExponent(byte[] buf, int off, int len)
Set the exponent for the key.

param
buf input buffer which hold the exponent
param
off offset in the buffer
param
len length of the data to be copied
see
#getExponent
exception
IllegalArgumentException if exponent is too large

	exp = new byte[len];
	System.arraycopy(buf, off, exp, 0, len);
    
private voidsetModulus(byte[] buf, int off, int len)
Set the modulus for the key.

param
buf input buffer which hold the modulus
param
off offset in the buffer
param
len length of the data to be copied in bytes
see
#getModulus

        // move modulus len out to a multiple of 64 bits (8 bytes)
        len = (len + 7) / 8 * 8;

	mod = new byte[len];
	System.arraycopy(buf, off, mod, 0, len);
    
public java.lang.StringtoString()
Convert the key to a readable string.

return
string representation of key

	return ("[" + (getModulusLen() * 8) + "-bit RSA key" +
		", Exponent: 0x" + Util.hexEncode(exp) + 
		", Modulus: 0x" + Util.hexEncode(mod) + "]");