zoukankan      html  css  js  c++  java
  • Porting .Net RSA xml keys to Java

    byte[] expBytes = Base64.decodeBase64(exponentElem.getText().trim()));
    byte[] modBytes = Base64.decodeBase64(modulusElem.getText().trim());
    byte[] dBytes = Base64.decodeBase64(dElem.getText().trim());
    
    BigInteger modules = new BigInteger(1, modBytes);
    BigInteger exponent = new BigInteger(1, expBytes);
    BigInteger d = new BigInteger(1, dBytes);
    
    KeyFactory factory = KeyFactory.getInstance("RSA");
    Cipher cipher = Cipher.getInstance("RSA");
    String input = "test";
    
    RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(modules, exponent);
    PublicKey pubKey = factory.generatePublic(pubSpec);
    cipher.init(Cipher.ENCRYPT_MODE, pubKey);
    byte[] encrypted = cipher.doFinal(input.getBytes("UTF-8"));
    System.out.println("encrypted: " + new String(encrypted));
    
    RSAPrivateKeySpec privSpec = new RSAPrivateKeySpec(modules, d);
    PrivateKey privKey = factory.generatePrivate(privSpec);
    cipher.init(Cipher.DECRYPT_MODE, privKey);
    byte[] decrypted = cipher.doFinal(encrypted);
    System.out.println("decrypted: " + new String(decrypted));
  • 相关阅读:
    Kubernetes之network: failed to set bridge addr: "cni0" already has an IP address different from xxx问题
    k8s的存储Volume
    系统漏洞扫描与分析软件
    linux图形化安装oracle
    JMX监控tomcat jdbc pool
    Hyper-V
    苹果手机
    读书
    clickhouse count
    clickhouse分布式表
  • 原文地址:https://www.cnblogs.com/frankyou/p/8335063.html
Copyright © 2011-2022 走看看