1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
//***************************************//
package cryptage;
import java.io.IOException;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.SecureRandom;
import javax.crypto.spec.*;
import org.bouncycastle.crypto.AsymmetricBlockCipher;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.encodings.PKCS1Encoding;
import org.bouncycastle.crypto.engines.RSAEngine;
import org.bouncycastle.crypto.generators.RSAKeyPairGenerator;
import org.bouncycastle.crypto.params.RSAKeyGenerationParameters;
import org.bouncycastle.crypto.params.RSAKeyParameters;
import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters;
import org.bouncycastle.util.encoders.Base64;
import sun.misc.BASE64Encoder;
import java.security.spec.RSAPublicKeySpec;
public class Encryption {
private RSAPrivateCrtKeyParameters _RSAPrivateKey;
private RSAKeyParameters _RSAPublicKey;
RSAKeyParameters rsaKeysFromWS;
private BigInteger exponent;
private BigInteger modulus;
public static void main(String[] args) {
Encryption theEncryption = new Encryption();
String theStringBeforeEncryption="salu tout le monde";
String theEncryptedString;
String theDecryptedString;
try {
//generation de la paire des clés
theEncryption.generateRSAKeyPair();
//cryptage de la chaine
//le resultat est un chaine encodée Base64
theEncryptedString = theEncryption.RSAEncrypt(theStringBeforeEncryption,theEncryption.get_RSAPublicKey());
System.out.println(theEncryptedString);
//decodage de la chaine
String s1=new String(Base64.decode(theEncryptedString));
//affiche de la chaine decodée et sa longueur
System.out.println(s1+":"+s1.length());
//decryptage de la chaine
//resultat encodée en Base64
theDecryptedString = theEncryption.RSADecrypt(s1,theEncryption.get_RSAPrivateKey());
System.out.println(theDecryptedString);
//decodeage de la chaine
String s2=new String(Base64.decode(theDecryptedString));
//affichage de la chaine decryptée puis decodée
System.out.println(s2);
} catch (Exception e) {
// TODO Handle exception!
e.printStackTrace();
}
}
public void generateRSAKeyPair() throws Exception {
System.out.println("on est en train de generrer les cles");
SecureRandom theSecureRandom = new SecureRandom();
BigInteger thePublicExponent = new BigInteger("10001", 20);
RSAKeyGenerationParameters theRSAKeyGenParam = new RSAKeyGenerationParameters(
thePublicExponent, theSecureRandom, 512, 100);
RSAKeyPairGenerator theRSAKeyPairGen = new RSAKeyPairGenerator();
theRSAKeyPairGen.init(theRSAKeyGenParam);
AsymmetricCipherKeyPair theKeyPair = theRSAKeyPairGen.generateKeyPair();
_RSAPrivateKey = (RSAPrivateCrtKeyParameters) theKeyPair.getPrivate();
_RSAPublicKey = (RSAKeyParameters) theKeyPair.getPublic();
String s1=getMod(_RSAPublicKey);
String s2=getExp(_RSAPublicKey);
modulus = new BigInteger(1,Base64.decode(s1));
exponent = new BigInteger(1,Base64.decode(s2));
rsaKeysFromWS = new RSAKeyParameters(false, modulus, exponent);
set_RSAPrivateKey(_RSAPrivateKey);
set_RSAPublicKey(_RSAPublicKey);
}
public String RSAEncrypt(String Encrypt,RSAKeyParameters rsaKeysFromWS1) throws Exception {
if (rsaKeysFromWS1 == null) {
throw new Exception(
"Please generate RSA keys first in order to work");
}
byte[] toEncrypt = Encrypt.getBytes();
AsymmetricBlockCipher theEngine = new RSAEngine();
theEngine = new PKCS1Encoding(theEngine);
theEngine.init(true, rsaKeysFromWS1);
//return theEngine.processBlock(toEncrypt, 0, toEncrypt.length);
try
{
byte[] raw = theEngine.processBlock(toEncrypt, 0, toEncrypt.length);
String tmpStr = new String(Base64.encode(raw));
return tmpStr;
} catch (InvalidCipherTextException err) {
return "";
}
}
public String RSADecrypt(String Decrypt,RSAPrivateCrtKeyParameters RSAPrivateKey) throws Exception {
if (RSAPrivateKey == null) {
throw new Exception(
"Please generate RSA keys first in order to work");
}
byte[] toDecrypt = Decrypt.getBytes();
System.out.println(toDecrypt);
System.out.println(toDecrypt.length);
AsymmetricBlockCipher theEngine = new RSAEngine();
theEngine = new PKCS1Encoding(theEngine);
theEngine.init(false,RSAPrivateKey);
//return theEngine.processBlock(toDecrypt, 0, toDecrypt.length);
try
{
byte[] raw = theEngine.processBlock(toDecrypt, 0,toDecrypt.length);
String tmpStr = new String(Base64.encode(raw));
return tmpStr;
} catch (InvalidCipherTextException err) {
return "";
}
}
/*****************************************/
// Public key specific parameter.
public static String getMod(RSAKeyParameters rsaKey) throws Exception {
return (new String(Base64.encode(rsaKey.getModulus().toByteArray())));
}
// General key parameter. pubExp is the same as pubKey.getExponent()
public static String getExp(RSAKeyParameters rsaKey) throws Exception {
return (new String(Base64.encode(rsaKey.getExponent().toByteArray())));
}
// General key parameter. pubExp is the same as pubKey.getExponent()
public String getPubExp(RSAKeyParameters rsaKey) throws Exception {
return (new String(Base64.encode(rsaKey.getExponent().toByteArray())));
}
public RSAPrivateCrtKeyParameters get_RSAPrivateKey() {
return _RSAPrivateKey;
}
public void set_RSAPrivateKey(RSAPrivateCrtKeyParameters privateKey) {
_RSAPrivateKey = privateKey;
}
public RSAKeyParameters get_RSAPublicKey() {
return _RSAPublicKey;
}
public void set_RSAPublicKey(RSAKeyParameters publicKey) {
_RSAPublicKey = publicKey;
}
} |
Partager