谁能将银联商务的这段Java RSA加密解密翻译成C#版本呀?

datahandler2 2016-01-19 04:35:36

//EncryptUtils.java
package com.chinaums.demo.encrypt;

import javax.crypto.Cipher;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.math.BigInteger;
import java.security.*;
import java.security.spec.*;

/**
* 加解密工具包
*
* @author YaoFeng
*/
public class EncryptUtils {

private static final String ALG_RSA = "RSA";
private static final String ALG_DSA = "DSA";

/**
* 私钥加密
*
* @param data 源数据
* @param privateKey 私钥
* @return
* @throws Exception
*/
public static byte[] encrypt(byte[] data, final PrivateKey privateKey) throws Exception {
final Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
return cipher(data, cipher, getBlockSize(privateKey) - 11);
}

/**
* 私钥解密
*
* @param encryptedData 已加密数据
* @param privateKey 私钥
* @return
* @throws Exception
*/
public static byte[] decrypt(byte[] encryptedData, final PrivateKey privateKey) throws Exception {
final Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher(encryptedData, cipher, getBlockSize(privateKey));
}

/**
* 公钥加密
*
* @param data 源数据
* @param publicKey 公钥
* @return
* @throws Exception
*/
public static byte[] encrypt(byte[] data, final PublicKey publicKey) throws Exception {
final Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher(data, cipher, getBlockSize(publicKey) - 11);
}

/**
* 公钥解密
*
* @param encryptedData 已加密数据
* @param publicKey 公钥
* @return
* @throws Exception
*/
public static byte[] decrypt(byte[] encryptedData, final PublicKey publicKey) throws Exception {
final Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, publicKey);
return cipher(encryptedData, cipher, getBlockSize(publicKey));
}

private static byte[] cipher(byte[] data, Cipher cipher, int blockSize) throws Exception {
final ByteArrayInputStream in = new ByteArrayInputStream(data);
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final byte[] cache = new byte[blockSize];
while (true) {
final int r = in.read(cache);
if (r < 0) {
break;
}
final byte[] temp = cipher.doFinal(cache, 0, r);
out.write(temp, 0, temp.length);
}
return out.toByteArray();
}

private static int getBlockSize(final Key key) throws NoSuchAlgorithmException, InvalidKeySpecException {
final String alg = key.getAlgorithm();
final KeyFactory keyFactory = KeyFactory.getInstance(alg);
if (key instanceof PublicKey) {
final BigInteger prime;
if (ALG_RSA.equals(alg)) {
prime = keyFactory.getKeySpec(key, RSAPublicKeySpec.class).getModulus();
} else if (ALG_DSA.equals(alg)) {
prime = keyFactory.getKeySpec(key, DSAPublicKeySpec.class).getP();
} else {
throw new NoSuchAlgorithmException("不支持的解密算法:" + alg);
}
return prime.toString(2).length() / 8;
} else if (key instanceof PrivateKey) {
final BigInteger prime;
if (ALG_RSA.equals(alg)) {
prime = keyFactory.getKeySpec(key, RSAPrivateKeySpec.class).getModulus();
} else if (ALG_DSA.equals(alg)) {
prime = keyFactory.getKeySpec(key, DSAPrivateKeySpec.class).getP();
} else {
throw new NoSuchAlgorithmException("不支持的解密算法:" + alg);
}
return prime.toString(2).length() / 8;
} else {
throw new RuntimeException("不支持的密钥类型:" + key.getClass());
}
}
}



// CertificateUtils.java
package com.chinaums.demo.encrypt;

import java.io.IOException;
import java.io.InputStream;
import java.security.*;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Date;
import java.util.Enumeration;

