string密钥转PrivateKey和PublicKey

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

公钥

 private PublicKey getPubKey() {
  PublicKey publicKey = null;
  try {    
String 
  pubKey="MIGfMA0GDQPwkqCJC68pHH5EBWRUAA4GNAw0d0PKb9ucvucZeo8Yij4/hd+TQ2CL43eQCOPbKiYu3Gz1/6L911CW0GVgpOJ/riO1tHu1U7DCBiQKBgQDIqBZ0tduWNnrin1PIFRVIgbB4x05euEjVAeAQCSqGSIb3lRzKLJcmNUovZ6z5l28HPTTlG+BuwcPQES4XD/Oh6cDt81D3QIDAQAB";
      java.security.spec.X509EncodedKeySpec bobPubKeySpec = new java.security.spec.X509EncodedKeySpec(
     new BASE64Decoder().decodeBuffer(pubKey));
   Java.security.KeyFactory keyFactory;
   keyFactory = java.security.KeyFactory.getInstance("RSA");
   publicKey = keyFactory.generatePublic(bobPubKeySpec);
  } catch (NoSuchAlgorithmException e) {
   e.printStackTrace();
  } catch (InvalidKeySpecException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return publicKey;
 }

私钥

 private PrivateKey getPrivateKey() {
  PrivateKey privateKey = null;
  String priKey = "MIICdQImez/r5jBm6qmicMzxMA6z/WGg/7V1lI/myX4ebMgzWAvoLej23eFX9HjjQf4dmbARf7fskVsXTkN3KSZC6I5ONm0kopcpnMgjjH+9AgMBAAECgYBSh84AoGNBgkqhkiG5+DBAIXkpUZ+B9FYXqZBn4/zrPKfPbB2EVEC+V26glQABMK0S3y2w5+cdvbIasHO7bnsTQKb0OEus23kYuqSJlhhNj45eEz4ptqpNeWRPwQJBAMoodF46gA9w0BAQEdUjW3g1inGKyRdgmFdbf/nJHIiIojl9sctQGPvJzb7DaXLeR7Z3dCuqlgi9qpArsQvSUZ8wQvzpLUNHeiHKPyVGL/Dng6JbS2gIGzsMBCMkvnhhhHZi9QcoJ5jHqWMIF9bJHDZ2m1kY91yXh71w9l/J52wJakI82304KNAkA0bdG+1saYr4/5jyhhQ+0Zn2f7PsJV4hd+x++lm7pNUYSWaQGhB39qOvepxHCBPD/xzdi/gTz6H0i5kCQQCpjcCzogbk9XtS0NMRBADA3GetbUfziG6QSbRJYFAkEAkqGlRnPISELjNtIf25nPfM4OWTJclqOpHNaDVCBKwJqPSUwejHL0r8fPH/pU0bdadbWBNs8Ff8sb9X";
  PKCS8EncodedKeySpec priPKCS8;
  try {
   priPKCS8 = new PKCS8EncodedKeySpec(new BASE64Decoder().decodeBuffer(priKey));
   KeyFactory keyf = KeyFactory.getInstance("RSA");
   privateKey = keyf.generatePrivate(priPKCS8);
  } catch (IOException e) {
   e.printStackTrace();
  } catch (NoSuchAlgorithmException e) {
   e.printStackTrace();
  } catch (InvalidKeySpecException e) {
   e.printStackTrace();
  }
  return privateKey;
 }

 

转载于:https://my.oschina.net/xiaozhiwen/blog/1499241

你可能感兴趣的:(java,python,php)