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

Java Discussion :

[String][ASCII]Java / Python


Sujet :

Java

  1. #1
    Futur Membre du Club
    Inscrit en
    Février 2005
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Février 2005
    Messages : 12
    Points : 9
    Points
    9
    Par défaut [String][ASCII]Java / Python
    Salut !

    J'ai un problème entre la correspondance ACII en Java et en python.
    (c'est peut-être une histoire d'unicode)

    Voilà, en python, donne ,
    alors qu'en java, donne .

    Du coup, si je crypte dans un language, je n'obtient pas le bon résultat en décryptant dans un autre.

    Est-ce que quelqu'un sait comment je pourrais avoir une bonne correspondance ?
    Merci

  2. #2
    Membre confirmé Avatar de benratti
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    471
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Mai 2004
    Messages : 471
    Points : 649
    Points
    649
    Par défaut
    Normalement, ASCII n'est-il pas sense etre un standart ??? De plus, il faut faire gaffe car par default, il me semble que java utilise unicode pour les caracteres ( mais bon, je peux me planter ).

  3. #3
    Membre éprouvé
    Profil pro
    Architecte technique
    Inscrit en
    Mars 2002
    Messages
    966
    Détails du profil
    Informations personnelles :
    Âge : 51
    Localisation : France

    Informations professionnelles :
    Activité : Architecte technique

    Informations forums :
    Inscription : Mars 2002
    Messages : 966
    Points : 1 085
    Points
    1 085
    Par défaut
    Oui ASCII C'est standard, c'est le jeu de caractères étendus dont tu parles.

    ASCII c'est de 0 à 127 je crois...

  4. #4
    Futur Membre du Club
    Inscrit en
    Février 2005
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Février 2005
    Messages : 12
    Points : 9
    Points
    9
    Par défaut
    ok, c standard, et ça m'arrange, mais alors :
    - soit java utilise unicode, dans ce cas comment utiliser le mm format qu'avec Python ?
    - soit '\xc2' correspond à Â dans un autre format, et ... la question est la mm : comment utiliser le mm format ?

  5. #5
    Membre éprouvé
    Profil pro
    Architecte technique
    Inscrit en
    Mars 2002
    Messages
    966
    Détails du profil
    Informations personnelles :
    Âge : 51
    Localisation : France

    Informations professionnelles :
    Activité : Architecte technique

    Informations forums :
    Inscription : Mars 2002
    Messages : 966
    Points : 1 085
    Points
    1 085
    Par défaut
    Utilises Unicode...

  6. #6
    Futur Membre du Club
    Inscrit en
    Février 2005
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Février 2005
    Messages : 12
    Points : 9
    Points
    9
    Par défaut
    hehe, jveux bien, mais je vois pas comment !

  7. #7
    Futur Membre du Club
    Inscrit en
    Février 2005
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Février 2005
    Messages : 12
    Points : 9
    Points
    9
    Par défaut
    Une autre petite question :

    Savez-vous comment je peut obtenir un résultat du style \xc2 en Java ?

  8. #8
    Membre habitué
    Avatar de Tifauv'
    Profil pro
    Inscrit en
    Mars 2002
    Messages
    102
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2002
    Messages : 102
    Points : 129
    Points
    129
    Par défaut
    Quand tu chiffres, tu obtiens un byte[] que tu encodes en Base64 (c'est fait pour ça).
    De l'autre côté, du décodes le Base64 en byte[] et le tour est joué.

    Voilà un exemple de classe de conversion tirée de BoucyCastle :
    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
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    import java.io.UnsupportedEncodingException;
     
    /**
     * The <code>Base64</code> is the entry point for a Base64 encoder/decoder.
     * 
     * @version 1.0
     * 
     * @author Extract from BouncyCastle
     */
     public class Base64
    {
    	private static final byte[] encodingTable =
    		{
    		    (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G',
                (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N',
                (byte)'O', (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U',
                (byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z',
    		    (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g',
                (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n',
                (byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u',
                (byte)'v',
    		    (byte)'w', (byte)'x', (byte)'y', (byte)'z',
    		    (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6',
                (byte)'7', (byte)'8', (byte)'9',
    		    (byte)'+', (byte)'/'
    		};
     
    	/**
             * encode the input data producong a base 64 encoded byte array.
             *
             * @return a String containing the base 64 encoded data.
             */
    	private static String lcl_encode(
    		byte[]	data, String pemType)
    	{
    		byte[]	bytes;
     
    		int modulus = data.length % 3;
    		int	add = 0;
    		if (pemType != null)
    		{
    			if (pemType.length() > 0)
    			{
    				add = data.length / 45;
    				add += 17;	/* ----BEGIN ...-----\n */
    				add += pemType.length();
    				add += 16;	/* \n----END ...-----\n */
    				add += pemType.length();
    			}
    		}
    		if (modulus == 0)
    		{
    			bytes = new byte[(4 * data.length / 3) + add];
    		}
    		else
    		{
    			bytes = new byte[(4 * ((data.length / 3) + 1)) + add];
    		}
     
            int dataLength = (data.length - modulus);
    		int j, a1, a2, a3;
    		j = 0;		
    		if (add != 0)
    		{
    			byte []
    				btmp;
    			String
    				stmp;
    			stmp = "-----BEGIN " + pemType + "-----";
    			btmp = stmp.getBytes();
    			for (j = 0; j < btmp.length; j++)
    				bytes[j] = btmp[j];
    		}
    		for (int i = 0; i < dataLength; i += 3, j += 4)
    		{
    			a1 = data[i] & 0xff;
    			a2 = data[i + 1] & 0xff;
    			a3 = data[i + 2] & 0xff;
     
    			if (add != 0 && (i % 45) == 0)
    				bytes[j++] = 0x0a;
    			bytes[j] = encodingTable[(a1 >>> 2) & 0x3f];
    			bytes[j + 1] = encodingTable[((a1 << 4) | (a2 >>> 4)) & 0x3f];
    			bytes[j + 2] = encodingTable[((a2 << 2) | (a3 >>> 6)) & 0x3f];
    			bytes[j + 3] = encodingTable[a3 & 0x3f];
    		}
     
    		/*
    		 * process the tail end.
    		 */
    		int	b1, b2, b3;
    		int	d1, d2;
     
    		switch (modulus)
    		{
    		case 0:		/* nothing left to do */
    			break;
    		case 1:
    			d1 = data[data.length - 1] & 0xff;
    			b1 = (d1 >>> 2) & 0x3f;
    			b2 = (d1 << 4) & 0x3f;
     
    			bytes[j++] = encodingTable[b1];
    			bytes[j++] = encodingTable[b2];
    			bytes[j++] = (byte)'=';
    			bytes[j++] = (byte)'=';
    			break;
    		case 2:
    			d1 = data[data.length - 2] & 0xff;
    			d2 = data[data.length - 1] & 0xff;
     
    			b1 = (d1 >>> 2) & 0x3f;
    			b2 = ((d1 << 4) | (d2 >>> 4)) & 0x3f;
    			b3 = (d2 << 2) & 0x3f;
     
    			bytes[j++] = encodingTable[b1];
    			bytes[j++] = encodingTable[b2];
    			bytes[j++] = encodingTable[b3];
    			bytes[j++] = (byte)'=';
    			break;
    		}
     
    		if (add != 0)
    		{
    			byte []
    				btmp;
    			String
    				stmp;
    			stmp = "\n-----END " + pemType + "-----\n";
    			btmp = stmp.getBytes();
    			for (a1 = 0; a1 < btmp.length; a1++, j++)
    				bytes[j] = btmp[a1];
    		}
     
    		return new String (bytes);
    	}
     
    	/**
             * encode the input data producong a base 64 encoded byte array.
             *
             * @return a String containing the base 64 encoded data.
             */
    	private static String lcl_encode(
    		String	data, String pemType)
    	{
    		byte []
    			b;
     
    		try {
    		    /*
    		     * We really do want ASCII7 -- the http protocol doesn't change
    		     * with locale.
    		     */
    		    b = data.getBytes("ASCII7");
    		}
    		catch (UnsupportedEncodingException ignored) {
    		    /*
    		     * If ASCII7 isn't there, something serious is wrong, but
    		     * Paranoia Is Good (tm)
    		     */
    		    b = data.getBytes();
    		}
    		return lcl_encode(b, pemType);
    	}
     
    	public static String encode(
    		byte[]	data)
    	{
    		return lcl_encode(data, null);
    	}
     
    	public static String encode(
    		String	data)
    	{
    		return lcl_encode(data, null);
    	}
     
    	public static String der2pem(
    		byte[]	data, String pemType)
    	{
    		return lcl_encode(data, pemType);
    	}
     
    	/*
    	 * set up the decoding table.
    	 */
    	private static final byte[] decodingTable;
     
    	static
    	{
    		decodingTable = new byte[128];
     
    		for (int i = 'A'; i <= 'Z'; i++)
    		{
    			decodingTable[i] = (byte)(i - 'A');
    		}
     
    		for (int i = 'a'; i <= 'z'; i++)
    		{
    			decodingTable[i] = (byte)(i - 'a' + 26);
    		}
     
    		for (int i = '0'; i <= '9'; i++)
    		{
    			decodingTable[i] = (byte)(i - '0' + 52);
    		}
     
    		decodingTable['+'] = 62;
    		decodingTable['/'] = 63;
    	}
     
    	/**
             * decode the base 64 encoded input data.
             *
             * @return a byte array representing the decoded data.
             */
    	public static byte[] decode(
    		byte[]	data)
    	{
    		byte[]	bytes;
    		byte	b1, b2, b3, b4;
     
    		if (data[data.length - 2] == '=')
    		{
    			bytes = new byte[(((data.length / 4) - 1) * 3) + 1];
    		}
    		else if (data[data.length - 1] == '=')
    		{
    			bytes = new byte[(((data.length / 4) - 1) * 3) + 2];
    		}
    		else
    		{
    			bytes = new byte[((data.length / 4) * 3)];
    		}
     
    		for (int i = 0, j = 0; i < data.length - 4; i += 4, j += 3)
    		{
    			b1 = decodingTable[data[i]];
    			b2 = decodingTable[data[i + 1]];
    			b3 = decodingTable[data[i + 2]];
    			b4 = decodingTable[data[i + 3]];
     
    			bytes[j] = (byte)((b1 << 2) | (b2 >> 4));
    			bytes[j + 1] = (byte)((b2 << 4) | (b3 >> 2));
    			bytes[j + 2] = (byte)((b3 << 6) | b4);
    		}
     
    		if (data[data.length - 2] == '=')
    		{
    			b1 = decodingTable[data[data.length - 4]];
    			b2 = decodingTable[data[data.length - 3]];
     
    			bytes[bytes.length - 1] = (byte)((b1 << 2) | (b2 >> 4));
    		}
    		else if (data[data.length - 1] == '=')
    		{
    			b1 = decodingTable[data[data.length - 4]];
    			b2 = decodingTable[data[data.length - 3]];
    			b3 = decodingTable[data[data.length - 2]];
     
    			bytes[bytes.length - 2] = (byte)((b1 << 2) | (b2 >> 4));
    			bytes[bytes.length - 1] = (byte)((b2 << 4) | (b3 >> 2));
    		}
    		else
    		{
    			b1 = decodingTable[data[data.length - 4]];
    			b2 = decodingTable[data[data.length - 3]];
    			b3 = decodingTable[data[data.length - 2]];
    			b4 = decodingTable[data[data.length - 1]];
     
    			bytes[bytes.length - 3] = (byte)((b1 << 2) | (b2 >> 4));
    			bytes[bytes.length - 2] = (byte)((b2 << 4) | (b3 >> 2));
    			bytes[bytes.length - 1] = (byte)((b3 << 6) | b4);
    		}
     
    		return bytes;
    	}
     
    	/**
             * decode the base 64 encoded String data.
             *
             * @return a byte array representing the decoded data.
             */
    	public static byte[] decode(
    		String	data)
    	{
    		byte []
    			b;
     
    		try {
    		    /*
    		     * We really do want ASCII7 -- the http protocol doesn't change
    		     * with locale.
    		     */
    		    b = data.getBytes("ASCII7");
    		}
    		catch (UnsupportedEncodingException ignored) {
    		    /*
    		     * If ASCII7 isn't there, something serious is wrong, but
    		     * Paranoia Is Good (tm)
    		     */
    		    b = data.getBytes();
    		}
    		return decode(b);
    	}
    }
    - Un pointeur, c'est comme un fusil chargé mal reglé avec la gachette qui s'appuie toute seule des fois.
    - Nan nan nan ça c'est le C. Un pointeur, c'est la même chose, mais avec le Quad Damage.

  9. #9
    Futur Membre du Club
    Inscrit en
    Février 2005
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Février 2005
    Messages : 12
    Points : 9
    Points
    9
    Par défaut
    pas tout compris au code, mais merci !

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

Discussions similaires

  1. [CDI] [CDD] Strasbourg Développeur PHP, Java, Python etc
    Par Kennel sébastien dans le forum Demandes
    Réponses: 0
    Dernier message: 22/09/2008, 20h00
  2. transformation de String to java.sql.date
    Par opensource dans le forum Langage
    Réponses: 7
    Dernier message: 07/03/2008, 17h23
  3. declaration string dans java
    Par kouki-raid dans le forum Langage
    Réponses: 15
    Dernier message: 29/08/2007, 08h52
  4. Lequel me conviendrait ? Java , Python ou OCaml
    Par kedare dans le forum Langages de programmation
    Réponses: 13
    Dernier message: 25/06/2006, 19h26
  5. Comment convertir de l'hexadecimale au string ASCII ?
    Par Battosaiii dans le forum Algorithmes et structures de données
    Réponses: 1
    Dernier message: 17/03/2006, 19h04

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