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));
  • 相关阅读:
    模块的种类和导入方法
    小知识点补充
    9.17模拟赛2.0
    hdu2181 哈密顿绕行世界问题
    9.17模拟赛
    9.15模拟赛
    P1084 疫情控制
    9.14模拟赛
    【bzoj1232】[Usaco2008Nov]安慰奶牛cheer
    P3128 [USACO15DEC]最大流Max Flow
  • 原文地址:https://www.cnblogs.com/frankyou/p/8335063.html
Copyright © 2011-2022 走看看