/**
* 数字证书工具包
*
* @author YaoFeng
*/
public class CertificateUtils {

/**
* Java密钥库(Java 密钥库,JKS)KEY_STORE
*/
public static final String KEY_STORE = "JKS";

public static final String X509 = "X.509";

/**
* 根据密钥库获得私钥
*
* @param keyStore 密钥库输入流
* @param alias 密钥别名,如果为<code>null</code>,则读取密钥库的一个条密钥
* @param password 密钥库密码
* @return
* @throws Exception
*/
public static PrivateKey getPrivateKey(final InputStream keyStore, final String alias, final String password)
throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, UnrecoverableKeyException {
final KeyStore keyStore0 = getKeyStore(keyStore, password);
if (alias != null) {
return (PrivateKey) keyStore0.getKey(alias, password.toCharArray());
} else {
final Enumeration<String> aliases = keyStore0.aliases();
return (PrivateKey) keyStore0.getKey(aliases.nextElement(), password.toCharArray());
}
}

/**
* 根据证书获得公钥
*
* @param certificate 证书输入流
* @return
* @throws Exception
*/
public static PublicKey getPublicKey(final InputStream certificate) throws CertificateException {
return getCertificate(certificate).getPublicKey();
}

/**
* 获得密钥库
*
* @param keyStore 密钥库输入流
* @param password 密钥库密码
* @return
* @throws Exception
*/
private static KeyStore getKeyStore(final InputStream keyStore, final String password)
throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException {
final KeyStore result = KeyStore.getInstance(KEY_STORE);
result.load(keyStore, password.toCharArray());
return result;
}

/**
* <p>
* 获得证书
* </p>
*
* @param certificate 证书输入流
* @return
* @throws Exception
*/
private static Certificate getCertificate(InputStream certificate) throws CertificateException {
final CertificateFactory certificateFactory = CertificateFactory.getInstance(X509);
return certificateFactory.generateCertificate(certificate);
}

/**
* <p>
* 根据密钥库获得证书
* </p>
*
* @param keyStore 密钥库输入流
* @param alias 密钥库别名
* @param password 密钥库密码
* @return
* @throws Exception
*/
private static Certificate getCertificate(InputStream keyStore, String alias, String password) throws Exception {
return getKeyStore(keyStore, password).getCertificate(alias);
}

/**
* <p>
* 校验证书当前是否有效
* </p>
*
* @param certificate 证书
* @return
*/
public static boolean verifyCertificate(Certificate certificate) {
return verifyCertificate(new Date(), certificate);
}

/**
* <p>
* 验证证书是否过期或无效
* </p>
*
* @param date 日期
* @param certificate 证书
* @return
*/
public static boolean verifyCertificate(Date date, Certificate certificate) {
try {
final X509Certificate x509Certificate = (X509Certificate) certificate;
x509Certificate.checkValidity(date);
return true;
} catch (Exception e) {
return false;
}
}

/**
* <p>
* 验证数字证书是在给定的日期是否有效
* </p>
*
* @param date 日期
* @param certificate 证书输入流
* @return
*/
public static boolean verifyCertificate(Date date, InputStream certificate) {
try {
return verifyCertificate(getCertificate(certificate));
} catch (Exception e) {
return false;
}
}

/**
* <p>
* 验证数字证书是在给定的日期是否有效
* </p>
*
* @param keyStore 密钥库输入流
* @param alias 密钥库别名
* @param password 密钥库密码
* @return
*/
public static boolean verifyCertificate(Date date, InputStream keyStore, String alias, String password) {
try {
return verifyCertificate(date, getCertificate(keyStore, alias, password));
} catch (Exception e) {
return false;
}
}

/**
* <p>
* 验证数字证书当前是否有效
* </p>
*
* @param keyStore 密钥库存储路径
* @param alias 密钥库别名
* @param password 密钥库密码
* @return
*/
public static boolean verifyCertificate(InputStream keyStore, String alias, String password) {
return verifyCertificate(new Date(), keyStore, alias, password);
}

/**
* <p>
* 验证数字证书当前是否有效
* </p>
*
* @param certificate 证书输入流
* @return
*/
public static boolean verifyCertificate(InputStream certificate) {
return verifyCertificate(new Date(), certificate);
}

}

...全文
689 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
lhf1990729 2016-01-22
  • 打赏
  • 举报
回复
银商,我擦。那个坑的一个B....h5支付的后退bug也不修复,Mpos也没出新版本
datahandler2 2016-01-21
  • 打赏
  • 举报
