Jun 18, 2014 In Java generating public private key using RSA algorithm is quite easy as it provides lib to do these tasks. In Java java.security package contains classes to do these operation. Generating public private key pairs. By using KeyPairGenerator class we can generate public/private key pairs as given below.
importorg.bouncycastle.util.encoders.Hex; |
importorg.web3j.crypto.*; |
importjava.math.BigInteger; |
publicclassECCExample { |
publicstaticStringcompressPubKey(BigIntegerpubKey) { |
String pubKeyYPrefix = pubKey.testBit(0) ?'03':'02'; |
String pubKeyHex = pubKey.toString(16); |
String pubKeyX = pubKeyHex.substring(0, 64); |
return pubKeyYPrefix + pubKeyX; |
} |
publicstaticvoidmain(String[] args) throwsException { |
//BigInteger privKey = Keys.createEcKeyPair().getPrivateKey(); |
BigInteger privKey =newBigInteger('97ddae0f3a25b92268175400149d65d6887b9cefaf28ea2c078e05cdc15a3c0a', 16); |
BigInteger pubKey =Sign.publicKeyFromPrivate(privKey); |
ECKeyPair keyPair =newECKeyPair(privKey, pubKey); |
System.out.println('Private key: '+ privKey.toString(16)); |
System.out.println('Public key: '+ pubKey.toString(16)); |
System.out.println('Public key (compressed): '+ compressPubKey(pubKey)); |
String msg ='Message for signing'; |
byte[] msgHash =Hash.sha3(msg.getBytes()); |
Sign.SignatureData signature =Sign.signMessage(msgHash, keyPair, false); |
System.out.println('Msg: '+ msg); |
System.out.println('Msg hash: '+Hex.toHexString(msgHash)); |
System.out.printf('Signature: [v = %d, r = %s, s = %s]n', |
signature.getV() -27, |
Hex.toHexString(signature.getR()), |
Hex.toHexString(signature.getS())); |
System.out.println(); |
BigInteger pubKeyRecovered =Sign.signedMessageToKey(msg.getBytes(), signature); |
System.out.println('Recovered public key: '+ pubKeyRecovered.toString(16)); |
boolean validSig = pubKey.equals(pubKeyRecovered); |
System.out.println('Signature valid? '+ validSig); |
} |
} |
<?xml version='1.0' encoding='UTF-8'?> |
<projectxmlns='http://maven.apache.org/POM/4.0.0' |
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' |
xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd'> |
<modelVersion>4.0.0</modelVersion> |
<groupId>bc-examples</groupId> |
<artifactId>bc-examples</artifactId> |
<version>1.0-SNAPSHOT</version> |
<dependencies> |
<dependency> |
<groupId>org.web3j</groupId> |
<artifactId>crypto</artifactId> |
<version>3.3.1</version> |
</dependency> |
</dependencies> |
</project> |
The expected output is as follows: Modern warfare 2 key code generator. May 20, 2017 Call of Duty: Modern Warfare 2 Serial Key Download Code Crack key generator Full Game Torrent skidrow Origin Key and Steam Online Code Avaiable. Call of Duty: Modern Warfare 2 Serial Key Cd Key Free Download Crack Full Game Call of Duty: Modern Warfare 2 Serial Cd Key Generator License Activator Product Origin Keys Full Game Download Free. Name of Obligation: Trendy Battle 2 Steam CD Key Name for Obligation: The serial quantity or product code that prompts Trendy Warfare 2 is generally printed on a mix of letters and numbers on a label inside the sport's case or on the sport's fast reference card. Quest Obligation: Trendy Warfare 2 Steam CD Key’s used to avoid wasting and activate the corresponding recreation. |
java.security package contains ECDSA classes for generating the key pair, signing and verifying signatures. |
In order to be able to create a digital signature, you need a private key. (Its corresponding public key will be needed in order to verify the authenticity of the signature.)
In some cases the key pair (private key and corresponding public key) are already available in files. In that case the program can import and use the private key for signing, as shown in Weaknesses and Alternatives.
In other cases the program needs to generate the key pair. A key pair is generated by using the KeyPairGenerator
class.
In this example you will generate a public/private key pair for the Digital Signature Algorithm (DSA). You will generate keys with a 1024-bit length.
Generating a key pair requires several steps:
Create a Key Pair Generator
The first step is to get a key-pair generator object for generating keys for the DSA signature algorithm.
As with all engine classes, the way to get a KeyPairGenerator
object for a particular type of algorithm is to call the getInstance
static factory method on the KeyPairGenerator
class. This method has two forms, both of which hava a String algorithm
first argument; one form also has a String provider
second argument.
A caller may thus optionally specify the name of a provider, which will guarantee that the implementation of the algorithm requested is from the named provider. The sample code of this lesson always specifies the default SUN provider built into the JDK.
Put the following statement after the
line in the file created in the previous step, Prepare Initial Program Structure:
Initialize the Key Pair Generator
The next step is to initialize the key pair generator. All key pair generators share the concepts of a keysize and a source of randomness. The KeyPairGenerator
class has an initialize
method that takes these two types of arguments.
The keysize for a DSA key generator is the key length (in bits), which you will set to 1024.
The source of randomness must be an instance of the SecureRandom
class that provides a cryptographically strong random number generator (RNG). For more information about SecureRandom
, see the SecureRandom API Specification and the Java Cryptography Architecture Reference Guide .
The following example requests an instance of SecureRandom
that uses the SHA1PRNG algorithm, as provided by the built-in SUN provider. The example then passes this SecureRandom
instance to the key-pair generator initialization method.
Some situations require strong random values, such as when creating high-value and long-lived secrets like RSA public and private keys. To help guide applications in selecting a suitable strong SecureRandom
implementation, starting from JDK 8 Java distributions include a list of known strong SecureRandom
implementations in the securerandom.strongAlgorithms
property of the java.security.Security
class. When you are creating such data, you should consider using SecureRandom.getInstanceStrong()
, as it obtains an instance of the known strong algorithms.
Generate the Pair of Keys
The final step is to generate the key pair and to store the keys in PrivateKey
and PublicKey
objects.