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

Langage Java Discussion :

String en InputStream


Sujet :

Langage Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Mars 2008
    Messages
    41
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Mars 2008
    Messages : 41
    Par défaut String en InputStream
    Bonjour,
    je besoin de votre aide voilà, j'ai un problème, j' utilise une méthode avec un argument de type InputStream :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    public static void toto(InputStream in, OutputStream out){
    .......}
    et là je passe les paramétres en appellant comme ça: toto( System.in, System.out); donc je rentre mes params dans la console, alors que je veux les entrées directement dans le code en dur, mais je ne veux pas passer par un fichier pour avoir un objet de type InputStream avec un méthode read() pour les lire, est-ce que c'est possible??? J'ai essayé ça Reader r = new StringReader("abc"); mais sa ne marche pas.
    Merci.

  2. #2
    Membre éclairé Avatar de Diablo_22
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2005
    Messages
    498
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Maine et Loire (Pays de la Loire)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Février 2005
    Messages : 498
    Par défaut
    Ah ce que j'ai compris

    tu ne veux plus passer par la console mais directement passé tes données par le code

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    public static void toto(InputStream in, OutputStream out){
    .......}
    
    Devient :
    
    public static String toto(String sDonneeEntree)
    {
       String sDonneeSortie ="";
       .....[TON TRAITEMENT]
       return sDonneeSortie;
    }

  3. #3
    Membre Expert

    Homme Profil pro
    Architecte logiciel
    Inscrit en
    Novembre 2006
    Messages
    1 252
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Architecte logiciel
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Novembre 2006
    Messages : 1 252
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
      String s = "toto";
      InputStream s = new ByteArrayInputStream( s.getBytes() );

  4. #4
    Membre averti
    Profil pro
    Inscrit en
    Mars 2008
    Messages
    41
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Mars 2008
    Messages : 41
    Par défaut
    Bonjour,

    Au fait je voulais faire une fonction qui crypte et décrypte en triple DES, j'ai trouvé ça sur java2s
    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
    * Copyright (c) 2000 David Flanagan.  All rights reserved.
     * This code is from the book Java Examples in a Nutshell, 2nd Edition.
     * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
     * You may study, use, and modify it for any non-commercial purpose.
     * You may distribute it non-commercially as long as you retain this notice.
     * For a commercial use license, or to purchase the book (recommended),
     * visit http://www.davidflanagan.com/javaexamples2.
     */
    
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    import java.security.Provider;
    import java.security.Security;
    import java.security.spec.InvalidKeySpecException;
    
    import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.CipherOutputStream;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.KeyGenerator;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESedeKeySpec;
    
    /**
     * This class defines methods for encrypting and decrypting using the Triple DES
     * algorithm and for generating, reading and writing Triple DES keys. It also
     * defines a main() method that allows these methods to be used from the command
     * line.
     */
    public class TripleDES {
      /**
       * The program. The first argument must be -e, -d, or -g to encrypt,
       * decrypt, or generate a key. The second argument is the name of a file
       * from which the key is read or to which it is written for -g. The -e and
       * -d arguments cause the program to read from standard input and encrypt or
       * decrypt to standard output.
       */
      public static void main(String[] args) {
        try {
          // Check to see whether there is a provider that can do TripleDES
          // encryption. If not, explicitly install the SunJCE provider.
          try {
            Cipher c = Cipher.getInstance("DESede");
          } catch (Exception e) {
            // An exception here probably means the JCE provider hasn't
            // been permanently installed on this system by listing it
            // in the $JAVA_HOME/jre/lib/security/java.security file.
            // Therefore, we have to install the JCE provider explicitly.
            System.err.println("Installing SunJCE provider.");
            Provider sunjce = new com.sun.crypto.provider.SunJCE();
            Security.addProvider(sunjce);
          }
    
          // This is where we'll read the key from or write it to
          File keyfile = new File(args[1]);
    
          // Now check the first arg to see what we're going to do
          if (args[0].equals("-g")) { // Generate a key
            System.out.print("Generating key. This may take some time...");
            System.out.flush();
            SecretKey key = generateKey();
            writeKey(key, keyfile);
            System.out.println("done.");
            System.out.println("Secret key written to " + args[1]
                + ". Protect that file carefully!");
          } else if (args[0].equals("-e")) { // Encrypt stdin to stdout
            SecretKey key = readKey(keyfile);
            encrypt(key, System.in, System.out);
          } else if (args[0].equals("-d")) { // Decrypt stdin to stdout
            SecretKey key = readKey(keyfile);
            decrypt(key, System.in, System.out);
          }
        } catch (Exception e) {
          System.err.println(e);
          System.err.println("Usage: java " + TripleDES.class.getName()
              + " -d|-e|-g <keyfile>");
        }
      }
    
      /** Generate a secret TripleDES encryption/decryption key */
      public static SecretKey generateKey() throws NoSuchAlgorithmException {
        // Get a key generator for Triple DES (a.k.a DESede)
        KeyGenerator keygen = KeyGenerator.getInstance("DESede");
        keygen.init(168);
        // Use it to generate a key
        return keygen.generateKey();
      }
    
      /** Save the specified TripleDES SecretKey to the specified file */
      public static void writeKey(SecretKey key, File f) throws IOException,
          NoSuchAlgorithmException, InvalidKeySpecException {
        // Convert the secret key to an array of bytes like this
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
        DESedeKeySpec keyspec = (DESedeKeySpec) keyfactory.getKeySpec(key,
            DESedeKeySpec.class);
        byte[] rawkey = keyspec.getKey();
    
        // Write the raw key to the file
        FileOutputStream out = new FileOutputStream(f);
        out.write(rawkey);
        out.close();
      }
    
      /** Read a TripleDES secret key from the specified file */
      public static SecretKey readKey(File f) throws IOException,
          NoSuchAlgorithmException, InvalidKeyException,
          InvalidKeySpecException {
        // Read the raw bytes from the keyfile
        DataInputStream in = new DataInputStream(new FileInputStream(f));
        byte[] rawkey = new byte[(int) f.length()];
        in.readFully(rawkey);
        in.close();
    
        // Convert the raw bytes to a secret key like this
        DESedeKeySpec keyspec = new DESedeKeySpec(rawkey);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
        SecretKey key = keyfactory.generateSecret(keyspec);
        return key;
      }
    
      /**
       * Use the specified TripleDES key to encrypt bytes from the input stream
       * and write them to the output stream. This method uses CipherOutputStream
       * to perform the encryption and write bytes at the same time.
       */
      public static void encrypt(SecretKey key, InputStream in, OutputStream out)
          throws NoSuchAlgorithmException, InvalidKeyException,
          NoSuchPaddingException, IOException {
        // Create and initialize the encryption engine
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, key);
    
        // Create a special output stream to do the work for us
        CipherOutputStream cos = new CipherOutputStream(out, cipher);
    ici
        // Read from the input and write to the encrypting output stream
        byte[] buffer = new byte[2048];
        int bytesRead;
        while ((bytesRead = in.read(buffer)) != -1) {
          cos.write(buffer, 0, bytesRead);
        }
        cos.close();
    
        // For extra security, don't leave any plaintext hanging around memory.
        java.util.Arrays.fill(buffer, (byte) 0);
      }
    
      /**
       * Use the specified TripleDES key to decrypt bytes ready from the input
       * stream and write them to the output stream. This method uses uses Cipher
       * directly to show how it can be done without CipherInputStream and
       * CipherOutputStream.
       */
      public static void decrypt(SecretKey key, InputStream in, OutputStream out)
          throws NoSuchAlgorithmException, InvalidKeyException, IOException,
          IllegalBlockSizeException, NoSuchPaddingException,
          BadPaddingException {
        // Create and initialize the decryption engine
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.DECRYPT_MODE, key);
    
        // Read bytes, decrypt, and write them out.
        byte[] buffer = new byte[2048];
        int bytesRead;
    ici
        while ((bytesRead = in.read(buffer)) != -1) {
          out.write(cipher.update(buffer, 0, bytesRead));
        }
    
        // Write out the final bunch of decrypted bytes
        out.write(cipher.doFinal());
        out.flush();
      }
    }
    Et il me fallait cryter des données à mettre dans BD c'est pour cela que j'avais besion de rentrer des données via le code et qui plus est une String facilement à stocker dans une BD Mysql que un type byte[].
    Voilà tout.
    Merci à Tommy31, ça à marcher je peux le faire directement dans le code garce à
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    String s = "toto";
        InputStream in = new ByteArrayInputStream( s.getBytes() );

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. String or InputStream??
    Par soeursourire dans le forum Langage
    Réponses: 2
    Dernier message: 05/05/2006, 16h13
  2. [InputStream]Convertir un String en Stream
    Par maximus001ma dans le forum Entrée/Sortie
    Réponses: 2
    Dernier message: 28/12/2005, 18h21
  3. [Conversion] String en InputStream
    Par canou94 dans le forum Langage
    Réponses: 5
    Dernier message: 22/02/2005, 15h04
  4. [String][InputStream]Conversion de l'un a l'autre
    Par 2000 dans le forum Entrée/Sortie
    Réponses: 2
    Dernier message: 14/10/2004, 12h03
  5. Transformer un String en InputStream
    Par felix79 dans le forum Entrée/Sortie
    Réponses: 5
    Dernier message: 07/07/2004, 12h18

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