Loading private key from string or resource in Java JSch in Android app

一曲冷凌霜 提交于 2019-12-01 06:40:58
String private_key = "xyz123abc456def789ghi012..."; // private key in OpenSSH format as a plain string
String public_key = "a1b2c3d4e5..."; // public key in OpenSSH format as a plain string

The JSch.addIdentity (and ultimately KeyPair.load) takes a buffer that contains a contents of key pair files as generated by ssh-keygen (with -m pem in recent versions of OpenSSH).

The formats are like this:

-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAvc04a8wViYV5Kb4jX+MxEqN1vi9q9C7mPhf6DV+mb1ADNAiR
YeLqPMLCYUF2ViobcGfarb51gz7iB2TgkDmhQNK9XDCOUaGYN/FeZcN0JpzkjEjZ
ApbRfshj1h9qKQUW+38XKnltMtf4dxiuxkXph8P6IMVveTDs3sSbBPq560bdJ1AD
...
PEyVxlat2I4ShuLQiO1QIuS8ABu5yDM2EouB6vlxtGEBpIJItp7cyA==
-----END RSA PRIVATE KEY-----
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC9zThrzBWJhXkpviNf4zESo3W+L2r0LuY+F/oNX6ZvUAM0CJFh4uo8wsJhQXZWKhtwZ9qtvnWDPuIHZOCQOaFA0r1cMI5RoZg38V5lw3QmnOSMSNkCltF+yGPWH2opBRb7fxcqeW0y1/h3GK7GRemHw/ogxW95MOzexJsE+rnrRt0nUAOu4hHjL6G/nlvdJ1jjZ06NwhYkbAxRoJkHUJTtMT2IL5ZmdAf37KHSPqZS32pLxQDmPutZxpIwlhz4aR78ZGp4+57mR069Y4at09GF0UmgtIiLjlKUexMf5sueVQ8LKhME6vOupMzTbiFEB3UJNq8d9Yx5i+c/IRHUIcI1 marti@MartinuvOmen

This is not the format you have in private_key and public_key.


See also JSch to add private key from a string.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!