C# RSA和Java RSA互通
今天调查了C# RSA和Java RSA,网上很多人说,C#加密或者java加密 ,Java不能解密或者C#不能解密
但是我尝试了一下,发现是可以的,下面就是我尝试的代码,如果您有什么问题,我想看看,他们为什么不能互通?
- package rsa;
- import java.math.BigInteger;
- import java.security.KeyFactory;
- import java.security.PrivateKey;
- import java.security.PublicKey;
- import java.security.spec.RSAPrivateKeySpec;
- import java.security.spec.RSAPublicKeySpec;
- import javax.crypto.Cipher;
- import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
- import com.sun.org.apache.xml.internal.security.utils.Base64;
- /**
- * @author cnchenhl
- * Jul 8, 2011
- */
- public class RSAMain {
- private static String module = "5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=";
- private static String exponentString = "AQAB";
- private static String delement = "vmaYHEbPAgOJvaEXQl+t8DQKFT1fudEysTy31LTyXjGu6XiltXXHUuZaa2IPyHgBz0Nd7znwsW/S44iql0Fen1kzKioEL3svANui63O3o5xdDeExVM6zOf1wUUh/oldovPweChyoAdMtUzgvCbJk1sYDJf++Nr0FeNW1RB1XG30=";
- private static String encryptString = "Vx/dGjS1YWKRubsoDgiShiwLgqyNE2z/eM65U7HZx+RogwaiZimNBxjuOS6acEhKZx66cMYEAd1fc6oewbEvDIfP44GaN9dCjKE/BkkQlwEg6aTO5q+yqy+nEGe1kvLY9EyXS/Kv1LDh3e/2xAk5FNj8Zp6oU2kq4ewL8kK/ai4=";
- /**
- * @param args
- */
- public static void main(String[] args) {
- byte[] en = encrypt();
- System.out.println(Base64.encode(en));
- byte[] enTest = null;
- try {
- enTest = Base64.decode(encryptString);
- } catch (Base64DecodingException e) {
- e.printStackTrace();
- }
- System.out.println(enTest.length);
- System.out.println(en.length);
- System.out.println(new String(Dencrypt(en)));
- System.out.println(new String(Dencrypt(enTest)));
- }
- public static byte[] encrypt() {
- try {
- byte[] modulusBytes = Base64.decode(module);
- byte[] exponentBytes = Base64.decode(exponentString);
- BigInteger modulus = new BigInteger(1, modulusBytes);
- BigInteger exponent = new BigInteger(1, exponentBytes);
- RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, exponent);
- KeyFactory fact = KeyFactory.getInstance("RSA");
- PublicKey pubKey = fact.generatePublic(rsaPubKey);
- Cipher cipher = Cipher.getInstance("RSA");
- cipher.init(Cipher.ENCRYPT_MODE, pubKey);
- byte[] cipherData = cipher.doFinal(new String("chenhailong").getBytes());
- return cipherData;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- public static byte[] Dencrypt(byte[] encrypted) {
- try {
- byte[] expBytes = Base64.decode(delement);
- byte[] modBytes = Base64.decode(module);
- BigInteger modules = new BigInteger(1, modBytes);
- BigInteger exponent = new BigInteger(1, expBytes);
- KeyFactory factory = KeyFactory.getInstance("RSA");
- Cipher cipher = Cipher.getInstance("RSA");
- RSAPrivateKeySpec privSpec = new RSAPrivateKeySpec(modules, exponent);
- PrivateKey privKey = factory.generatePrivate(privSpec);
- cipher.init(Cipher.DECRYPT_MODE, privKey);
- byte[] decrypted = cipher.doFinal(encrypted);
- return decrypted;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Security.Cryptography;
- namespace RSA
- {
- class Program
- {
- static void Main(string[] args)
- {
- string de = "iBILuPJFgPMxgpbgN3F2JjD6XjcqRSApjVVbvBBEBDV21Pjj7lTrfhEjSVnJX/MVoZrmX0lxsvoXTMvvVwVF7K7W5hs7Qo+aMN96yWke7wiLEM9M4pPz60A/KSckskiona67tXcqOLXb8N18TKaNCKHv0Ce+GyEKK5+MT7e1vao=";
- //string encrypt = RSAEncrypt("", "chenhailong");
- byte[] encrypt = RSAEncrypt("chenhailong");
- //string name = RSADecrypt(encrypt);
- string name = RSADecrypt(Convert.FromBase64String(de));
- Console.WriteLine(encrypt.Length);
- Console.WriteLine(Convert.ToBase64String(encrypt));
- Console.WriteLine(name);
- Console.ReadKey();
- }
- /// <summary>
- /// RSA encrypt
- /// </summary>
- /// <param name="publickey"></param>
- /// <param name="content"></param>
- /// <returns></returns>
- public static byte[] RSAEncrypt(string content)
- {
- string publickey = @"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
- RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
- byte[] cipherbytes;
- rsa.FromXmlString(publickey);
- cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);
- //return Convert.ToBase64String(cipherbytes);
- return cipherbytes;
- }
- /// <summary>
- /// RSA decrypt
- /// </summary>
- /// <param name="privatekey"></param>
- /// <param name="content"></param>
- /// <returns></returns>
- public static string RSADecrypt(byte[] content)
- {
- string privatekey = @"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent><P>/hf2dnK7rNfl3lbqghWcpFdu778hUpIEBixCDL5WiBtpkZdpSw90aERmHJYaW2RGvGRi6zSftLh00KHsPcNUMw==</P><Q>6Cn/jOLrPapDTEp1Fkq+uz++1Do0eeX7HYqi9rY29CqShzCeI7LEYOoSwYuAJ3xA/DuCdQENPSoJ9KFbO4Wsow==</Q><DP>ga1rHIJro8e/yhxjrKYo/nqc5ICQGhrpMNlPkD9n3CjZVPOISkWF7FzUHEzDANeJfkZhcZa21z24aG3rKo5Qnw==</DP><DQ>MNGsCB8rYlMsRZ2ek2pyQwO7h/sZT8y5ilO9wu08Dwnot/7UMiOEQfDWstY3w5XQQHnvC9WFyCfP4h4QBissyw==</DQ><InverseQ>EG02S7SADhH1EVT9DD0Z62Y0uY7gIYvxX/uq+IzKSCwB8M2G7Qv9xgZQaQlLpCaeKbux3Y59hHM+KpamGL19Kg==</InverseQ><D>vmaYHEbPAgOJvaEXQl+t8DQKFT1fudEysTy31LTyXjGu6XiltXXHUuZaa2IPyHgBz0Nd7znwsW/S44iql0Fen1kzKioEL3svANui63O3o5xdDeExVM6zOf1wUUh/oldovPweChyoAdMtUzgvCbJk1sYDJf++Nr0FeNW1RB1XG30=</D></RSAKeyValue>";
- RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
- byte[] cipherbytes;
- rsa.FromXmlString(privatekey);
- cipherbytes = rsa.Decrypt(content, false);
- return Encoding.UTF8.GetString(cipherbytes);
- }
- }
- }
有什么问题 请给我留言
下面是Key的互通代码
- private byte[] removeMSZero(byte[] data) {
- byte[] data1;
- int len = data.length;
- if (data[0] == 0) {
- data1 = new byte[data.length - 1];
- System.arraycopy(data, 1, data1, 0, len - 1);
- } else
- data1 = data;
- return data1;
- }
C# RSA和Java RSA互通的更多相关文章
- RSA算法 Android JAVA C#互通
RSA算法属非对称加密算法,在实际使用中,往往客户端使用公钥进行加密传递敏感数据,服务端server使用私钥进行解密,这样防止中间人从网络获取敏感数据的明文. Android端主要代码如下: pack ...
- C# 与 Java Rsa加密与解密互通
Rsa 加密标准的制定已经过去了十多年了. 这两天在看rsa 加密的文章,基本上都是在说 .net 与 java 之间的 rsa加密是不能互通的.因为项目有用到,所以花了点时间对rsa加密做了一点点了 ...
- RSA密钥,JAVA与.NET之间转换
最近在做银联的一个接口,用到RSA签名,悲剧来了,.net用的RSA密钥格式和JAVA用的不一样 .net为XML格式 <RSAKeyValue><Modulus>53Knuj ...
- Android网络传输中必用的两个加密算法:MD5 和 RSA (附java完成测试代码)
MD5和RSA是网络传输中最常用的两个算法,了解这两个算法原理后就能大致知道加密是怎么一回事了.但这两种算法使用环境有差异,刚好互补. 一.MD5算法 首先MD5是不可逆的,只能加密而不能解密.比如明 ...
- Android网络传输中必用的两个加密算法:MD5 和 RSA (附java完毕測试代码)
MD5和RSA是网络传输中最经常使用的两个算法,了解这两个算法原理后就能大致知道加密是怎么一回事了.但这两种算法使用环境有差异,刚好互补. 一.MD5算法 首先MD5是不可逆的,仅仅能加密而不能解密. ...
- JAVA RSA非对称加密详解[转载]
一.概述1.RSA是基于大数因子分解难题.目前各种主流计算机语言都支持RSA算法的实现2.java6支持RSA算法3.RSA算法可以用于数据加密和数字签名4.RSA算法相对于DES/AES等对称加密算 ...
- java RSA 加签验签【转】
引用自: http://blog.csdn.net/wangqiuyun/article/details/42143957/ java RSA 加签验签 package com.testdemo.co ...
- C#-java RSA加密解密
using Org.BouncyCastle.Math; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Securi ...
- java rsa 解密报:javax.crypto.BadPaddingException: Decryption error
Exception in thread "main" javax.crypto.BadPaddingException: Decryption error at sun.se ...
随机推荐
- 【C#】用C#通过读取数据库方式读取CSV文件
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; names ...
- sql server 2008 r2 出问题
1.想利用sql2008的数据挖掘功能,以为是没有安装全,所以就卸载了. (1)利用Windows Installer Clean UP将以前的卸载干净 (2)出现了Could not open ke ...
- visual assistent 过期
VA功能超级好使,下载的一般都有时间限制,但又不想买正版. 我的是32位系统 vs2008: 将VA_X.dll文件拷到 (x86)C:\Program Files\Visual Assist X\ ...
- 设计模式 外观 Facade
外观模式的作用是简化接口.它提供一个统一的接口用来访问子系统的一群接口.通过这个高层接口使子系统更容易使用. 同时,通过外观将客户从组件的子系统中解耦. Head 1st中使用了家庭影院的例子来说明外 ...
- SpringMVC + Spring + MyBatis 学习笔记:SpringMVC和Spring一同工作的时候,AOP事务管理不起作用的解决方法
系统:WIN8.1 数据库:Oracle 11GR2 开发工具:MyEclipse 8.6 框架:Spring3.2.9.SpringMVC3.2.9.MyBatis3.2.8 SpringMVC 的 ...
- 微信企业支付--遇到不明确结果的err_code:SYSTEMERROR,NOT_FOUND
前提 项目开发中实现微信提现的功能.使用到了两个接口 企业付款接口:https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfer ...
- HDU 4617 Weapon (简单三维计算几何,异面直线距离)
Weapon Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Subm ...
- 如何在Centos上安装python3.4
Centos上面默认的Python版本是2.6,本文介绍如何安装3.4版本. 0.下载前准备 需要安装以下库,不然会有问题. yum -y install zlib-devel bzip2-devel ...
- ubuntu 64位系统下加速Android模拟器
安装KVM: sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils 在Postfix Configurati ...
- java实现图片与base64字符串之间的转换
package cn.com; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOEx ...