1.å¦ä½å®ç°ç¨javascriptå®ç°rsaå 解å¯
å¦ä½å®ç°ç¨javascriptå®ç°rsaå 解å¯
å ·ä½å®ç°æè·¯å¦ä¸ï¼1ãæå¡ç«¯çæå ¬é¥ä¸ç§é¥ï¼ä¿åã
2ã客æ·ç«¯å¨è¯·æ±å°ç»å½é¡µé¢åï¼éæºçæä¸å符串ã
3ãåæ¤éæºå符串ä½ä¸ºå¯é¥å å¯å¯ç ï¼åç¨ä»æå¡ç«¯è·åå°çå ¬é¥å å¯çæçéæºå符串ã
4ãå°æ¤ä¸¤æ®µå¯æä¼ å ¥æå¡ç«¯ï¼æå¡ç«¯ç¨ç§é¥è§£åºéæºå符串ï¼åç¨æ¤ç§é¥è§£åºå å¯çå¯æã
è¿å ¶ä¸æä¸ä¸ªå ³é®æ¯è§£å³æå¡ç«¯çå ¬é¥ï¼ä¼ å ¥å®¢æ·ç«¯ï¼å®¢æ·ç«¯ç¨æ¤å ¬é¥å å¯å符串åï¼ååè½å¨æå¡ç«¯ç¨ç§é¥è§£åºã
æ¤æå³ä¸ºå®ç°æ¤æ¥èä½ã
å å¯ç®æ³ä¸ºRSAï¼
1ãæå¡ç«¯çRSA javaå®ç°ã
/***
*/
package com.sunsoft.struts.util;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;
/**
* RSA å·¥å ·ç±»ãæä¾å å¯ï¼è§£å¯ï¼çæå¯é¥å¯¹çæ¹æ³ã
* éè¦å°http://www.bouncycastle.orgä¸è½½bcprov-jdk-.jarã
*
*/
public class RSAUtil {
/**
* * çæå¯é¥å¯¹ *
*
* @return KeyPair *
* @throws EncryptException
*/
public static KeyPair generateKeyPair() throws Exception {
try {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA",源码反码与其源码相反
new org.bouncycastle.jce.provider.BouncyCastleProvider());
final int KEY_SIZE = ;// 没ä»ä¹å¥½è¯´çäºï¼è¿ä¸ªå¼å ³ç³»å°åå å¯ç大å°ï¼å¯ä»¥æ´æ¹ï¼ä½æ¯ä¸è¦å¤ªå¤§ï¼å¦åæçä¼ä½
keyPairGen.initialize(KEY_SIZE, new SecureRandom());
KeyPair keyPair = keyPairGen.generateKeyPair();
saveKeyPair(keyPair);
return keyPair;
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
public static KeyPair getKeyPair()throws Exception{
FileInputStream fis = new FileInputStream("C:/RSAKey.txt");
ObjectInputStream oos = new ObjectInputStream(fis);
KeyPair kp= (KeyPair) oos.readObject();
oos.close();
fis.close();
return kp;
}
public static void saveKeyPair(KeyPair kp)throws Exception{
FileOutputStream fos = new FileOutputStream("C:/RSAKey.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
//çæå¯é¥
oos.writeObject(kp);
oos.close();
fos.close();
}
/**
* * çæå ¬é¥ *
*
* @param modulus *
* @param publicExponent *
* @return RSAPublicKey *
* @throws Exception
*/
public static RSAPublicKey generateRSAPublicKey(byte[] modulus,
byte[] publicExponent) throws Exception {
KeyFactory keyFac = null;
try {
keyFac = KeyFactory.getInstance("RSA",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
} catch (NoSuchAlgorithmException ex) {
throw new Exception(ex.getMessage());
}
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(
modulus), new BigInteger(publicExponent));
try {
return (RSAPublicKey) keyFac.generatePublic(pubKeySpec);
} catch (InvalidKeySpecException ex) {
throw new Exception(ex.getMessage());
}
}
/**
* * çæç§é¥ *
*
* @param modulus *
* @param privateExponent *
* @return RSAPrivateKey *
* @throws Exception
*/
public static RSAPrivateKey generateRSAPrivateKey(byte[] modulus,
byte[] privateExponent) throws Exception {
KeyFactory keyFac = null;
try {
keyFac = KeyFactory.getInstance("RSA",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
} catch (NoSuchAlgorithmException ex) {
throw new Exception(ex.getMessage());
}
RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(new BigInteger(
modulus), new BigInteger(privateExponent));
try {
return (RSAPrivateKey) keyFac.generatePrivate(priKeySpec);
} catch (InvalidKeySpecException ex) {
throw new Exception(ex.getMessage());
}
}
/**
* * å å¯ *
*
* @param key
* å å¯çå¯é¥ *
* @param data
* å¾ å å¯çæææ°æ® *
* @return å å¯åçæ°æ® *
* @throws Exception
*/
public static byte[] encrypt(PublicKey pk, byte[] data) throws Exception {
try {
Cipher cipher = Cipher.getInstance("RSA",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE, pk);
int blockSize = cipher.getBlockSize();// è·å¾å å¯å大å°ï¼å¦ï¼å å¯åæ°æ®ä¸ºä¸ªbyteï¼èkey_size=
// å å¯å大å°ä¸º
// byte,å å¯å为个byte;å æ¤å ±æ2个å å¯åï¼ç¬¬ä¸ä¸ª
// byte第äºä¸ªä¸º1个byte
int outputSize = cipher.getOutputSize(data.length);// è·å¾å å¯åå å¯åå大å°
int leavedSize = data.length % blockSize;
int blocksSize = leavedSize != 0 ? data.length / blockSize + 1
: data.length / blockSize;
byte[] raw = new byte[outputSize * blocksSize];
int i = 0;
while (data.length - i * blockSize > 0) {
if (data.length - i * blockSize > blockSize)
cipher.doFinal(data, i * blockSize, blockSize, raw, i
* outputSize);
else
cipher.doFinal(data, i * blockSize, data.length - i
* blockSize, raw, i * outputSize);
// è¿éé¢doUpdateæ¹æ³ä¸å¯ç¨ï¼æ¥çæºä»£ç ååç°æ¯æ¬¡doUpdateå并没æä»ä¹å®é å¨ä½é¤äºæbyte[]æ¾å°
// ByteArrayOutputStreamä¸ï¼èæådoFinalçæ¶åæå°ææçbyte[]è¿è¡å å¯ï¼å¯æ¯å°äºæ¤æ¶å å¯å大å°å¾å¯è½å·²ç»è¶ åºäº
// OutputSizeæ以åªå¥½ç¨dofinalæ¹æ³ã
i++;
}
return raw;
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
/**
* * è§£å¯ *
*
* @param key
* 解å¯çå¯é¥ *
* @param raw
* å·²ç»å å¯çæ°æ® *
* @return 解å¯åçææ *
* @throws Exception
*/
public static byte[] decrypt(PrivateKey pk, byte[] raw) throws Exception {
try {
Cipher cipher = Cipher.getInstance("RSA",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
cipher.init(cipher.DECRYPT_MODE, pk);
int blockSize = cipher.getBlockSize();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
int j = 0;
while (raw.length - j * blockSize > 0) {
bout.write(cipher.doFinal(raw, j * blockSize, blockSize));
j++;
}
return bout.toByteArray();
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
/**
* * *
*
* @param args *
* @throws Exception
*/
public static void main(String[] args) throws Exception {
RSAPublicKey rsap = (RSAPublicKey) RSAUtil.generateKeyPair().getPublic();
String test = "hello world";
byte[] en_test = encrypt(getKeyPair().getPublic(),test.getBytes());
byte[] de_test = decrypt(getKeyPair().getPrivate(),en_test);
System.out.println(new String(de_test));
}
}
2.æµè¯é¡µé¢ï¼
IndexAction.java