0

I need to generate RSA key pair in java.I tried the following,

<%@page import="java.security.Key"%>
<%@page import="java.security.KeyPair"%>
<%@page import="java.security.KeyPairGenerator"%>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <%


        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
        kpg.initialize(2048);
        KeyPair kp = kpg.genKeyPair();
        Key publicKey = kp.getPublic();
        Key privateKey = kp.getPrivate();
        out.println("PrivateKey:" + privateKey);
        out.println("PublicKey:" +publicKey);

    %>
</body>
 </html>

when the page is running in netbeans(this page only) an error is occuring.,error: package

sun.org.mozilla.javascript.internal.regexp does not exist

when the entire project is running I got the output..., but the generated public key is too long...,like this

PrivateKey:sun.security.rsa.RSAPrivateCrtKeyImpl@b8a7c

PublicKey:Sun RSA public key, 2048 bits modulus:

16357206704297604671856121853158662273841275717667103178663872982510600516942159

92471768797559279747649637039251872720857162699034207744835023844213276461437235

62716346732316118850882643586149442248236190221255104694771208469870082732902270

59176928873062588804197238673756206442086637249330898308938378378066971049120606

00637770477260198883852885925396692544417880794817246467903698369172064896388091

16103893445868520394887338681032080760488563541369139420725965115593026544388053

89245256261473050095495300460611881341368409054850562520674680342153131165041561

752280363820799023393672676767368529573441046320095568301 
public exponent: 65537

I want to insert this public key and private key into database..,so it should be small.., please help me......,

Balayesu Chilakalapudi
  • 1,386
  • 3
  • 19
  • 43

2 Answers2

0

RSA keys are large by definition for any viable security. If you need smaller keys you need to switch either to symmetric encryption (AES) - if appropriate - or Elliptic Curve Cryptography (ECC). With ECC you still need to use known or named curves otherwise your key will still be large.

There is a trick to create smaller RSA keys: let them be generated with a pseudo random number generator in a known state (and just save this state). This solution however relies on implementation details; if the implementation changes only very slightly you may get a different key pair so this is not recommended. Hopefully I can be forgiven for mentioning it all on Stackoverflow.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • Direct answer, though I wonder why your database needs to be *that* small. Normal servers should handle these kind of sizes without too much trouble. – Maarten Bodewes Mar 05 '14 at 08:37
  • Absolutely unrelated to the crypto code. You probably need a separate question and post the entire stacktrace. – Maarten Bodewes Mar 05 '14 at 10:56
-1

You are specifying key size as 2048 bits. modify it as per your needs.

change this line

kpg.initialize(2048);

to whatever length you want your key to be(whatever length your database supports)

kpg.initialize(length_of_key);

Please check the documentation of the initialize method below :

http://docs.oracle.com/javase/7/docs/api/java/security/KeyPairGenerator.html#initialize(int)

Rajesh Pantula
  • 10,061
  • 9
  • 43
  • 52
  • Check http://keylength.com to see more information on secure key sizes. 512 bits is may be too large for you, but it certainly is not secure. – Maarten Bodewes Mar 05 '14 at 10:57