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 ...
随机推荐
- 细雨学习笔记:Jmeter集合点
设置集合点的原则 (1) 集合点设置数<=线程组线程数量(因为大于线程组线程数量的话就永远也到不了集合点) (2)线程组线程数量是集合点设置数的整数倍(因为分组有余数的话最后一组永远也到不了集合 ...
- python内建函数sorted方法概述
python中,具体到对list进行排序的方法有俩,一个是list自带的sort方法,这个是直接对list进行操作,只有list才包含的方法:另外一个是内建函数sorted方法,可以对所有可迭代的对象 ...
- 【LeetCode】100 - Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- 高质量代码之HTML、CSS篇
HTML篇 使用语义化标签<strong><fieldset><legend><ul>等等,少用<div><span> 判断网页 ...
- TopFreeTheme精选免费模板【20130619】
今天给大家推荐7款最新精选的WordPress主题和一个WooCommerce订单跟踪插件,如果你有更换自己博客主题的想法或者正要做自己的博客,不妨试试.一些是WordPress商业模板,但都可以免费 ...
- Redis批量导入数据
首先准备数据文件 格式为 SET Key0 Value0 SET Key1 Value1 ... SET KeyN ValueN 利用shell转换数据 #!/bin/bash while read ...
- hadoop2.2伪分布安装加2.2源码编译
配置linux基本环境: --> java.ip.hostname.hosts.iptables.chkconfig.ssh环境配置 hadoop2.2安装在linux64位机器上,需要对源码进 ...
- 《Linux设备驱动程序》 笔记2
驱动代码hello.c #include <linux/init.h> #include <linux/module.h> static int hello_init(void ...
- [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序
一. 题目 487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 274040 Accepted: 48891 ...
- INTEL XDK 真机调试
需要安装 1.google服务框架 2.google play 3.app preview