byte[] data = Hex.decode(edgeInput);
//
// RAW
//
AsymmetricBlockCipher eng = new RSAEngine();
eng.init(true, pubParameters);
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
}
eng.init(false, privParameters);
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
}
if (!edgeInput.equals(new String(Hex.encode(data))))
{
return new SimpleTestResult(false, "RSA: failed RAW edge Test");
}
data = Hex.decode(input);
eng.init(true, pubParameters);
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
}
eng.init(false, privParameters);
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
}
if (!input.equals(new String(Hex.encode(data))))
{
return new SimpleTestResult(false, "RSA: failed RAW Test");
}
//
// PKCS1 - public encrypt, private decrypt
//
eng = new PKCS1Encoding(eng);
eng.init(true, pubParameters);
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
}
eng.init(false, privParameters);
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
}
if (!input.equals(new String(Hex.encode(data))))
{
return new SimpleTestResult(false, "RSA: failed PKCS1 public/private Test");
}
//
// PKCS1 - private encrypt, public decrypt
//
eng = new PKCS1Encoding(((PKCS1Encoding)eng).getUnderlyingCipher());
eng.init(true, privParameters);
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
}
eng.init(false, pubParameters);
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
}
if (!input.equals(new String(Hex.encode(data))))
{
return new SimpleTestResult(false, "RSA: failed PKCS1 private/public Test");
}
//
// OAEP - public encrypt, private decrypt
//
eng = new OAEPEncoding(((PKCS1Encoding)eng).getUnderlyingCipher());
eng.init(true, pubParameters);
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
}
eng.init(false, privParameters);
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
}
if (!input.equals(new String(Hex.encode(data))))
{
return new SimpleTestResult(false, "RSA: failed OAEP Test");
}
RSAKeyPairGenerator pGen = new RSAKeyPairGenerator();
RSAKeyGenerationParameters genParam = new RSAKeyGenerationParameters(
BigInteger.valueOf(0x11), new SecureRandom(), 768, 25);
pGen.init(genParam);
AsymmetricCipherKeyPair pair = pGen.generateKeyPair();
eng = new RSAEngine();
if (((RSAKeyParameters)pair.getPublic()).getModulus().bitLength() < 762)
{
return new SimpleTestResult(false, "RSA: failed key generation (768) length test");
}
eng.init(true, pair.getPublic());
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
}
eng.init(false, pair.getPrivate());
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
}
if (!input.equals(new String(Hex.encode(data))))
{
return new SimpleTestResult(false, "RSA: failed key generation (768) Test");
}
genParam = new RSAKeyGenerationParameters(BigInteger.valueOf(0x11), new SecureRandom(), 1024, 25);
pGen.init(genParam);
pair = pGen.generateKeyPair();
eng.init(true, pair.getPublic());
if (((RSAKeyParameters)pair.getPublic()).getModulus().bitLength() < 1018)
{
return new SimpleTestResult(false, "RSA: failed key generation (1024) length test");
}
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
}
eng.init(false, pair.getPrivate());
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
}