Last active
August 9, 2017 11:49
-
-
Save asanso/6d33e91a9f85f3d4dba4ec519eece6e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package orig; | |
import java.math.BigInteger; | |
import java.security.InvalidKeyException; | |
import java.security.KeyFactory; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.PrivateKey; | |
import java.security.PublicKey; | |
import java.security.spec.ECFieldFp; | |
import java.security.spec.ECParameterSpec; | |
import java.security.spec.ECPoint; | |
import java.security.spec.ECPrivateKeySpec; | |
import java.security.spec.EllipticCurve; | |
import java.security.spec.InvalidKeySpecException; | |
import javax.crypto.KeyAgreement; | |
import java.security.spec.ECPublicKeySpec; | |
public class EccJava521 { | |
public static void main(String[] args) throws Exception { | |
//g the generator which is also known as the base point. | |
ECPoint point = new ECPoint( | |
// gx | |
new BigInteger("26617408020502170632287687167233609607298591687569" + | |
"73147706671368418802944996427808491545080627771902" + | |
"35209424122506555866215711354557091681416163731589" + | |
"5999846"), | |
// gy | |
new BigInteger("37571800257700204635455072244911836035944551347697" + | |
"62486694567779615544477440556316691234405012945539" + | |
"56214444453728942852258566672919658081012434427757" + | |
"8376784")); | |
final ECParameterSpec P521 = new ECParameterSpec( | |
new EllipticCurve( | |
// field the finite field that this elliptic curve is over. | |
new ECFieldFp(new BigInteger("68647976601306097149819007990813932172694353001433" + | |
"05409394463459185543183397656052122559640661454554" + | |
"97729631139148085803712198799971664381257402829111" + | |
"5057151")), | |
// a the first coefficient of this elliptic curve. | |
new BigInteger("68647976601306097149819007990813932172694353001433" + | |
"05409394463459185543183397656052122559640661454554" + | |
"97729631139148085803712198799971664381257402829111" + | |
"5057148"), | |
// b the second coefficient of this elliptic curve. | |
new BigInteger("10938490380737342745111123907668055699362075989516" + | |
"83748994586394495953116150735016013708737573759623" + | |
"24859213229670631330943845253159101291214232748847" + | |
"8985984") | |
), | |
point, | |
// Order n | |
new BigInteger("68647976601306097149819007990813932172694353001433" + | |
"05409394463459185543183397655394245057746333217197" + | |
"53296399637136332111386476861244038034037280889270" + | |
"7005449"), | |
1); | |
ECParameterSpec ecParameterSpec = P521; | |
KeyFactory keyFactory = KeyFactory.getInstance("EC"); | |
ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(point, ecParameterSpec); | |
PublicKey publicKey = keyFactory.generatePublic(pubKeySpec); | |
getAgreedKey(new BigInteger("6864797660130609714981900799081393217269435300143305409394463459185543183397655394245057746333217197532963996371363321113864768612440380340372808892707005431"),keyFactory, publicKey, ecParameterSpec); | |
} | |
private static byte[] getAgreedKey(BigInteger counter, KeyFactory keyFactory, PublicKey publicKey, ECParameterSpec ecParameterSpec) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException { | |
ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(counter, ecParameterSpec); | |
PrivateKey recoveredPrivateKey = keyFactory.generatePrivate(privateKeySpec); | |
KeyAgreement keyAgreement = KeyAgreement.getInstance("ECDH"); | |
keyAgreement.init(recoveredPrivateKey); | |
keyAgreement.doPhase(publicKey, true); | |
return keyAgreement.generateSecret(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment