久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

以 RSA OpenSSH 格式讀取 KeyPair 的公鑰?

Read KeyPair#39;s publickey in RSA OpenSSH format?(以 RSA OpenSSH 格式讀取 KeyPair 的公鑰?)
本文介紹了以 RSA OpenSSH 格式讀取 KeyPair 的公鑰?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我通過執(zhí)行以下操作在 Java 中創(chuàng)建了一個 KeyPair:

I’ve created a KeyPair in Java by doing the following:

KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024, new SecureRandom());
KeyPair keyPair = keyPairGenerator.generateKeyPair();

如何從 keyPair 獲取以 -----BEGIN" 開頭的 RSA OpenSSH 格式的 publicKey?

How do I get the publicKey from keyPair in the RSA OpenSSH format that begins with "-----BEGIN"?

推薦答案

這是一個我沒有測試過的快速 hack.這需要 Java 6 或更高版本.有關詳細信息,請參閱以下 RFC:

Here is a quick hack which I haven't tested. This requires Java 6 or greater. For more information see the following RFCs:

RFC 4716
RFC 4253
RFC 4251

import java.io.*;
import java.math.BigInteger;
import java.nio.*;
import java.nio.charset.Charset;
import java.security.*;
import java.security.interfaces.RSAPublicKey;

import javax.xml.bind.DatatypeConverter;

public class SecshPublicKey {
    
    /*
     * Taken from RFC 4716, with reference to RFCs 4253 and 4251.
     */
    public static void main(String[] args) throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(1024, new SecureRandom());
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        
        ByteArrayOutputStream binaryOS = new ByteArrayOutputStream();
        writeSshString(binaryOS, "ssh-rsa");
        RSAPublicKey rsaPub = (RSAPublicKey)keyPair.getPublic();
        writeSshMPInt(binaryOS, rsaPub.getPublicExponent());
        writeSshMPInt(binaryOS, rsaPub.getModulus());
        
        // Now base64-encode the result.
        
        String b64Encoded = sshBase64Encode(binaryOS.toByteArray());
        
        // Now write out the result
        
        System.out.println("---- BEGIN SSH2 PUBLIC KEY ----");
        System.out.println(b64Encoded);
        System.out.println("---- END SSH2 PUBLIC KEY ----");
    }

    private static String sshBase64Encode(byte[] byteArray) {
        String b64_prelim = DatatypeConverter.printBase64Binary(byteArray);
        
        // Break into lines of at most 72 characters.
        
        StringBuilder b64_final = new StringBuilder(b64_prelim.length() * 2);
        
        while (b64_prelim.length() > 72) {
            b64_final.append(b64_prelim.substring(0, 72));
            b64_final.append("
");
            b64_prelim = b64_prelim.substring(72);
        }
        b64_final.append(b64_prelim);
        return b64_final.toString();
    }

    private static void writeSshMPInt(OutputStream os, BigInteger mpint) throws IOException {
        ByteBuffer lengthBuf = ByteBuffer.allocate(4);
        lengthBuf.order(ByteOrder.BIG_ENDIAN);
        byte [] x;
        if (mpint.equals(BigInteger.ZERO)) {
            x = new byte[0];
        } else {
            x = mpint.toByteArray();
        }
        lengthBuf.putInt(x.length);
        os.write(lengthBuf.array());
        os.write(x);
    }

    private static void writeSshString(OutputStream os, String s) throws IOException {
        ByteBuffer lengthBuf = ByteBuffer.allocate(4);
        lengthBuf.order(ByteOrder.BIG_ENDIAN);
        byte [] encoded = s.getBytes(Charset.forName("UTF-8"));
        lengthBuf.putInt(encoded.length);
        os.write(lengthBuf.array());
        os.write(encoded);
    }

}

這篇關于以 RSA OpenSSH 格式讀取 KeyPair 的公鑰?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 国产精品入口 | 成人免费在线视频 | 免费看大片bbbb欧美 | 亚洲精品久久久蜜桃 | 日本一区二区不卡 | 男人久久天堂 | 色综合一区二区 | 国产久 | 亚洲精品一 | 欧美第一页| 国产91综合 | 91av在线视频观看 | 精品视频一区二区 | 精品一区国产 | 久久伊人精品 | 国产精品久久久精品 | 亚洲大片一区 | 91精品中文字幕一区二区三区 | 国产免费一区二区三区网站免费 | 久久激情网 | jizz视频| 九九99靖品 | 亚洲一区二区三区四区在线观看 | 91社区在线观看播放 | 亚洲乱码国产乱码精品精的特点 | 免费高潮视频95在线观看网站 | 色悠悠久 | 国产精品久久久久久久久久久久 | 中文字幕av一区二区三区 | 日本三级线观看 视频 | 九九热这里只有精品在线观看 | 精品国产一区二区三区久久 | 男人亚洲天堂 | 欧美日韩精品影院 | 精品中文字幕一区二区 | 中文字幕av第一页 | 国产免费视频在线 | 伊人春色在线观看 | 亚洲欧美在线视频 | 国产精品乱码一二三区的特点 | 九色 在线|