  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # Restore RSA private key by modulus, public and private exponents using Java Security  » development » Restore RSA private key by modulus, public and private exponents using Java Security

By : Romemeek
Date : November 20 2020, 03:01 PM code :
``````/**
* Find a factor of n by following the algorithm outlined in Handbook of Applied Cryptography, section
*
*/

private static BigInteger findFactor(BigInteger e, BigInteger d, BigInteger n) {
BigInteger edMinus1 = e.multiply(d).subtract(BigInteger.ONE);
int s = edMinus1.getLowestSetBit();
BigInteger t = edMinus1.shiftRight(s);

for (int aInt = 2; true; aInt++) {
BigInteger aPow = BigInteger.valueOf(aInt).modPow(t, n);
for (int i = 1; i <= s; i++) {
if (aPow.equals(BigInteger.ONE)) {
break;
}
if (aPow.equals(n.subtract(BigInteger.ONE))) {
break;
}
BigInteger aPowSquared = aPow.multiply(aPow).mod(n);
if (aPowSquared.equals(BigInteger.ONE)) {
return aPow.subtract(BigInteger.ONE).gcd(n);
}
aPow = aPowSquared;
}
}

}

public static RSAPrivateCrtKey createCrtKey(RSAPublicKey rsaPub, RSAPrivateKey rsaPriv) throws NoSuchAlgorithmException, InvalidKeySpecException {

BigInteger e = rsaPub.getPublicExponent();
BigInteger d = rsaPriv.getPrivateExponent();
BigInteger n = rsaPub.getModulus();
BigInteger p = findFactor(e, d, n);
BigInteger q = n.divide(p);
if (p.compareTo(q) > 1) {
BigInteger t = p;
p = q;
q = t;
}
BigInteger exp1 = d.mod(p.subtract(BigInteger.ONE));
BigInteger exp2 = d.mod(q.subtract(BigInteger.ONE));
BigInteger coeff = q.modInverse(p);
RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(n, e, d, p, q, exp1, exp2, coeff);
KeyFactory kf = KeyFactory.getInstance("RSA");
return (RSAPrivateCrtKey) kf.generatePrivate(keySpec);

}
`````` ## How to factor RSA modulus given the public and private exponent?

By : user2986118
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Yes -- once you know the modulus N, and public/private exponents d and e, it is not too difficult to obtain p and q such that N=pq.
This paper by Dan Boneh describes an algorithm for doing so. It relies on the fact that, by definition, ## RSA Key Values and Modulus in Public/Private Keys

By : QoL
Date : March 29 2020, 07:55 AM
it helps some times The RSAKeyValue, Modulus, and Exponent tags are in there because you used the method ToXmlString().
An RSA public key is made up of the modulus and the public exponent. There is no security issue with distributing these 2 items. HOWEVER, you do NOT want to distribute any of the other items in the Private Key. The private key has these fields:
code :
``````<RSAKeyValue>
<Modulus>…</Modulus>
<Exponent>…</Exponent>
<P>…</P>
<Q>…</Q>
<DP>…</DP>
<DQ>…</DQ>
<InverseQ>…</InverseQ>
<D>…</D>
</RSAKeyValue>
`````` ## Java Security - RSA Public Key & Private Key Code Issue

By : EOpp
Date : March 29 2020, 07:55 AM
will be helpful for those in need Here is my error
code :
``````import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.KeySpec;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;

