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 :

Cryptage/Decryptage de String


Sujet :

Sécurité Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Mai 2006
    Messages
    41
    Détails du profil
    Informations forums :
    Inscription : Mai 2006
    Messages : 41
    Par défaut Cryptage/Decryptage de String
    Bonjour,

    En fait je cherche à faire quelque chose d'assez simple.
    J'aimerais crypter de manière asymétrique une chaine de caractère sur un serveur, puis sur un autre serveur (poscédant la clé publique correspondante), décrypter cette meme chaine à l'intérieur d'une JSP ou autre.

    J'ai trouvé cette méthode pour crypter/decrypter un fichier apres avoir générer le duo-clé publique/clé privé proposée par Razgriz ici

    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
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    /*
     * RSAEncryptor.java
     *
     * Created on 17 juin 2006, 15:58
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */
     
    package Security;
     
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.security.GeneralSecurityException;
    import java.security.Key;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.NoSuchAlgorithmException;
    import java.security.SecureRandom;
    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    import javax.crypto.SecretKey;
     
    /**
     *
     * @author Absil Romain
     */
    public class RSAEncryptor
    {
     
        /**
         * Generate a pair of key, write the public in publicOutputFileName and the
         * private in privateOutputFileName.
         * @param publicOutputFileName the file where the public key is written in.
         * @param privateOutputFileName the file where the private key is written in.
         **/
        public static void generateKeys(String publicInputFileName, 
                String privateOutputFileName)
        {
            try
            {
                KeyPairGenerator pairgen = KeyPairGenerator.getInstance("RSA");
                SecureRandom random = new SecureRandom();
                pairgen.initialize(2048, random);
                KeyPair keyPair = pairgen.generateKeyPair();
     
                ObjectOutputStream out = new ObjectOutputStream(
                        new FileOutputStream(publicInputFileName));
                out.writeObject(keyPair.getPublic());
                out.close();
     
                out = new ObjectOutputStream(
                        new FileOutputStream(privateOutputFileName));
                out.writeObject(keyPair.getPrivate());
                out.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
     
        /**
         * Encrytps the file inputFileName and saves the result in the file 
         * outputFileName, with the specified key keyFileName.
         * @param inputFileNane the file to encrypt.
         * @param outputFileName the file to save the encrypting result in.
         * @param keyFileName the key to use to encrypt the file.
         **/
        public static void encryptFile(String inputFileName, String outputFileName, 
                String keyFileName)
        {
            try
            {
                KeyGenerator keygen = KeyGenerator.getInstance("AES");
                SecureRandom random = new SecureRandom();
                keygen.init(random);
                SecretKey key = keygen.generateKey();
     
                //emballe avec la clé publique RSA
                ObjectInputStream keyIn = new ObjectInputStream(
                        new FileInputStream(keyFileName));
                Key publicKey = (Key)keyIn.readObject();
                keyIn.close();
     
                Cipher cipher = Cipher.getInstance("RSA");            
                cipher.init(Cipher.WRAP_MODE, publicKey);
                byte[] wrappedKey = cipher.wrap(key);
                DataOutputStream out = new DataOutputStream(
                        new FileOutputStream(outputFileName));
                out.writeInt(wrappedKey.length);
                out.write(wrappedKey);
     
                InputStream in = new FileInputStream(inputFileName);
                cipher = cipher.getInstance("AES");
                cipher.init(Cipher.ENCRYPT_MODE, key);
                crypt(in, out, cipher);
                in.close();
                out.close();
            } 
     
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
     
        /**
         * Decrytps the file inputFileName and saves the result in the file 
         * outputFileName, with the specified key keyFileName.
         * @param inputFileName the file to decrypt.
         * @param outputFileName the file to save the decrypting result in.
         * @param keyFileName the key to use to decrypt the file.
         **/
        public static void decryptFile(String inputFileName, String outputFileName, 
                String keyFileName)
        {
            try
            {
                DataInputStream in = new DataInputStream(
                        new FileInputStream(inputFileName));
                int length = in.readInt();
                byte[] wrappedKey = new byte[length];
                in.read(wrappedKey, 0, length);
     
                //déballe avec la clé RSA
                ObjectInputStream keyIn = new ObjectInputStream(
                        new FileInputStream(keyFileName));
                Key privateKey = (Key)keyIn.readObject();
                keyIn.close();
     
                Cipher cipher = Cipher.getInstance("RSA");
                cipher.init(Cipher.UNWRAP_MODE, privateKey);
                Key key = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);
     
                OutputStream out = new FileOutputStream(outputFileName);
                cipher = cipher.getInstance("AES");
                cipher.init(Cipher.DECRYPT_MODE, key);
     
                crypt(in, out, cipher);
                in.close();
                out.close();            
            } 
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
     
        }
     
        private static void crypt(InputStream in, OutputStream out, Cipher cipher)
            throws IOException, GeneralSecurityException
        {
            int blockSize = cipher.getBlockSize();
            int outputSize = cipher.getOutputSize(blockSize);
            byte[] inBytes = new byte[blockSize];
            byte[] outBytes = new byte[outputSize];
     
            int inLength = 0;
            boolean done = false;
            while(!done)
            {
                inLength = in.read(inBytes);
                if(inLength == blockSize)
                {
                    int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
                    out.write(outBytes, 0, outLength);
                }
                else
                    done = true;
            }
     
            if(inLength > 0)
                outBytes = cipher.doFinal(inBytes, 0, inLength);
            else
                outBytes = cipher.doFinal();
            out.write(outBytes);
        }
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    //Génération de clés : 
            RSAEncryptor.generateKeys("public key.key","private key.key");
     
            //Encryptage
    	RSAEncryptor.encryptFile("The Chronicles Of Narnia.avi","Encrypt.encrypt","public key.key");
     
            //Décryptage
    	RSAEncryptor.decryptFile("Encrypt.encrypt","Chronicles 2.avi"
    Merci pour vos suggestions.

    Aswat

  2. #2
    Membre éclairé Avatar de Razgriz
    Profil pro
    Professeur / chercheur en informatique / mathématiques
    Inscrit en
    Avril 2006
    Messages
    391
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations professionnelles :
    Activité : Professeur / chercheur en informatique / mathématiques

    Informations forums :
    Inscription : Avril 2006
    Messages : 391
    Par défaut
    Tu utilise la méthode getBytes() de la classe String, ça te renvoie un tableau de bytes et tu passe ça au cipher. Pour décrypter tu fait new String (decryptedbytes) et ça va mercher.

    Sinon beaucoup plus simple : j'ai posté une autre classe sympatique quicrypte bien (un stream de cryptage), elle se trouev ici.

    Y a l'input stream et l'output stream.
    Note : une amélioration serait d'écrire la clé AES en début de fichier crypté. Pour décryptae, il suffit de la décrypter avec la RSA et ensuite de décrypter le reste du fichier avec la clé AES.

    Tu n'as qu'à faire stream.writeObject(monString) et a marche.

  3. #3
    Membre averti
    Inscrit en
    Mai 2006
    Messages
    41
    Détails du profil
    Informations forums :
    Inscription : Mai 2006
    Messages : 41
    Par défaut
    Citation Envoyé par Razgriz
    Tu utilise la méthode getBytes() de la classe String, ça te renvoie un tableau de bytes et tu passe ça au cipher. Pour décrypter tu fait new String (decryptedbytes) et ça va mercher.

    Sinon beaucoup plus simple : j'ai posté une autre classe sympatique quicrypte bien (un stream de cryptage), elle se trouev ici.

    Y a l'input stream et l'output stream.
    Note : une amélioration serait d'écrire la clé AES en début de fichier crypté. Pour décryptae, il suffit de la décrypter avec la RSA et ensuite de décrypter le reste du fichier avec la clé AES.

    Tu n'as qu'à faire stream.writeObject(monString) et a marche.
    Attend je ne suis pas sur que tu est compris ce que je cherche a faire. En fait j'aimerais balader une chaine de caractère dans un cookie. Cette chaine doit être cryptée et décryptée. Les solutions que tu m'indiques semblent s'orienter vers le cryptage/décryptage de fichiers ce qui ne me sert pas vraiment. Il faudrait que j'utilise pour cela une clé coté serveur 1 et une autre clé coté appli (serveur 2) pour décoder.

    Je suis un peu paumé....

  4. #4
    Membre averti
    Inscrit en
    Mai 2006
    Messages
    41
    Détails du profil
    Informations forums :
    Inscription : Mai 2006
    Messages : 41
    Par défaut
    j'ai une classe qui fait presque ce que je veux. En fait j'aimerais remplacer le cryptage symétrique par un cryptage asymétrique. Et surtout j'aimerais pouvoir avoir une clé privé sur le serveur qui crypte et une clé publique sur le serveur qui decrypte

    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
    package com.atosorigin.opiam.auth.cas.security;
     
    import java.io.UnsupportedEncodingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.SecretKey;
     
    public class Encryption {
    	Cipher ecipher;
        Cipher dcipher;
     
        public Encryption(SecretKey key) {
        	try {
                ecipher = Cipher.getInstance("DES");
                dcipher = Cipher.getInstance("DES");
                ecipher.init(Cipher.ENCRYPT_MODE, key);
                dcipher.init(Cipher.DECRYPT_MODE, key);
     
            } catch (javax.crypto.NoSuchPaddingException e) {
            } catch (java.security.NoSuchAlgorithmException e) {
            } catch (java.security.InvalidKeyException e) {
            }
        }
     
        /**
         * @param str
         * @return
         */
        public String encrypt(String str) {
            try {
                // Encode the string into bytes using utf-8
                byte[] utf8 = str.getBytes("UTF8");
     
                // Encrypt
                byte[] enc = ecipher.doFinal(utf8);
     
                // Encode bytes to base64 to get a string
                return new sun.misc.BASE64Encoder().encode(enc);
            } catch (javax.crypto.BadPaddingException e) {
            } catch (IllegalBlockSizeException e) {
            } catch (UnsupportedEncodingException e) {
            }
            return null;
        }
     
        /**
         * @param str
         * @return
         */
        public String decrypt(String str) {
            try {
                // Decode base64 to get bytes
                byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
     
                // Decrypt
                byte[] utf8 = dcipher.doFinal(dec);
     
                // Decode using utf-8
                return new String(utf8, "UTF8");
            } catch (javax.crypto.BadPaddingException e) {
            } catch (IllegalBlockSizeException e) {
            } catch (UnsupportedEncodingException e) {
            } catch (java.io.IOException e) {
            }
            return null;
        }
    }

  5. #5
    Membre averti
    Inscrit en
    Mai 2006
    Messages
    41
    Détails du profil
    Informations forums :
    Inscription : Mai 2006
    Messages : 41
    Par défaut
    bon j'ai généré un couple de clé avec OpenSSL de cette manière :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    openssl genrsa -out private.der 2048 
    openssl rsa -in private.der -pubout -outform DER -out public.der
    ensuite j'essaye d'utiliser ma clé privé pour chiffrer :

    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
    package com.atosorigin.opiam.auth.cas.security;
     
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.security.KeyFactory;
    import java.security.NoSuchAlgorithmException;
    import java.security.PrivateKey;
    import java.security.spec.InvalidKeySpecException;
    import java.security.spec.PKCS8EncodedKeySpec;
    import java.security.spec.X509EncodedKeySpec;
     
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.NoSuchPaddingException;
     
    public class Main {
     
    	/**
             * @param args
             */
    	public static void main(String[] args) {
     
    		String passwordEnClair = "motDePasse";
    		String passwordCrypte = "vide";
    		File keyFile = new File("private.der");
    		byte[] encodedKey = new byte[(int)keyFile.length()];
     
    		try {
    			new FileInputStream(keyFile).read(encodedKey);
    		} catch (FileNotFoundException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		} catch (IOException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
     
    		PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedKey);
     
    		KeyFactory kf;
    		try {
    			kf = KeyFactory.getInstance("RSA");
    			PrivateKey pk;
    			try {
    				pk = kf.generatePrivate(privateKeySpec);
    			} catch (InvalidKeySpecException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}
    			Cipher rsa;
     
    			try {
    				rsa = Cipher.getInstance("RSA");
     
    				try {
    	                // Encode the string into bytes using utf-8
    	                byte[] utf8 = passwordEnClair.getBytes("UTF8");
     
    	                // Encrypt
    	                byte[] enc = rsa.doFinal(utf8);
     
    	                // Encode bytes to base64 to get a string
    	                passwordCrypte = new sun.misc.BASE64Encoder().encode(enc);
    	            } catch (javax.crypto.BadPaddingException e) {
    	            } catch (IllegalBlockSizeException e) {
    	            } catch (UnsupportedEncodingException e) {
    	            }
    			} catch (NoSuchAlgorithmException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			} catch (NoSuchPaddingException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}
     
    		} catch (NoSuchAlgorithmException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
    		System.out.println(passwordCrypte);
    	}
    }
    mais j'obtient cette erreur :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format
    Quelqun a déjàs essayé de faire ceci ?

  6. #6
    Membre averti
    Inscrit en
    Mai 2006
    Messages
    41
    Détails du profil
    Informations forums :
    Inscription : Mai 2006
    Messages : 41
    Par défaut
    Bon j'y suis presque, le cryptage fonctionne mais j'arrive pas a utiliser ma clé privé pour décrypter ...


    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
    package com.atosorigin.opiam.auth.cas.security;
     
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.security.InvalidKeyException;
    import java.security.KeyFactory;
    import java.security.NoSuchAlgorithmException;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    import java.security.spec.InvalidKeySpecException;
    import java.security.spec.PKCS8EncodedKeySpec;
    import java.security.spec.X509EncodedKeySpec;
     
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.NoSuchPaddingException;
     
    public class Main {
     
    	/**
             * @param args
             */
    	public static void main(String[] args) {
     
    		String passwordEnClair = "motDePasse";
    		String passwordCrypte = "vide";
    		String passwordDeCrypte = "vide";
     
    		File publicKeyFile = new File("public2.der");
    		byte[] encodedPublicKey = new byte[(int)publicKeyFile.length()];
     
    		try {
    			new FileInputStream(publicKeyFile).read(encodedPublicKey);
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
     
    		X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey);
     
    		KeyFactory kf;
    		try {
    			kf = KeyFactory.getInstance("RSA");
    			PublicKey pk;
    			try {
    				pk = kf.generatePublic(publicKeySpec);
    				Cipher rsa;
    				try {
    					rsa = Cipher.getInstance("RSA");
    					try {
    						rsa.init(Cipher.ENCRYPT_MODE, pk);
    					} catch (InvalidKeyException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    					try {
    			            // Encode the string into bytes using utf-8
    			            byte[] utf8 = passwordEnClair.getBytes("UTF8");
     
    			            // Encrypt
    			            byte[] enc = rsa.doFinal(utf8);
     
    			            // Encode bytes to base64 to get a string
    			            passwordCrypte =  new sun.misc.BASE64Encoder().encode(enc);
    			        } catch (javax.crypto.BadPaddingException e) {
    			        } catch (IllegalBlockSizeException e) {
    			        } catch (UnsupportedEncodingException e) {
    			        }
    				} catch (NoSuchAlgorithmException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (NoSuchPaddingException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			} catch (InvalidKeySpecException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		} catch (NoSuchAlgorithmException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		System.out.println(passwordCrypte);
     
    		//************************************************************************
    		//Décryptage
    		//************************************************************************
     
    		File privateKeyFile = new File("private2.pem");
    		byte[] encodedPrivateKey = new byte[(int)privateKeyFile.length()];
     
    		try {
    			new FileInputStream(privateKeyFile).read(encodedPrivateKey);
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
     
    		PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
     
    		KeyFactory kf2;
    		try {
    			kf2 = KeyFactory.getInstance("RSA");
    			PrivateKey pk2;
    			try {
    				pk2 = kf2.generatePrivate(privateKeySpec);
    				Cipher rsa2;
    				try {
    					rsa2 = Cipher.getInstance("RSA");
    					try {
    						rsa2.init(Cipher.DECRYPT_MODE, pk2);
    					} catch (InvalidKeyException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    					try {
    			            // Decode base64 to get bytes
    			            byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(passwordCrypte);
     
    			            // Decrypt
    			            byte[] utf8 = rsa2.doFinal(dec);
     
    			            // Decode using utf-8
    			            passwordDeCrypte = new String(utf8, "UTF8");
    			        } catch (javax.crypto.BadPaddingException e) {
    			        } catch (IllegalBlockSizeException e) {
    			        } catch (UnsupportedEncodingException e) {
    			        } catch (java.io.IOException e) {
    			        }
     
    				} catch (NoSuchAlgorithmException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (NoSuchPaddingException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			} catch (InvalidKeySpecException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		} catch (NoSuchAlgorithmException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		System.out.println("**********\nInitial: " + passwordEnClair + "\nCrypté: " + passwordCrypte + "\nDécrypté: " + passwordDeCrypte);
     
    	}
    }
    Par contre j'ai une exception :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format
    	at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:175)
    	at java.security.KeyFactory.generatePrivate(KeyFactory.java:322)
    	at com.atosorigin.opiam.auth.cas.security.Main.main(Main.java:116)
    Caused by: java.security.InvalidKeyException: invalid key format
    	at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:324)
    	at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:350)
    	at sun.security.rsa.RSAPrivateCrtKeyImpl.<init>(RSAPrivateCrtKeyImpl.java:74)
    	at sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(RSAPrivateCrtKeyImpl.java:58)
    	at sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java:274)
    	at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:171)
    	... 2 more
    Je comprend pas j'ai du me planter quelque part .... cette fois-ci j'ai testé en créant les clés de deux manières différentes :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    openssl genrsa -out private.der 2048 
    openssl rsa -in private.der -pubout -outform DER -out public.der
    et
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    openssl genrsa -out private2.pem 2048 
    openssl rsa -in private2.pem -pubout -outform DER -out public2.der
    Quelqun pour m'aider ??

    aswat

Discussions similaires

  1. Cryptage / decryptage xor
    Par deny dans le forum Débuter
    Réponses: 2
    Dernier message: 08/06/2008, 11h52
  2. Problème Cryptage / Decryptage
    Par Invité dans le forum C#
    Réponses: 6
    Dernier message: 19/05/2008, 16h51
  3. cryptage/decryptage, comment faire?
    Par bossun dans le forum Général Dotnet
    Réponses: 6
    Dernier message: 24/04/2008, 17h07
  4. Cryptage Decryptage asymetrique
    Par bslota dans le forum Sécurité
    Réponses: 5
    Dernier message: 02/05/2007, 16h15
  5. [VB]Cryptage/decryptage
    Par Tyrael62 dans le forum VB 6 et antérieur
    Réponses: 14
    Dernier message: 25/01/2006, 18h57

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