Converting Strings to encryption keys and vice versa java

前端 未结 1 946
情歌与酒
情歌与酒 2020-12-02 15:50

I\'m currently working on a way to convert keys into strings and vice versa. It works for the public key conversions, and converts a private key into a String. For some reas

相关标签:
1条回答
  • 2020-12-02 16:25

    Public keys are stored using a X509EncodedKeySpec as you have, but Private keys use the PKCS8EncodedKeySpec. For example like this:

    public static PrivateKey loadPrivateKey(String key64) throws GeneralSecurityException {
        byte[] clear = base64Decode(key64);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(clear);
        KeyFactory fact = KeyFactory.getInstance("DSA");
        PrivateKey priv = fact.generatePrivate(keySpec);
        Arrays.fill(clear, (byte) 0);
        return priv;
    }
    
    
    public static PublicKey loadPublicKey(String stored) throws GeneralSecurityException {
        byte[] data = base64Decode(stored);
        X509EncodedKeySpec spec = new X509EncodedKeySpec(data);
        KeyFactory fact = KeyFactory.getInstance("DSA");
        return fact.generatePublic(spec);
    }
    
    public static String savePrivateKey(PrivateKey priv) throws GeneralSecurityException {
        KeyFactory fact = KeyFactory.getInstance("DSA");
        PKCS8EncodedKeySpec spec = fact.getKeySpec(priv,
                PKCS8EncodedKeySpec.class);
        byte[] packed = spec.getEncoded();
        String key64 = base64Encode(packed);
    
        Arrays.fill(packed, (byte) 0);
        return key64;
    }
    
    
    public static String savePublicKey(PublicKey publ) throws GeneralSecurityException {
        KeyFactory fact = KeyFactory.getInstance("DSA");
        X509EncodedKeySpec spec = fact.getKeySpec(publ,
                X509EncodedKeySpec.class);
        return base64Encode(spec.getEncoded());
    }
    
    
    public static void main(String[] args) throws Exception {
        KeyPairGenerator gen = KeyPairGenerator.getInstance("DSA");
        KeyPair pair = gen.generateKeyPair();
    
        String pubKey = savePublicKey(pair.getPublic());
        PublicKey pubSaved = loadPublicKey(pubKey);
        System.out.println(pair.getPublic()+"\n"+pubSaved);
    
        String privKey = savePrivateKey(pair.getPrivate());
        PrivateKey privSaved = loadPrivateKey(privKey);
        System.out.println(pair.getPrivate()+"\n"+privSaved);
    }
    
    0 讨论(0)
提交回复
热议问题