class RSA{

public static void main(String[] args) throws Exception {
generateKeys();
rsaEncrypt("plaintext.txt", "encrypted.txt");
}

public static void generateKeys() throws Exception {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
KeyPair kp = kpg.genKeyPair();
PublicKey publicKey = kp.getPublic();
PrivateKey privateKey = kp.getPrivate();

System.out.println("keys created");

KeyFactory fact = KeyFactory.getInstance("RSA");
RSAPublicKeySpec pub = fact.getKeySpec(publicKey,
RSAPublicKeySpec.class);
RSAPrivateKeySpec priv = fact.getKeySpec(privateKey,
RSAPrivateKeySpec.class);

saveToFile("public.key", pub.getModulus(), pub.getPublicExponent());
saveToFile("private.key", priv.getModulus(), priv.getPrivateExponent());

System.out.println("keys saved");
}

public static void saveToFile(String fileName, BigInteger mod,
BigInteger exp) throws IOException {
ObjectOutputStream fileOut = new ObjectOutputStream(
new BufferedOutputStream(new FileOutputStream(fileName)));
try {
fileOut.writeObject(mod);
fileOut.writeObject(exp);
} catch (Exception e) {
throw new IOException("Unexpected error");
} finally {
fileOut.close();
System.out.println("Closed writing file.");
}
}

// Return the saved key
static Key readKeyFromFile(String keyFileName) throws IOException {
InputStream in = new FileInputStream(keyFileName);
ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(
in));
try {
KeyFactory fact = KeyFactory.getInstance("RSA");
if (keyFileName.startsWith("public"))
return fact.generatePublic(new RSAPublicKeySpec(m, e));
else
return fact.generatePrivate(new RSAPrivateKeySpec(m, e));
} catch (Exception e) {
throw new RuntimeException("Spurious serialisation error", e);
} finally {
oin.close();
}
}

// Use this PublicKey object to initialize a Cipher and encrypt some data
public static void rsaEncrypt(String file_loc, String file_des)
throws Exception {
String text = "";
while(line!= null)
{
text += line;
}
System.out.println(text);
byte[] data = new byte;
int i;

System.out.println("start encyption");
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);

FileInputStream fileIn = new FileInputStream(file_loc);
FileOutputStream fileOut = new FileOutputStream(file_des);
CipherOutputStream cipherOut = new CipherOutputStream(fileOut, cipher);

// Read in the data from the file and encrypt it
while ((i = fileIn.read(data)) != -1) {
cipherOut.write(data, 0, i);
}

// Close the encrypted file
cipherOut.close();
fileIn.close();

System.out.println("encrypted file created");
}

// Use this PublicKey object to initialize a Cipher and decrypt some data
public static void rsaDecrypt(String file_loc, String file_des)
throws Exception {
byte[] data = new byte;
int i;

System.out.println("start decyption");

Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, priKey);

FileInputStream fileIn = new FileInputStream(file_loc);
CipherInputStream cipherIn = new CipherInputStream(fileIn, cipher);
FileOutputStream fileOut = new FileOutputStream(file_des);

// Write data to new file
while ((i = cipherIn.read()) != -1) {
fileOut.write(i);
}

// Close the file
fileIn.close();
cipherIn.close();
fileOut.close();

System.out.println("decrypted file created");

}
}
`````` ## Generated RSA Public and Private key modulus are the same in Java/Android

By : Pallavi
Date : November 05 2020, 03:01 PM
like below fixes the issue What you generate is a key pair for Java. The public and private keys of the key pair always share the same modulus: the calculations are performed within that domain.
What is kept secure is not the modulus but the private exponent and - generally also available - the parameters required to perform the fast exponentiation using the Chinese Remainder Theorem (CRT). These are basically the parameters such as prime P and Q used to calculate the private key and indeed the modulus. ## Regenerating Public and Private Key based on modulus and public exponent

By : Emily Chan
Date : March 29 2020, 07:55 AM
help you fix your problem In RSA the public key is (n, e), which you already have. Deriving the private key from the modulus is the exact thing RSA wants to be difficult. This is known as the factoring problem. If this was a tractable problem computationally then RSA would not be a useful cryptosystem. In fact, as computers have gotten faster and algorithms for factoring have improved (see: GNFS) the recommended bit length of an RSA key has gotten longer to combat this. Where 1024 was once considered acceptable today 2048 is a minimum recommendation.
You can't sign without the private key, as the idea is that a signature demonstrates possession of the private key and you use the public key (which you distribute in some fashion to the parties who need to validate the signature) to confirm that the signature is genuine. Frequently this is combined with X.509 to provide some identity binding, but it isn't required. 