在java中使用openssl创建密钥

发布于 2024-09-19 10:56:33 字数 266 浏览 3 评论 0原文

我需要在java代码中使用openssl。例如

$ openssl genrsa -out private.pem 2048

$ openssl pkcs8 -topk8 -in private.pem -outform DER -out private.der -nocrypt

$ openssl rsa -in private.pem -pubout -outform DER -out public.der

有没有任何库或方法来实现这个?

I need to use openssl in java code. e.g.

$ openssl genrsa -out private.pem 2048

$ openssl pkcs8 -topk8 -in private.pem -outform DER -out private.der -nocrypt

$ openssl rsa -in private.pem -pubout -outform DER -out public.der

Is there any library or method to implement this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音盲 2024-09-26 10:56:33

最好的方法是使用 Java 库来执行此操作。我现在无法编写确切的代码,但这并不难。看看java.security.KeyPairGenerator等等。这将是理解密码学的良好经验。

但如果您只需要调用这三个命令行,则 Process.waitFor() 调用就是答案。你可以使用这个类。

package ru.donz.util.javatools;

import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User: Donz
 * Date: 25.05.2010
 * Time: 21:57:52
 * Start process, read all its streams and write them to pointed streams.
 */
public class ConsoleProcessExecutor
{
    /**
     * Start process, redirect its streams to pointed streams and return only after finishing of this process
     *
     * @param args        process arguments including executable file
     * @param runtime just Runtime object for process
     * @param workDir working dir
     * @param out         stream for redirecting System.out of process
     * @param err         stream for redirecting System.err of process
     * @throws IOException
     * @throws InterruptedException
     */
    public static void execute( String[] args, Runtime runtime, File workDir, OutputStream out, OutputStream err )
            throws IOException, InterruptedException
    {
        Process process = runtime.exec( args, null, workDir );

        new Thread( new StreamReader( process.getInputStream(), out ) ).start();
        new Thread( new StreamReader( process.getErrorStream(), err ) ).start();

        int rc = process.waitFor();
        if( rc != 0 )
        {
            StringBuilder argSB = new StringBuilder( );
            for( String arg : args )
            {
                argSB.append( arg ).append( ' ' );
            }
            throw new RuntimeException( "Process execution failed. Return code: " + rc + "\ncommand: " + argSB );
        }
    }

}

class StreamReader implements Runnable
{
    private final InputStream in;
    private final OutputStream out;


    public StreamReader( InputStream in, OutputStream out )
    {
        this.in = in;
        this.out = out;
    }

    @Override
    public void run()
    {
        int c;
        try
        {
            while( ( c = in.read() ) != -1 )
            {
                out.write( c );
            }
            out.flush();
        }
        catch( IOException e )
        {
            e.printStackTrace();
        }
    }
}

The best way is to use Java library for this actions. I can't write exact code right now but it isn't very hard. Look at java.security.KeyPairGenerator and so on. And it will be good experience in understanding of cryptography.

But if you need only to call this three command line, Process.waitFor() call is the answer. You can use this class.

package ru.donz.util.javatools;

import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User: Donz
 * Date: 25.05.2010
 * Time: 21:57:52
 * Start process, read all its streams and write them to pointed streams.
 */
public class ConsoleProcessExecutor
{
    /**
     * Start process, redirect its streams to pointed streams and return only after finishing of this process
     *
     * @param args        process arguments including executable file
     * @param runtime just Runtime object for process
     * @param workDir working dir
     * @param out         stream for redirecting System.out of process
     * @param err         stream for redirecting System.err of process
     * @throws IOException
     * @throws InterruptedException
     */
    public static void execute( String[] args, Runtime runtime, File workDir, OutputStream out, OutputStream err )
            throws IOException, InterruptedException
    {
        Process process = runtime.exec( args, null, workDir );

        new Thread( new StreamReader( process.getInputStream(), out ) ).start();
        new Thread( new StreamReader( process.getErrorStream(), err ) ).start();

        int rc = process.waitFor();
        if( rc != 0 )
        {
            StringBuilder argSB = new StringBuilder( );
            for( String arg : args )
            {
                argSB.append( arg ).append( ' ' );
            }
            throw new RuntimeException( "Process execution failed. Return code: " + rc + "\ncommand: " + argSB );
        }
    }

}

class StreamReader implements Runnable
{
    private final InputStream in;
    private final OutputStream out;


    public StreamReader( InputStream in, OutputStream out )
    {
        this.in = in;
        this.out = out;
    }

    @Override
    public void run()
    {
        int c;
        try
        {
            while( ( c = in.read() ) != -1 )
            {
                out.write( c );
            }
            out.flush();
        }
        catch( IOException e )
        {
            e.printStackTrace();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文