Converting a byte [] to PrivateKey in java for digital signature

两盒软妹~` 提交于 2019-12-03 21:43:59

I don't know how you persisted the private key into the DB. But this page provides some information on how to load a KeyStore from a file system, and retrieve the Private and Public keys.

Relevant code snippet (modified to suit your requirement) is,

   String password = ...;
   KeyStore ks = KeyStore.getInstance(KEY_STORE_TYPE);

   byte[] keyAsByteArray = ...; // The key persisted in the DB
   InputStream keyStream = new ByteArrayInputStream(keyAsByteArray);
   ks.load(keyStream, password);

and then,

   KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry) ks.getEntry(PRIVATE_KEY_ALIAS, password);  
   PrivateKey privateKey = pkEntry.getPrivateKey();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!