如何在Android 9.0派中获取ECDH密钥对?

时间:2019-07-04 09:18:16

标签: android encryption cryptography elliptic-curve ecdh

我想获取ECDH密钥对(公共密钥和私有密钥)。此方法不适用于Android 9.0 pie,因为从此版本中删除了安全提供程序“ BC”,“ SC”。我尝试了以下方法

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC", "BC");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
keyGen.initialize(256, random);
KeyFactory kaif = KeyFactory.getInstance("EC", "BC");
KeyPair pair = keyGen.generateKeyPair();
PrivateKey privateKey = pair.getPrivate();
PublicKey publicKey = pair.getPublic();

以下是我使用 “BC” 提供商与波夫代码,EC专用密钥S时得到关键:30e3def89f6aca7ab4e1e0e0367bf936955339db03a0c32c63a08293066f9423 EC公共密钥X:1675a6b1c8097f651be6f6a555ab9e5da83f03d3082041ae29111609b98594be Y:ed23f9263c6a1e8892d03a0c33ed9d8bfc5886dfe67fb7947457e3ff43baffca

方法2:Security.insertProviderAt(BouncyCastleProvider(),1);

当我在gradle中添加Bouncy城​​堡并尝试如上所述启动时,输出如下privateKey = {OpenSSLECPrivateKey @ 7518}“ OpenSSLECPrivateKey {params = {ECDSA-Parameters:(256 bit)\ n}}”“ publicKey = {OpenSSLECPublicKey @ 7519}“公钥:(256位)\ n00000000 04 5c 2c 76 23 09 41 c4 16 e2 99 ea e0 fa ed 16 |。\,v#.A ......... | \ n00000010 52 ca 91 d2 0c fe 7f c4 94 76 54 9a 3c 49 ab a5 | R ........ vT。

我需要以可读格式像上面一样简单,我是否需要进行任何转换以获取字母数字键

3 个答案:

答案 0 :(得分:2)

尝试手动添加const fetch = require("isomorphic-unfetch"); const fs = require("fs"); const path = require("path"); const fse = require("fs-extra"); const withBundleAnalyzer = require("@next/bundle-analyzer")({ enabled: false }); module.exports = withBundleAnalyzer({ webpack(config) { config.node = { fs: "empty", path: "empty" }; return config; }, async exportPathMap() { const response = await fetch( "https://jsonplaceholder.typicode.com/posts?_page=1" ); const postList = await response.json(); fs.writeFileSync( path.resolve(`data/route.json`), JSON.stringify({ postList }, null, 2) ); for (let i = 0; i < postList.length; ++i) { const id = postList[i].id; const response = await fetch( `https://jsonplaceholder.typicode.com/posts/${id}` ); const post = await response.json(); const fn = path.resolve(`data/post/${id}/route.json`); await fse.outputFile(fn, JSON.stringify(post, null, 2)); } const pages = postList.reduce( (pages, post) => Object.assign({}, pages, { [`/post/${post.id}`]: { page: "/post" } }), {} ); return Object.assign({}, pages, { "/": { page: "/" } }); } });

SpongyCastle

将此添加到您的Security.insertProviderAt(BouncyCastleProvider(), 1); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); keyGen.initialize(256, random); KeyFactory kaif = KeyFactory.getInstance("EC"); KeyPair pair = keyGen.generateKeyPair(); PrivateKey privateKey = pair.getPrivate(); PublicKey publicKey = pair.getPublic(); 依赖项中:

build.gradle

确保/* spongy castle */ implementation "com.madgag.spongycastle:core:1.58.0.0" implementation "com.madgag.spongycastle:prov:1.58.0.0" 来自BouncyCastleProvider()

spongycastle

答案 1 :(得分:0)

还可以添加BouncyCastleProvider别名bcprov-jdk15on

dependencies {
    // https://mvnrepository.com/artifact/org.bouncycastle
    implementation "org.bouncycastle:bcprov-jdk15on:1.60"
    implementation "org.bouncycastle:bcpkix-jdk15on:1.60"
}

答案 2 :(得分:0)

删除提供商(“ BC”)并手动插入BouncyCastle

Security.removeProvider("BC");
Security.insertProviderAt(BouncyCastleProvider(), 1);

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
keyGen.initialize(256, random);
KeyFactory kaif = KeyFactory.getInstance("EC");
KeyPair pair = keyGen.generateKeyPair();
PrivateKey privateKey = pair.getPrivate();
PublicKey publicKey = pair.getPublic();

将此添加到您的build.gradle依赖项中:

/* Bouncy castle */
implementation 'org.bouncycastle:com.springsource.org.bouncycastle.jce:1.46.0'