Convert Ed25519PrivateKeyParameters to PKCS8

I have generated an Ed25519 key pair using bouncy castle. For my own source of randomness I need to use BC.

Converting it to PKCS8, to be able to use the key without BC, the encoded key spec is not recognized.


Goal

Ed25519PrivateKeyParameters.getEncoded() to PrivateKey (PKCS8EncodedKeySpec).

Do you know, that format / bin encoding BC use at class Ed25519PrivateKeyParameters?


Ed25519PrivateKeyParameters
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public byte[] getEncoded()
</code>
<code>public byte[] getEncoded() </code>
public byte[] getEncoded()

Source

Currently I am failing in converting it to PKCS8.

Not working:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>KeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
java.security.KeyFactory keyFactory = java.security.KeyFactory.getInstance("Ed25519", "BC");
PrivateKey privKey = keyFactory.generatePrivate(keySpec);
</code>
<code>KeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes); java.security.KeyFactory keyFactory = java.security.KeyFactory.getInstance("Ed25519", "BC"); PrivateKey privKey = keyFactory.generatePrivate(keySpec); </code>
KeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
java.security.KeyFactory keyFactory = java.security.KeyFactory.getInstance("Ed25519", "BC");
PrivateKey privKey = keyFactory.generatePrivate(keySpec);

failing with

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>java.security.spec.InvalidKeySpecException: encoded key spec not recognized: failed to construct sequence from byte[]: long form definite-length more than 31 bits
</code>
<code>java.security.spec.InvalidKeySpecException: encoded key spec not recognized: failed to construct sequence from byte[]: long form definite-length more than 31 bits </code>
java.security.spec.InvalidKeySpecException: encoded key spec not recognized: failed to construct sequence from byte[]: long form definite-length more than 31 bits

Extra

Generate a key
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public static Ed25519PrivateKeyParameters createKeyBc() throws Exception {
Security.addProvider(new BouncyCastleProvider());
Ed25519KeyPairGenerator generator = new Ed25519KeyPairGenerator();
Ed25519KeyGenerationParameters spec = new Ed25519KeyGenerationParameters(new SecureRandom());
generator.init(spec);
AsymmetricCipherKeyPair kp = generator.generateKeyPair();
return (Ed25519PrivateKeyParameters) kp.getPrivate();
}
</code>
<code>public static Ed25519PrivateKeyParameters createKeyBc() throws Exception { Security.addProvider(new BouncyCastleProvider()); Ed25519KeyPairGenerator generator = new Ed25519KeyPairGenerator(); Ed25519KeyGenerationParameters spec = new Ed25519KeyGenerationParameters(new SecureRandom()); generator.init(spec); AsymmetricCipherKeyPair kp = generator.generateKeyPair(); return (Ed25519PrivateKeyParameters) kp.getPrivate(); } </code>
public static Ed25519PrivateKeyParameters createKeyBc() throws Exception {

        Security.addProvider(new BouncyCastleProvider());

        Ed25519KeyPairGenerator generator = new Ed25519KeyPairGenerator();
        Ed25519KeyGenerationParameters spec = new Ed25519KeyGenerationParameters(new SecureRandom());
        generator.init(spec);

        AsymmetricCipherKeyPair kp = generator.generateKeyPair();

        return (Ed25519PrivateKeyParameters) kp.getPrivate();
    }
Convert key
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public static PrivateKey convertToJavaPrivateKey(Ed25519PrivateKeyParameters privateKeyParameters)
throws Exception {
byte[] privateKeyBytes = privateKeyParameters.getEncoded();
KeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
java.security.KeyFactory keyFactory = java.security.KeyFactory.getInstance("Ed25519", "BC");
return keyFactory.generatePrivate(keySpec);
}
</code>
<code>public static PrivateKey convertToJavaPrivateKey(Ed25519PrivateKeyParameters privateKeyParameters) throws Exception { byte[] privateKeyBytes = privateKeyParameters.getEncoded(); KeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes); java.security.KeyFactory keyFactory = java.security.KeyFactory.getInstance("Ed25519", "BC"); return keyFactory.generatePrivate(keySpec); } </code>
public static PrivateKey convertToJavaPrivateKey(Ed25519PrivateKeyParameters privateKeyParameters)
   throws Exception {

        byte[] privateKeyBytes = privateKeyParameters.getEncoded();

        KeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);

        java.security.KeyFactory keyFactory = java.security.KeyFactory.getInstance("Ed25519", "BC");

        return keyFactory.generatePrivate(keySpec);
    }

So my goal:

Ed25519PrivateKeyParameters.getEncoded() should be converted to PKCS8.

Thank you for your answer

