精华内容
下载资源
问答
  • java RSA生成公钥对象和私钥对象

    千次阅读 2017-04-20 14:42:21
    1、字符串生成公钥对象 PublicKey /**  * 实例化公钥  *   * @return  */  private PublicKey getPubKey() {  PublicKey publicKey = null;  try {  // 自己的公钥(测试)  String pubKey ="MIGfMA0...

    1、字符串生成公钥对象 PublicKey

    /**
      * 实例化公钥
      * 
      * @return
      */
     private PublicKey getPubKey() {
      PublicKey publicKey = null;
      try {

       // 自己的公钥(测试)
        String pubKey ="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCVRiDkEKXy/KBTe+UmkA+feq1zGWIgBxkgbz7aBJGb5+eMKKoiDRoEHzlGndwFKm4mQWNftuMOfNcogzYpGKSEfC7sqfBPDHsGPZixMWzL3J10zkMTWo6MDIXKKqMG1Pgeq1wENfJjcYSU/enYSZkg3rFTOaBSFId+rrPjPo7Y4wIDAQAB";
          java.security.spec.X509EncodedKeySpec bobPubKeySpec = new java.security.spec.X509EncodedKeySpec(
         new BASE64Decoder().decodeBuffer(pubKey));
       // RSA对称加密算法
       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;
     }

     

     

    2、字符串生成私钥对象

     /**
      * 实例化私钥
      * 
      * @return
      */
     private PrivateKey getPrivateKey() {
      PrivateKey privateKey = null;
      String priKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJVGIOQQpfL8oFN75SaQD596rXMZYiAHGSBvPtoEkZvn54woqiINGgQfOUad3AUqbiZBY1+24w581yiDNikYpIR8Luyp8E8MewY9mLExbMvcnXTOQxNajowMhcoqowbU+B6rXAQ18mNxhJT96dhJmSDesVM5oFIUh36us+M+jtjjAgMBAAECgYABtnxKIabF0wBD9Pf8KUsEmXPEDlaB55LyPFSMS+Ef2NlfUlgha+UQhwsxND6CEKqS5c0uG/se/2+4l0jXz+CTYBEh+USYB3gxcMKEo5XDFOGaM2Ncbc7FAKJIkYYN2DHmr4voSM5YkVibw5Lerw0kKdYyr0Xd0kmqTok3JLiLgQJBAOGZ1ao9oqWUzCKnpuTmXre8pZLmpWPhm6S1FU0vHjI0pZh/jusc8UXSRPnx1gLsgXq0ux30j968x/DmkESwxX8CQQCpY1+2p1aX2EzYO3UoTbBUTg7lCsopVNVf41xriek7XF1YyXOwEOSokp2SDQcRoKJ2PyPc2FJ/f54pigdsW0adAkAM8JTnydc9ZhZ7WmBhOrFuGnzoux/7ZaJWxSguoCg8OvbQk2hwJd3U4mWgbHWY/1XB4wHkivWBkhRpxd+6gOUjAkBH9qscS52zZzbGiwQsOk1Wk88qKdpXku4QDeUe3vmSuZwC85tNyu+KWrfM6/H74DYFbK/MzK7H8iz80uJye5jVAkAEqEB/LwlpXljFAxTID/SLZBb+bCIoV/kvg+2145F+CSSUjEWRhG/+OH0cQfqomfg36WrvHl0g/Xw06fg31HgK";
      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;
     }

    展开全文
  • I'm writing a java program to import private keys from files within the file system and make a private key object, using java...I could do it for files in .pem format but, with .der format, I had no i...

    I'm writing a java program to import private keys from files within the file system and make a private key object, using java...

    I could do it for files in .pem format but, with .der format, I had no idea what to do, since I couldnt firstly detect the algorithm used to generate the keys.

    within .pem files I could determine the algorithm from the header for PKCS#1 which have a header like

    -----BEGIN RSA PRIVATE KEY----

    formats and used the bouncycastle pem reader for those in PKCS#8 which have a header

    -----BEGIN PRIVATE KEY-----

    but with those in .der format no idea :(

    also if anyone have an idea about .key format tell me

    thanx

    解决方案

    If your DER files are in PKCS#8 format, you can use the Java KeyFactory and do something like this:

    // Read file to a byte array.

    String privateKeyFileName = "C:\\myPrivateKey.der";

    Path path = Paths.get(privateKeyFileName);

    byte[] privKeyByteArray = Files.readAllBytes(path);

    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privKeyByteArray);

    KeyFactory keyFactory = KeyFactory.getInstance("RSA");

    PrivateKey myPrivKey = keyFactory.generatePrivate(keySpec);

    System.out.println("Algorithm: " + myPrivKey.getAlgorithm());

    You mentioned that you may not know what algorithm the key is using. I'm sure there is a more elegant solution than this, but you could create several KeyFactory objects (one for each possible algorithm) and try to generatePrivate() on each one until you do not get an InvalidKeySpecException.

    展开全文
  • -(SecKeyRef)getPrivateKeyRef{ Byte byte[]={0xf3,0xfc,0xcc,0x0d,0x00,0xd8,0x03,0x19,0x54,0xf9,0x08,0x64,0xd4,0x3c,0x24,0x7f,0x4b,0xf5,0xf0,0x66,0x5c,0x6b,0x50,0xcc,0x17,0x74,0x9a,0x27,0xd1,0xcf,0x...
  • How to get the related public-key object java.security.PublicKey out of a private-key Object java.security.PrivateKey in the RSA cryptosystem.解决方案Java is able to create a public key by using the ...

    How to get the related public-key object java.security.PublicKey out of a private-key Object java.security.PrivateKey in the RSA cryptosystem.

    解决方案

    Java is able to create a public key by using the modulus and exponent:

    RSAPublicKeySpec keySpec = new RSAPublicKeySpec(modulus, exponent);

    kf.generatePublic(keySpec);

    So we need to extract these values out of the private key:

    KeyFactory kf = KeyFactory.getInstance("RSA");

    RSAPrivateKeySpec priv = kf.getKeySpec(privateKey, RSAPrivateKeySpec.class);

    The RSAPrivateKeySpec-Object now contains the modulus we need, but the exponent is not the one we need for the public key.

    For the public key the exponent is commonly at 65537:

    http://en.wikipedia.org/wiki/65537_(number)

    Therefore we can now create the public key:

    KeyFactory kf = KeyFactory.getInstance("RSA");

    RSAPrivateKeySpec priv = kf.getKeySpec(privateKey, RSAPrivateKeySpec.class);

    RSAPublicKeySpec keySpec = new RSAPublicKeySpec(priv.getModulus(), BigInteger.valueOf(65537));

    PublicKey publicKey = kf.generatePublic(keySpec);

    展开全文
  • 获取私钥key对象

    2020-09-22 22:06:21
    } /** * 生成密钥对并保存在本地文件中 * * @param algorithm : 算法 * @param pubPath : 公钥保存路径 * @param priPath : 私钥保存路径 * @throws Exception */ private static void generateKeyToFile(String ...
    import com.sun.org.apache.xml.internal.security.utils.Base64;
    import org.apache.commons.io.FileUtils;
    
    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
    import java.io.File;
    import java.nio.charset.Charset;
    import java.security.*;
    import java.security.spec.PKCS8EncodedKeySpec;
    
    public class RSAdemo {
        public static void main(String[] args) throws Exception {
            String input = "硅谷";
            // 加密算法
            String algorithm = "RSA";
            PrivateKey privateKey = getPrivateKey("a.pri", algorithm);
    
    
    
        }
    
        public static PrivateKey getPrivateKey(String priPath,String algorithm) throws Exception{
            // 将文件内容转为字符串
            String privateKeyString = FileUtils.readFileToString(new File(priPath), Charset.defaultCharset());
            // 获取密钥工厂
            KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
            // 构建密钥规范 进行Base64解码
            PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(Base64.decode(privateKeyString));
            // 生成私钥
            return keyFactory.generatePrivate(spec);
        }
    
        /**
         * 生成密钥对并保存在本地文件中
         *
         * @param algorithm : 算法
         * @param pubPath   : 公钥保存路径
         * @param priPath   : 私钥保存路径
         * @throws Exception
         */
        private static void generateKeyToFile(String algorithm, String pubPath, String priPath) throws Exception {
            // 获取密钥对生成器
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);
            // 获取密钥对
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            // 获取公钥
            PublicKey publicKey = keyPair.getPublic();
            // 获取私钥
            PrivateKey privateKey = keyPair.getPrivate();
            // 获取byte数组
            byte[] publicKeyEncoded = publicKey.getEncoded();
            byte[] privateKeyEncoded = privateKey.getEncoded();
            // 进行Base64编码
            String publicKeyString = Base64.encode(publicKeyEncoded);
            String privateKeyString = Base64.encode(privateKeyEncoded);
            // 保存文件
            FileUtils.writeStringToFile(new File(pubPath), publicKeyString, Charset.forName("UTF-8"));
            FileUtils.writeStringToFile(new File(priPath), privateKeyString, Charset.forName("UTF-8"));
    
        }
    
        /**
         * 解密数据
         *
         * @param algorithm      : 算法
         * @param encrypted      : 密文
         * @param key            : 密钥
         * @return : 原文
         * @throws Exception
         */
        public static String decryptRSA(String algorithm,Key key,String encrypted) throws Exception{
             // 创建加密对象
            // 参数表示加密算法
            Cipher cipher = Cipher.getInstance(algorithm);
            // 私钥进行解密
            cipher.init(Cipher.DECRYPT_MODE,key);
            // 由于密文进行了Base64编码, 在这里需要进行解码
            byte[] decode = Base64.decode(encrypted);
            // 对密文进行解密,不需要使用base64,因为原文不会乱码
            byte[] bytes1 = cipher.doFinal(decode);
            System.out.println(new String(bytes1));
            return new String(bytes1);
    
        }
        /**
         * 使用密钥加密数据
         *
         * @param algorithm      : 算法
         * @param input          : 原文
         * @param key            : 密钥
         * @return : 密文
         * @throws Exception
         */
        public static String encryptRSA(String algorithm,Key key,String input) throws Exception{
            // 创建加密对象
            // 参数表示加密算法
            Cipher cipher = Cipher.getInstance(algorithm);
            // 初始化加密
            // 第一个参数:加密的模式
            // 第二个参数:使用私钥进行加密
            cipher.init(Cipher.ENCRYPT_MODE,key);
            // 私钥加密
            byte[] bytes = cipher.doFinal(input.getBytes());
            // 对密文进行Base64编码
            System.out.println(Base64.encode(bytes));
            return Base64.encode(bytes);
        }
    }

     

    展开全文
  • 该类具体功能:根据pfx证书得到私钥、根据私钥字节数组获取私钥对象、根据公钥字节数组获取公钥、根据pfx证书获取证书对象,根据私钥、公钥证书、密码生成pkcs12,根据私钥、公钥证书、密钥,合成为pfx文件,依赖...
  • Rsa读取pem公钥和私钥

    千次阅读 2019-06-26 11:16:57
    1.把pem文件用text打开,把开头和结尾的begin和...3.用以下代码生成公钥和私钥对象; public static PublicKey string2PublicKey(String pubStr) throws Exception{ byte[] keyBytes = base642Byte(pubStr); X5...
  • 对象加密算法-rsa加解密 公钥加密私钥解密
  • 介绍在Java语言中三种创建RSA私钥对象的方法,以及实现RSA私钥从C++到Java的数据格式转化。
  • liunx命令: sftp -oIdentityFile=私钥路径 -oPort=端口用户名@地址 /** * 获取sftp协议连接. * @param filepath 私钥存放位置 * @param host 主机名 ... * @return 连接对象 * @throws JSch.
  • PKCS8私钥转PKCS1私钥

    2021-07-30 09:21:42
    PKCS8私钥转PKCS1私钥简介jar坐标代码 简介 企业微信获取到的加密内容为企业微信产生。RSA使用PKCS1,而不是PKCS8,但是填充的公钥要求是PKCS8,这是企业微信巨坑的地方。找到此问题后,就需要单独转换私钥key了。...
  • 区块链私钥公钥

    千次阅读 2020-06-10 15:51:00
    私钥:51位,以数字“5”开头,是不支持压缩格式的私钥; 52位,以“K”或“L”开头,是支持压缩格式的私钥; 5开头的私钥都是不支持压缩格式的私钥,而L和K开头的都是支持压缩格式的私钥。 公钥:33位,...
  • itext私钥签名

    2019-06-13 18:38:12
    使用私钥签章很简单,网上百度,一大把,不多说,直接上代码; 前提,你需要一个PKCS12,带签章PDF和需要盖章的图片,如果没有,使用私钥和证书也可以,看自己喜好,修改代码 ps:本人使用的是itext5.5.13。 ...
  • 公钥与私钥

    2017-05-16 15:54:09
    公钥与私钥 数字签名是什么? 原文网址:http://www.youdzone.com/signature.html 1. 鲍勃有两把钥匙,一把是公钥,另一把是私钥。 2. 鲍勃把公钥送给他的朋友们----帕蒂、道格、苏珊----...
  • RSA私钥加密研究

    2017-06-21 16:57:37
    RSA私钥加密研究 朋友碰到调用第三方API的加密问题,JAVA代码中用pfx私钥文件来加密字符串,流程如下: 输入私钥文件地址pfxPath、私钥密码pfxKey、被加密串dataContent dataContent转成base64串,使用sun....
  • 测试: public static void main(String[] ... //生成公钥私钥 Map<String, Object> map = RSAUtil.initKey(2048); String PrivateKey = RSAUtil.getPrivateKey(map); String PublicKey = RSAUtil.getPubli
  • 我喜欢2这个数字,就保留起来,不告诉你们(私钥),然后我告诉大家,1是我的公钥。 我有一个文件,不能让别人看,我就用1加密了。别人找到了这个文件,但是他不知道2就是解密的私钥啊,所以他解不开,只有我可以用 ...
  • go生成私钥公钥

    2020-12-02 16:41:57
    openssl方式生成私钥公钥 openssl genrsa -out rsa_private_key.pem 1024 openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem Go代码方式生成私钥公钥 package main import ( "crypto/...
  • 1、字符串生成公钥对象 PublicKey /**  * 实例化公钥  *  * @return  */  private PublicKey getPubKey() {  PublicKey publicKey = null;  try {  // 自己的公钥(测试)  String pubKey =...
  • RAS公钥私钥

    2020-09-03 21:58:48
    } //获得私钥 public static String getPrivateKey(Map, Object> keyMap) throws Exception { //获得map中的私钥对象 转为key对象 Key key = (Key) keyMap.get(PRIVATE_KEY); //byte[] privateKey = key.getEncoded...
  • RSA生成公钥私钥公钥加密私钥解密 一、RSA加密简介 RSA加密是一种非对称加密。可以在不直接传递密钥的情况下,完成解密。这能够确保信息的安全性,避免了直接传递密钥所造成的被破解的风险。是由一对密钥来进行加...
  • rsa私钥和公钥RSA is an algorithm used for Cryptography. It was patented until 2000 in the USA (not the whole world) where now it can be used freely. RSA has a lot of usage examples but it is mainly ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 34,024
精华内容 13,609
关键字:

私钥对象