将 .Net RSA xml 密钥移植到 Java

编程入门 行业动态 更新时间:2024-10-24 09:29:30
本文介绍了将 .Net RSA xml 密钥移植到 Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有来自 .Net 系统的 xml 格式的私钥和公钥.我必须使用此密钥在 Java 中执行加密/解密.有什么办法吗?

I have private and public keys from the .Net system in the xml format. I have to use this keys to perform encryption/decryption in Java. Is there any way to do it?

公钥看起来像这样:

<RSAKeyValue> <Modulus>jHIxcGzzpByFv...pvhxFnP0ssmlBfMALis</Modulus> <Exponent>AQAB</Exponent> </RSAKeyValue>

私钥:

<RSAKeyValue> <Modulus>4hjg1ibWXHIlH...ssmlBfMAListzrgk=</Modulus> <Exponent>AQAB</Exponent> <P>8QZCtrmJcr9uW7VRex+diH...jLHV5StmuBs1+vZZAQ==</P> <Q>8CUvJTv...yeDszMWNCQ==</Q> <DP>elh2Nv...cygE3657AQ==</DP> <DQ>MBUh5XC...+PfiMfX0EQ==</DQ> <InverseQ>oxvsj4WCbQ....LyjggXg==</InverseQ> <D>KrhmqzAVasx...uxQ5VGZmZ6yOAE=</D> </RSAKeyValue>

我写了一些代码来加密数据,但我不确定它是否正确.

I have written a bit of code to encrypt data but I am not sure if its correct.

Element modulusElem = root.getChild("Modulus"); Element exponentElem = root.getChild("Exponent"); byte[] expBytes = decoder.decodeBuffer(exponentElem.getText().trim()); byte[] modBytes = decoder.decodeBuffer(modulusElem.getText().trim()); RSAPublicKeySpec keySpec = new RSAPublicKeySpec(new BigInteger(1, modBytes), new BigInteger(1, expBytes)); KeyFactory fact = KeyFactory.getInstance("RSA"); PublicKey pubKey = fact.generatePublic(keySpec);

如何从xml中生成私钥来解密数据?

How can I make a private key from the xml to decrypt the data?

推荐答案

在您的示例中是 decoder 进行 Base64 解码吗?看起来您可能依赖 sun.misc.BASE64Decoder 并且依赖这些内部类通常不是一个好主意(例如其他 JVM 不会有它).您可以使用 Apache Commons Codec ,它有一个 Base64 类来解码.以下是 RSA 加密和解密所需的其余内容:

Is that decoder in your example doing the Base64 decoding? It looks like you might be relying on sun.misc.BASE64Decoder and it's generally not a good idea to depend on those internal classes (other JVM's won't have it for instance). You could use Apache Commons Codec that has a Base64 class to decode with. Here's the rest of what you need though for RSA encryption and decryption:

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));

更多推荐

将 .Net RSA xml 密钥移植到 Java

本文发布于:2023-11-15 19:05:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1597159.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:密钥   RSA   Net   Java   xml

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!