Java.security - 如何在String中存储KeyPair密钥。我得到无效的DER编码异常

问题描述 投票:0回答:1

生成密钥对,使用字节数组进行编码和解码可以正常工作。

我想将私钥和公钥都存储为字符串。这是为了实验目的。我想研究如何存储必须在使用前解码的密码。

我使用string.getBytes()和new String(bytes)将字节数组转换为String和vv。

当我尝试使用字符串存储和检索这些字节数组时,使用它们编码一个秘密文本,然后我得到这个异常:

线程“main”中的异常java.security.spec.InvalidKeySpecException:java.security.InvalidKeyException:IOException:ObjectIdentifier() - 无效的DER编码,未结束

感谢James K Polk的解决方案就是答案。

java security rsa
1个回答
3
投票

谢谢@James K Polk !!这真的帮助我完成了我的实验!当你发布答案时我会回答“V”和“+1”!

使用James K Polk的答案,我重写了实验例子:

import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class KeyPairToString {
    private static final String ALGORITHM = "RSA";

    private static byte[] encrypt(byte[] publicKey, byte[] inputData) throws Exception {
        PublicKey key = KeyFactory.getInstance(ALGORITHM)    /* ExceptionL Invalid DER encoding */
                .generatePublic(new X509EncodedKeySpec(publicKey));
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(inputData);
    }

    private static byte[] decrypt(byte[] privateKey, byte[] inputData) throws Exception {
        PrivateKey key = KeyFactory.getInstance(ALGORITHM)
                .generatePrivate(new PKCS8EncodedKeySpec(privateKey));
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
        return cipher.doFinal(inputData);
    }

    private static KeyPair generateKeyPair()
            throws NoSuchAlgorithmException, NoSuchProviderException {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
        keyGen.initialize(512, random);
        return keyGen.generateKeyPair();
    }

    private static String bytesToString(byte[] bytes) {
        return new String(bytes);
    }

    private static byte[] stringToBytes(String astring) {
        return astring.getBytes();
    }

    private static String bytesToEncodedString(byte[] bytes) {
        return Base64.getEncoder().encodeToString(bytes);
    }

    private static byte[] encodedStringToBytes(String encodedString) {
        return Base64.getDecoder().decode(encodedString);
    }

    public static void main(String[] args) throws Exception {
        KeyPair generateKeyPair = generateKeyPair();

        byte[] publicKey = generateKeyPair.getPublic().getEncoded();
        byte[] privateKey = generateKeyPair.getPrivate().getEncoded();

        // Byte array
        String secretText = "hi this is secret johan here";
        byte[] encryptedData = encrypt(publicKey, secretText.getBytes());
        byte[] decryptedData = decrypt(privateKey, encryptedData);
        System.out.println(new String(decryptedData));

        // Now with Strings
        String encodedPublicKeyString = bytesToEncodedString(publicKey);
        String encodedPrivateKeyString = bytesToEncodedString(privateKey);
        String encryptedDataString = bytesToEncodedString(
                encrypt(encodedStringToBytes(encodedPublicKeyString), stringToBytes(secretText)));
        String decryptedDataString = bytesToString(
                decrypt(
                        encodedStringToBytes(encodedPrivateKeyString),
                        encodedStringToBytes(encryptedDataString)));
        System.out.println(new String(decryptedDataString));
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.