回复
引用 16 楼 yeness 的回复:
如果是这样,确实建议你,直接上java吧,难度总比,把代码都翻译成c#要小的多。
这成本也太高了吧!你有用过IKVM.net? 奇怪webform可以调用,但MVC却没法用。。太怪异了。。 不知道是不是IKVM.net 不支持mvc....
xuan.ye 2016-01-21
  • 打赏
  • 举报
回复
如果是这样,确实建议你,直接上java吧,难度总比,把代码都翻译成c#要小的多。
datahandler2 2016-01-21
  • 打赏
  • 举报
回复
引用 13 楼 yeness 的回复:
其实您得出 "只有java可以支持私钥加密公钥解密"的结论应该是错误; 既然是支付,大多数支付会同时提供几种语言的支付demo和sdk。首先需要仔细翻看下文档,其中不会没有.net。 我本人也做过支付,一般常见支付接口都有提供.net的demo和sdk。
银联商务的代付功能跟支付不一样!
datahandler2 2016-01-21
  • 打赏
  • 举报
回复
引用 13 楼 yeness 的回复:
其实您得出 "只有java可以支持私钥加密公钥解密"的结论应该是错误; 既然是支付,大多数支付会同时提供几种语言的支付demo和sdk。首先需要仔细翻看下文档,其中不会没有.net。 我本人也做过支付,一般常见支付接口都有提供.net的demo和sdk。
没有!
xuan.ye 2016-01-21
  • 打赏
  • 举报
回复
其实您得出 "只有java可以支持私钥加密公钥解密"的结论应该是错误; 既然是支付,大多数支付会同时提供几种语言的支付demo和sdk。首先需要仔细翻看下文档,其中不会没有.net。 我本人也做过支付,一般常见支付接口都有提供.net的demo和sdk。
datahandler2 2016-01-21
  • 打赏
  • 举报
回复
结贴了,对方终于开发了一个是人用的新接口了!
datahandler2 2016-01-21
  • 打赏
  • 举报
回复
引用 19 楼 zdslgl 的回复:
http://download.csdn.net/detail/zdslgl/8861509 C# RSA加密解密
Doson 2016-01-21
  • 打赏
  • 举报
回复
xuan.ye 2016-01-21
  • 打赏
  • 举报
回复
IKVM 别说用过,连听都没听过。基本上无论开发什么,本人均使用原生开发。 关键是你给出代码,指明了需要引用一个jar 包,不是把代码进行翻译这么简单。 而且你也指明支付没有.net 的版本。 -------------------------------------------------------- 虚拟机或者非原生的开发往往难度更大。不如你找个会java的进行下二次开发,你在.net 程序中调用java接口即可。 而且可能你给的这个代码,也需要调试和适配。
datahandler2 2016-01-20
  • 打赏
  • 举报
回复
引用 7 楼 xdashewan 的回复:
[quote=引用 6 楼 tiancaolin 的回复:] 我现在的根源问题是: 我客户端c# 利用pfx文件获取私钥,然后将这个私钥和要加密的原文进行RSA加密,再将加密后的密文传到对方java服务端用他们的公钥进行解密,解密后对方处理完后再用他们的公钥进行加密,我本地再用私钥进行解密。。。但问题是我c#私钥加密后,他们服务端公钥没法解密。。。。。我查了下c#貌似不支持私钥加密公钥解密的。。。求解啊!
你用的是类库的rsa吗[/quote] 加密方法就这样。。。

public static byte[] EncryptString(string source,string privateKey)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            byte[] data = Encoding.UTF8.GetBytes(source);             
            rsa.FromXmlString(privateKey);

            int keySize = rsa.KeySize / 8;
            int bufferSize = keySize - 11;
            byte[] buffer = new byte[bufferSize];
            MemoryStream msInput = new MemoryStream(data);
            MemoryStream msOutput = new MemoryStream();
            int readLen = msInput.Read(buffer, 0, bufferSize);
            while (readLen > 0)
            {
                byte[] dataToEnc = new byte[readLen];
                Array.Copy(buffer, 0, dataToEnc, 0, readLen);
                byte[] encData = rsa.Encrypt(dataToEnc, false);
                msOutput.Write(encData, 0, encData.Length);
                readLen = msInput.Read(buffer, 0, bufferSize);
            }
            msInput.Close();
            byte[] result = msOutput.ToArray();    //得到加密结果
            msOutput.Close();
            rsa.Clear();
            return result;
        }
xdashewan 2016-01-20
  • 打赏
  • 举报
回复
引用 6 楼 tiancaolin 的回复:
我现在的根源问题是: 我客户端c# 利用pfx文件获取私钥,然后将这个私钥和要加密的原文进行RSA加密,再将加密后的密文传到对方java服务端用他们的公钥进行解密,解密后对方处理完后再用他们的公钥进行加密,我本地再用私钥进行解密。。。但问题是我c#私钥加密后,他们服务端公钥没法解密。。。。。我查了下c#貌似不支持私钥加密公钥解密的。。。求解啊!
你用的是类库的rsa吗
datahandler2 2016-01-20
  • 打赏
  • 举报
回复
引用 5 楼 xdashewan 的回复:
[quote=引用 4 楼 tiancaolin 的回复:] 你能贴一个你自己的RSA加密解密方法?我现在的根源问题是C# 读取pfx证书密钥文件后,利用得到的公钥加密密文提交到服务器端(Java),但服务器端解密不了。。。这个服务器端是第三方支付平台。。。很郁闷,不知道啥原因,没有相关思路调试!
看看http://zhuoyaopingzi.iteye.com/blog/1992205[/quote] 我现在的根源问题是: 我客户端c# 利用pfx文件获取私钥,然后将这个私钥和要加密的原文进行RSA加密,再将加密后的密文传到对方java服务端用他们的公钥进行解密,解密后对方处理完后再用他们的公钥进行加密,我本地再用私钥进行解密。。。但问题是我c#私钥加密后,他们服务端公钥没法解密。。。。。我查了下c#貌似不支持私钥加密公钥解密的。。。求解啊!
xdashewan 2016-01-20
  • 打赏
  • 举报
回复
引用 4 楼 tiancaolin 的回复:
你能贴一个你自己的RSA加密解密方法?我现在的根源问题是C# 读取pfx证书密钥文件后,利用得到的公钥加密密文提交到服务器端(Java),但服务器端解密不了。。。这个服务器端是第三方支付平台。。。很郁闷,不知道啥原因,没有相关思路调试!
看看http://zhuoyaopingzi.iteye.com/blog/1992205
datahandler2 2016-01-20
  • 打赏
  • 举报
回复
引用 3 楼 FoxDave 的回复:
你直接搜索C# RSA就行了啊
你能贴一个你自己的RSA加密解密方法?我现在的根源问题是C# 读取pfx证书密钥文件后,利用得到的公钥加密密文提交到服务器端(Java),但服务器端解密不了。。。这个服务器端是第三方支付平台。。。很郁闷,不知道啥原因,没有相关思路调试!
Justin-Liu 2016-01-20
  • 打赏
  • 举报
回复
你直接搜索C# RSA就行了啊
datahandler2 2016-01-20
  • 打赏
  • 举报
回复
引用 11 楼 yeness 的回复:
不如直接上个tomcat 把现有代码布置上,改一下java的业务逻辑。 c#直接读取数据就好了。
大哥,这个java端是第三方的支付公司。。。这种希望我压根就不敢想。。。。貌似问题根源是只有java可以支持私钥加密公钥解密。。其他语言或c#压根就不支持私钥加密公钥解密的。。。。
xuan.ye 2016-01-20
  • 打赏
  • 举报
回复
不如直接上个tomcat 把现有代码布置上,改一下java的业务逻辑。 c#直接读取数据就好了。
datahandler2 2016-01-20
  • 打赏
  • 举报
回复
引用 9 楼 xdashewan 的回复:
[quote=引用 8 楼 tiancaolin 的回复:] 加密方法就这样。。。
再看看http://bbs.csdn.net/topics/390833693[/quote] 这个帖子我早看过了。。。但没帮助。。。我需要的是c# 如何用私钥加密。。。好让java端可以解密。。。。就一个方法!!!!!
xdashewan 2016-01-20
  • 打赏
  • 举报
回复
引用 8 楼 tiancaolin 的回复:
加密方法就这样。。。
再看看http://bbs.csdn.net/topics/390833693
加载更多回复(2)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