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
2024-11-25 02:161981人浏览
2024-11-25 01:562191人浏览
2024-11-25 01:54405人浏览
2024-11-25 01:512833人浏览
2024-11-25 01:19216人浏览
2024-11-25 00:37822人浏览
1.微信扫雷怎么玩 微信扫雷玩法介绍微信扫雷怎么玩 微信扫雷玩法介绍 1、下载微信红包扫雷这款软件,打开。 2、在“设置——辅助功能——微信红包扫雷软件”里,点击“开启”。 3、打开微信,
文:瑪格麗特・蜜Margaret Mee)第七章 沿毛埃斯河生長的五彩鳳梨屬植物 1971 年我的植物讓我忙碌了好幾個月。接著,出乎意料地,我收到了一份令人感到非常愉悅的驚喜,在亞馬遜國家研究院的資深
中国消费者报重庆讯记者刘文新)近日,重庆市场监管局认真贯彻落实全国市场监管工作电视电话会议精神,紧盯全年目标任务,以钉钉子精神抓好下半年各项任务落实,特别是要维护统一大市场,确保市场监管工作上台阶。重