// cmd/ethkey/generate.go var commandGenerate = cli.Command{ Name: "generate", Usage: "generate new keyfile", ArgsUsage: "[ <keyfile> ]", ... Action: func(ctx *cli.Context)error { ... var privateKey *ecdsa.PrivateKey var err error if file := ctx.String("privatekey"); file != "" { // Load private key from file. privateKey, err = crypto.LoadECDSA(file) if err != nil { utils.Fatalf("Can't load private key: %v", err) } } else { // If not loaded, generate random. // 这里生成 私钥 privateKey, err = crypto.GenerateKey() if err != nil { utils.Fatalf("Failed to generate random private key: %v", err) } }
// 下面是生成 account address 和 keystore
// Create the keyfile object with a random UUID. id := uuid.NewRandom() key := &keystore.Key{ Id: id, Address: crypto.PubkeyToAddress(privateKey.PublicKey), PrivateKey: privateKey, }
//---------------------------- // Encrypt with public key //----------------------------
// public key for test String publicKeyValue = "30d67dc730d0d253df841d82baac12357430de8b8f5ce8a35e254e1982c304554fdea88c9cebdda72bf6be9b14aa684288eb3ba6f9cb7b7872b6e41d2b9706fc"; String prePublicKeyStr = publicKeyValue.substring(0, 64); String postPublicKeyStr = publicKeyValue.substring(64);
EllipticCurve ellipticCurve = new EllipticCurve(new ECFieldFp(fieldP), new BigInteger("0"), new BigInteger("7")); ECPoint pointG = new ECPoint(pointGPre, pointGPost); ECNamedCurveSpec namedCurveSpec = new ECNamedCurveSpec("secp256k1", ellipticCurve, pointG, factorN);
// public key SecP256K1Curve secP256K1Curve = new SecP256K1Curve(); SecP256K1Point secP256K1Point = new SecP256K1Point(secP256K1Curve, new SecP256K1FieldElement(new BigInteger(prePublicKeyStr, 16)), new SecP256K1FieldElement(new BigInteger(postPublicKeyStr, 16))); SecP256K1Point secP256K1PointG = new SecP256K1Point(secP256K1Curve, new SecP256K1FieldElement(pointGPre), new SecP256K1FieldElement(pointGPost)); ECDomainParameters domainParameters = new ECDomainParameters(secP256K1Curve, secP256K1PointG, factorN); ECPublicKeyParameters publicKeyParameters = new ECPublicKeyParameters(secP256K1Point, domainParameters); BCECPublicKey publicKeySelf = new BCECPublicKey("ECDSA", publicKeyParameters, namedCurveSpec, BouncyCastleProvider.CONFIGURATION);
// begin encrypt
cipher.init(Cipher.ENCRYPT_MODE, publicKeySelf, iesParams); String cleartextFile = "test/source.txt"; String ciphertextFile = "test/cipher.txt"; byte[] block = newbyte[64]; FileInputStream fis = new FileInputStream(cleartextFile); FileOutputStream fos = new FileOutputStream(ciphertextFile); CipherOutputStream cos = new CipherOutputStream(fos, cipher);
int i; while ((i = fis.read(block)) != -1) { cos.write(block, 0, i); } cos.close();
//---------------------------- // Decrypt with private key //----------------------------
// private key for test, match with public key above BigInteger privateKeyValue = new BigInteger("eb06bde0e1e9427b3e23ab010a124e8cea0d9242b5406eff295f0a501b49db3", 16);
ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(privateKeyValue, namedCurveSpec); BCECPrivateKey privateKeySelf = new BCECPrivateKey("ECDSA", privateKeySpec, BouncyCastleProvider.CONFIGURATION);
// begin decrypt String cleartextAgainFile = "test/decrypt.txt"; cipher.init(Cipher.DECRYPT_MODE, privateKeySelf, iesParams); fis = new FileInputStream(ciphertextFile); CipherInputStream cis = new CipherInputStream(fis, cipher); fos = new FileOutputStream(cleartextAgainFile); while ((i = cis.read(block)) != -1) { fos.write(block, 0, i); } fos.close(); } }
KDF(Key Derivation Function) derives one or more secret keys from a secret value such as a master key, a password, or a passphrase using a pseudorandom function.
下图为加密之前的 source.txt 文本文件:
下图为通过 public key 加密之后的 encrypt.txt 文本文件,非文本格式,无法识别: