1.å¦ä½å©ç¨OpenSSLåºè¿è¡RSAå å¯å解å¯
2.å¦ä½å®ç°ç¨javascriptå®ç°rsaå 解å¯
å¦ä½å©ç¨OpenSSLåºè¿è¡RSAå å¯å解å¯
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<openssl/rsa.h>
#include<openssl/engine.h>
int main(int argc,解解密硕士论文源码 char* argv[])
{
printf("openssl_test begin\n");
RSA* rsa=NULL;
char originstr[]="hello\n"; //è¿æ¯æ们éè¦å å¯çåå§æ°æ®
//allocate RSA structureï¼é¦å éè¦ç³è¯·ä¸ä¸ªRSAç»æé¢ç¨äºåæ¾çæçå ¬ç§é¥ï¼è¿érsaå°±æ¯è¿ä¸ªç»æä½çæé
rsa = RSA_new();
if(rsa==NULL)
{
printf("RSA_new failed\n");
return -1;
}
//generate RSA keys
BIGNUM* exponent;
exponent = BN_new(); //çæRSAå ¬ç§é¥ä¹åéè¦éæ©ä¸ä¸ªå¥æ°ï¼odd numberï¼æ¥ç¨äºçæå ¬ç§é¥
if(exponent ==NULL)
{
printf("BN_new failed\n");
goto FAIL1;
}
if(0==BN_set_word(exponent,)) //è¿ééæ©å¥æ°
{
printf("BN_set_word failed\n");
goto FAIL1;
}
//è¿émodulusçé¿åº¦éæ©ï¼å°äºçmodulusé¿åº¦é½æ¯ä¸å®å ¨çï¼å®¹æè¢«ç ´è§£
if(0==RSA_generate_key_ex(rsa,,exponent,NULL))
{
printf("RSA_generate_key_ex failed\n");
goto FAIL;
}
char* cipherstr = NULL;
//åé ä¸æ®µç©ºé´ç¨äºåå¨å å¯åçæ°æ®ï¼è¿ä¸ªç©ºé´ç大å°ç±RSA_sizeå½æ°æ ¹æ®rsaç®åº
cipherstr = malloc(RSA_size(rsa));
if(cipherstr==NULL)
{
printf("malloc cipherstr buf failed\n");
goto FAIL1;
}
//ä¸é¢æ¯å®é çå å¯è¿ç¨ï¼æåä¸ä¸ªåæ°padding typeï¼æ以ä¸å ç§ã
/
*RSA_PKCS1_PADDINGPKCS #1 v1.5 padding. This currently is the most widely used mode.
RSA_PKCS1_OAEP_PADDING
EME-OAEP as defined in PKCS #1 v2.0 with SHA-1, MGF1 and an empty encoding parameter. This mode is recommended for all new applications.
RSA_SSLV_PADDING
PKCS #1 v1.5 padding with an SSL-specific modification that denotes that the server is SSL3 capable.
RSA_NO_PADDING
Raw RSA encryption. This mode should only be used to implement cryptographically sound padding modes in the application code. Encrypting user data directly with RSA is insecure.
*/
//è¿éé¦å ç¨å ¬é¥è¿è¡å å¯ï¼éæ©äºRSA_PKCS1_PADDING
if(RSA_size(rsa)!=RSA_public_encrypt(strlen(originstr)+1,originstr,cipherstr,rsa,RSA_PKCS1_PADDING))
{
printf("encryption failure\n");
goto FAIL2;
}
printf("the original string is %s\n",originstr);
printf("the encrypted string is %s\n",cipherstr);
//Now, let's decrypt the string with private key
//ä¸é¢æ¥ç¨ç§é¥è§£å¯ï¼é¦å éè¦ä¸ä¸ªbufferç¨äºåå¨è§£å¯åçæ°æ®ï¼è¿ä¸ªbufferçé¿åº¦è¦è¶³å¤ï¼å°äºRSA_size(rsa)ï¼
//è¿éåé ä¸ä¸ªé¿åº¦ä¸ºçå符æ°ç»ï¼åºè¯¥æ¯å¤ç¨çã
char decrypted_str[];
int decrypted_len;
if(-1=(decrypted_len=RSA_private_decrypt(,cipherstr,decrypted_str,rsa,RSA_PKCS1_PADDING)))
{
printf("decryption failure\n");
goto FAIL2;
}
printf("decrypted string length is %d,decryped_str is %s\n",decrypted_len,decrypted_str);
FAIL2:
free(cipherstr);
FAIL1:
BN_free(exponent);
FAIL:
RSA_free(rsa);
return 0;
}
以ä¸æ¯æºä»£ç ï¼ä¸é¢ä½¿ç¨ä¸é¢çç¼è¯å½ä»¤å¨æºç æå¨è·¯å¾ä¸çæå¯æ§è¡æ件
gcc *.c -o openssl_test -lcrypto -ldl -L/usr/local/ssl/lib -I/usr/local/ssl/include
å ¶ä¸ï¼-lcryptoå-ldlæ¯å¿ é¡»çï¼åè æ¯OpenSSLä¸çå å¯ç®æ³åºï¼åè æ¯ç¨äºæåå è½½å¨æåºã
å¦ä½å®ç°ç¨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