The following code illustrates one method of doing this conversion. I think the only step you were missing was PrivateKeyInfo privInfo = PrivateKeyInfoFactory.createPrivateKeyInfo(privateKeyParameters);

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.generators.Ed25519KeyPairGenerator;
import org.bouncycastle.crypto.params.Ed25519KeyGenerationParameters;
import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters;
import org.bouncycastle.crypto.util.PrivateKeyInfoFactory;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.spec.PKCS8EncodedKeySpec;
public class Ed25519PrivBcToJava1 {
public static void main(String[] args) throws Exception {
AsymmetricCipherKeyPair keyPair = generateEd25519KeyPair();
Ed25519PrivateKeyParameters privateKeyParameters = (Ed25519PrivateKeyParameters) keyPair.getPrivate();
PrivateKey privKey = convertToJavaPrivateKey(privateKeyParameters);
}
private static AsymmetricCipherKeyPair generateEd25519KeyPair() {
SecureRandom rand = new SecureRandom();
Ed25519KeyGenerationParameters kpgParams = new Ed25519KeyGenerationParameters(rand);
Ed25519KeyPairGenerator kpg = new Ed25519KeyPairGenerator();
kpg.init(kpgParams);
return kpg.generateKeyPair();
}
public static PrivateKey convertToJavaPrivateKey(Ed25519PrivateKeyParameters privateKeyParameters) throws Exception {
PrivateKeyInfo privInfo = PrivateKeyInfoFactory.createPrivateKeyInfo(privateKeyParameters);
byte[] pkcs8EncodedBytes = privInfo.getEncoded();
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(pkcs8EncodedBytes);
// System.out.println(Base64.getEncoder().encodeToString(pkcs8EncodedBytes));
KeyFactory kf = KeyFactory.getInstance("Ed25519");
return kf.generatePrivate(pkcs8KeySpec);
}
}
</code>
<code>import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; import org.bouncycastle.crypto.AsymmetricCipherKeyPair; import org.bouncycastle.crypto.generators.Ed25519KeyPairGenerator; import org.bouncycastle.crypto.params.Ed25519KeyGenerationParameters; import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters; import org.bouncycastle.crypto.util.PrivateKeyInfoFactory; import java.security.KeyFactory; import java.security.PrivateKey; import java.security.SecureRandom; import java.security.spec.PKCS8EncodedKeySpec; public class Ed25519PrivBcToJava1 { public static void main(String[] args) throws Exception { AsymmetricCipherKeyPair keyPair = generateEd25519KeyPair(); Ed25519PrivateKeyParameters privateKeyParameters = (Ed25519PrivateKeyParameters) keyPair.getPrivate(); PrivateKey privKey = convertToJavaPrivateKey(privateKeyParameters); } private static AsymmetricCipherKeyPair generateEd25519KeyPair() { SecureRandom rand = new SecureRandom(); Ed25519KeyGenerationParameters kpgParams = new Ed25519KeyGenerationParameters(rand); Ed25519KeyPairGenerator kpg = new Ed25519KeyPairGenerator(); kpg.init(kpgParams); return kpg.generateKeyPair(); } public static PrivateKey convertToJavaPrivateKey(Ed25519PrivateKeyParameters privateKeyParameters) throws Exception { PrivateKeyInfo privInfo = PrivateKeyInfoFactory.createPrivateKeyInfo(privateKeyParameters); byte[] pkcs8EncodedBytes = privInfo.getEncoded(); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(pkcs8EncodedBytes); // System.out.println(Base64.getEncoder().encodeToString(pkcs8EncodedBytes)); KeyFactory kf = KeyFactory.getInstance("Ed25519"); return kf.generatePrivate(pkcs8KeySpec); } } </code>
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.generators.Ed25519KeyPairGenerator;
import org.bouncycastle.crypto.params.Ed25519KeyGenerationParameters;
import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters;
import org.bouncycastle.crypto.util.PrivateKeyInfoFactory;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.spec.PKCS8EncodedKeySpec;

public class Ed25519PrivBcToJava1 {
    public static void main(String[] args) throws Exception {
        AsymmetricCipherKeyPair keyPair = generateEd25519KeyPair();
        Ed25519PrivateKeyParameters privateKeyParameters = (Ed25519PrivateKeyParameters) keyPair.getPrivate();
        PrivateKey privKey = convertToJavaPrivateKey(privateKeyParameters);
    }

    private static AsymmetricCipherKeyPair generateEd25519KeyPair() {
        SecureRandom rand = new SecureRandom();
        Ed25519KeyGenerationParameters kpgParams = new Ed25519KeyGenerationParameters(rand);
        Ed25519KeyPairGenerator kpg = new Ed25519KeyPairGenerator();
        kpg.init(kpgParams);
        return kpg.generateKeyPair();
    }

    public static PrivateKey convertToJavaPrivateKey(Ed25519PrivateKeyParameters privateKeyParameters) throws Exception {

        PrivateKeyInfo privInfo = PrivateKeyInfoFactory.createPrivateKeyInfo(privateKeyParameters);
        byte[] pkcs8EncodedBytes = privInfo.getEncoded();
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(pkcs8EncodedBytes);
//        System.out.println(Base64.getEncoder().encodeToString(pkcs8EncodedBytes));
        KeyFactory kf = KeyFactory.getInstance("Ed25519");
        return kf.generatePrivate(pkcs8KeySpec);
    }
}

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật