- ·上一篇文章:vs2005下Global.asax缺少cs文件的解决方法
- ·下一篇文章:CollapsiblePanel控件使用方法介绍
java与.net平台之间进行RSA加密验证
return false;
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
return false;
}
}
/**//// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
ssosign sso = new ssosign(RSAType.RSP);
string user = "limt";
string time = "2010-12-01 11:00:00";
string data = user + time;
string endata = Convert.ToBase64String(Encoding.UTF8.GetBytes(data));
//string ensignature = @"SjAoGfsw+vjTLOEC7eXq+V41Q6UNdRXVIdD+5gTbEfy8tfE8cgDIZRn4uIAydYfqprhJ2GbJnTTpQZxOJ0PsQR9TUVVGp0QmbNOJc/Zjm0kuBBwF43ESTSMe0CpXqOLMpLasP7hEdJlVgcrEIXijde0GxSD7qZ+6Ty8P0istR1Y=";
string ensignature = sso.signData(data);
bool result = sso.verifySignature(ensignature, endata);
Console.WriteLine("Data is validate: " + result);
//string str = HttpUtility.UrlDecode("MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OQ%3D%3D",Encoding.UTF8);
//Console.WriteLine(str);
Console.ReadLine();
}
}
}
java端代码:
package rsa;
import java.security.*;
import java.security.spec.*;
import java.io.*;
import java.security.interfaces.*;
import org.castor.util.Base64Decoder;
import org.castor.util.Base64Encoder;
public class ssosign {
private KeyPairGenerator keyGen; // Key pair generator for RSA
private PrivateKey privateKey; // Private Key Class
private PublicKey publicKey; // Public Key Class
private KeyPair keypair; // KeyPair Class
private Signature sign; // Signature, used to sign the data
/**//**
* Default Constructor. Instantiates the signature algorithm.
*/
public ssosign() {
try {
// Get the instance of Signature Engine.
sign = Signature.getInstance("SHA1withRSA");
} catch (NoSuchAlgorithmException nsa) {
System.out.println("" + nsa.getMessage());
}
}
/**//**
* Signs the data and return the signature for a given data.
*
* @param user
* The current user’s external person number
* @param time
* The current time in string format: yyyy-mm-dd hh:mm:ss
* @param EncodedCert
* The hard coded certificate string, i.e. <b>private key</b>
* @return String URLEncode string of Signature
* @throws UnsupportedEncodingException
*/
public String Sign(String user, String time, String EncodedCert) {
String returnStr = "";
try {
String toBeSigned = user + time;
byte[] signature = signData(toBeSigned.getBytes(), EncodedCert);
String base64Signature = b64encode(signature);
returnStr = base64Signature;// java.net.URLEncoder.encode(base64Signature,
// "UTF-8");
}
// catch (UnsupportedEncodingException ue) {
// // TODO Auto-generated catch block
// System.out.println(ue.getMessage());
// }
catch (Exception e) {
System.out.println(e);
}
return returnStr;
}
public boolean Verify(String base64signature, String user, String time,
String EncodedCert) {
String toBeSigned = user + time;
// try {
// base64signature = java.net.URLDecoder.decode(base64signature,
// "UTF-8");
// } catch (UnsupportedEncodingException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
byte[] signature = b64decode(base64signature);
return verifySignature(signature, toBeSigned.getBytes(), EncodedCert);
}
/**//**
* Generates the keys for given size.
*
* @param size
* Key Size [512|1024]
* @param privateKeyPath
* Private key will be generated in file which can be named with
* "privateKeyPath" parameter;
* @param publicKeyPath
* Public key will be generated in file which can be named with
* "publicKeyPath" parameter;
* @param netPublicKeyPath
* Public key can be read for .Net platform will be generated in
* file which can be named with "netPublicKeyPath" parameter;
*/
public void GenerateKeys(int size, String privateKeyPath,
String publicKeyPath, String netPublicKeyPath,
String netPrivateKeyPath) {
try {
System.out.println("Generatign Keys");
// Get Key Pair Generator for RSA.
keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(size);
keypair = keyGen.genKeyPair();
privateKey = keypair.getPrivate();
publicKey = keypair.getPublic();
// Get the bytes of the public and private keys
byte[] privateKeyBytes = privateKey.getEncoded();
byte[] publicKeyBytes = publicKey.getEncoded();
// write bytes to corresponding files.
writeKeyBytesToFile(b64encode(privateKeyBytes).getBytes(),
privateKeyPath);
String encodedValue = b64encode(publicKeyBytes);
writeKeyBytesToFile(encodedValue.getBytes(), publicKeyPath);
// Generate the Private Key, Public Key and Public Key in XML
// format.
PrivateKey privateKey = KeyFactory.getInstance("RSA")
.generatePrivate(new PKCS8EncodedKeySpec(privateKeyBytes));
PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(
new X509EncodedKeySpec(publicKeyBytes));
// RSAPublicKey rsaPublicKey = (RSAPublicKey)
// KeyFactory.getInstance(
// "RSA").generatePublic(
// new X509EncodedKeySpec(publicKeyBytes));
// // get the modules and exponent of public key to make compatible
// // .Net public key file
// String netPublicKey = getRSAPublicKeyAsNetFormat(rsaPublicKey);
// Store the modules and exponent (Generated .Net public key file)
// in file
// writeKeyBytesToFile(netPublicKey.getBytes(), netPublicKeyPath);
String netPr
java与.net平台之间进行RSA加密验证