IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Sécurité Java Discussion :

probleme avec Rsa


Sujet :

Sécurité Java

  1. #1
    Candidat au Club
    Inscrit en
    Avril 2007
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 3
    Points : 2
    Points
    2
    Par défaut probleme avec Rsa
    salu,
    j'ai une classe qui gebere deux clés un privé et lautre public pour les utiliser pour crypter et decrypter une chaine de caracteres avec l'algorithme RSA.
    j'ai fixé une chaine pour tester la classe,elle marche bien pour des essa et ne donne pas de resultat pour des autres essais.
    voici ci dessous le code de la classe:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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;
    	}
     
     
    }
    /*****************************************/
    pour la 3eme execution de la meme classe lerreur suivante saffiche:
    java.lang.StringIndexOutOfBoundsException: String index out of range: -4
    at java.lang.String.charAt(String.java:558)
    at org.bouncycastle.util.encoders.Base64Encoder.decode(Unknown Source)
    at org.bouncycastle.util.encoders.Base64.decode(Unknown Source)
    at cryptage.Encryption.main(Encryption.java:55)



    merci pour votr aide

  2. #2
    Membre averti
    Inscrit en
    Juin 2003
    Messages
    292
    Détails du profil
    Informations forums :
    Inscription : Juin 2003
    Messages : 292
    Points : 317
    Points
    317
    Par défaut
    essaye de mettre juste ton code ou il y a l exception:
    d apres tion exception tu manipule un Struing avec charAt() et tu est entrain d essayer de voir une valeur qui n exist c est comme le ArrayOutOfBoundException.

Discussions similaires

  1. Probleme avec la copie des surfaces
    Par Black_Daimond dans le forum DirectX
    Réponses: 3
    Dernier message: 09/01/2003, 10h33
  2. Problèmes avec le filtrage des ip
    Par berry dans le forum Réseau
    Réponses: 9
    Dernier message: 30/12/2002, 07h51
  3. probleme avec la touche F10
    Par b.grellee dans le forum Langage
    Réponses: 2
    Dernier message: 15/09/2002, 22h04
  4. Probleme avec fseek
    Par Bjorn dans le forum C
    Réponses: 5
    Dernier message: 04/08/2002, 07h17
  5. [Kylix] probleme avec un imagelist
    Par NicoLinux dans le forum EDI
    Réponses: 4
    Dernier message: 08/06/2002, 23h06

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo