Pycrypto

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • luca72

    #1

    Pycrypto

    Hello
    I have to make an easy operation but reading the pycrypto doc. a never
    see AES example
    I have to cript this key 'ea523a664dabaa 4476d31226a1e3b ab0' with the
    AES.
    Can you help me for make it with pycrypto

    Regards Luca

  • wittempj@hotmail.com

    #2
    Re: Pycrypto

    luca72 wrote:[color=blue]
    > Hello
    > I have to make an easy operation but reading the pycrypto doc. a never
    > see AES example
    > I have to cript this key 'ea523a664dabaa 4476d31226a1e3b ab0' with the
    > AES.
    > Can you help me for make it with pycrypto
    >
    > Regards Luca[/color]

    You can do this as follows:

    py> from Crypto.Cipher import AES
    py> # key has to be 16, 24 or 32 bytes for AES
    py> crypt = AES.new('abcdef ghijklmnop', AES.MODE_ECB)
    # we're lucky, the string to encrypt is a multiple of 16 in length
    py> txt = 'ea523a664dabaa 4476d31226a1e3b ab0'
    py> c = crypt.encrypt(t xt)
    py> c
    'w\x81\xe3\xdd\ x066\x9eY\xc7\x ce~O\x9e\xfb\xe f\xfa\xb5\x8a\x ac\x7f\xca\x9fl {\xe5\xfd6\x80\ xe3\x81%\xb9'
    py> crypt.decrypt(c )
    'ea523a664dabaa 4476d31226a1e3b ab0'

    see http://www.amk.ca/python/writing/pycrypt for the docs. if you have
    to encrypt data which has not a multiple of length 16 you have to pad
    it e.g. with spaces, and then strip the decrypt() result.

    Comment

    • Laszlo Nagy

      #3
      Re: Pycrypto

      [color=blue]
      > You can do this as follows:
      >
      > py> from Crypto.Cipher import AES
      > py> # key has to be 16, 24 or 32 bytes for AES
      > py> crypt = AES.new('abcdef ghijklmnop', AES.MODE_ECB)
      > # we're lucky, the string to encrypt is a multiple of 16 in length
      > py> txt = 'ea523a664dabaa 4476d31226a1e3b ab0'
      > py> c = crypt.encrypt(t xt)
      > py> c
      > 'w\x81\xe3\xdd\ x066\x9eY\xc7\x ce~O\x9e\xfb\xe f\xfa\xb5\x8a\x ac\x7f\xca\x9fl {\xe5\xfd6\x80\ xe3\x81%\xb9'
      > py> crypt.decrypt(c )
      > 'ea523a664dabaa 4476d31226a1e3b ab0'
      >
      > see http://www.amk.ca/python/writing/pycrypt for the docs. if you have
      > to encrypt data which has not a multiple of length 16 you have to pad
      > it e.g. with spaces, and then strip the decrypt() result.
      >[/color]
      Or use CBC mode? I'm not familiar with pycrypto but I know that CBC mode
      can crypt/decrypt text with any size.

      Laszlo


      Comment

      • wittempj@hotmail.com

        #4
        Re: Pycrypto


        Laszlo Nagy wrote:[color=blue][color=green]
        > > You can do this as follows:
        > >
        > > py> from Crypto.Cipher import AES
        > > py> # key has to be 16, 24 or 32 bytes for AES
        > > py> crypt = AES.new('abcdef ghijklmnop', AES.MODE_ECB)
        > > # we're lucky, the string to encrypt is a multiple of 16 in length
        > > py> txt = 'ea523a664dabaa 4476d31226a1e3b ab0'
        > > py> c = crypt.encrypt(t xt)
        > > py> c
        > > 'w\x81\xe3\xdd\ x066\x9eY\xc7\x ce~O\x9e\xfb\xe f\xfa\xb5\x8a\x ac\x7f\xca\x9fl {\xe5\xfd6\x80\ xe3\x81%\xb9'
        > > py> crypt.decrypt(c )
        > > 'ea523a664dabaa 4476d31226a1e3b ab0'
        > >
        > > see http://www.amk.ca/python/writing/pycrypt for the docs. if you have
        > > to encrypt data which has not a multiple of length 16 you have to pad
        > > it e.g. with spaces, and then strip the decrypt() result.
        > >[/color]
        > Or use CBC mode? I'm not familiar with pycrypto but I know that CBC mode
        > can crypt/decrypt text with any size.
        >
        > Laszlo[/color]

        Not in this implementation:
        py> from Crypto.Cipher import AES
        py> crypt = AES.new('abcdef ghijklmnop', AES.MODE_CBC)
        py> c = crypt.encrypt(' 1')
        Traceback (most recent call last):
        File "<stdin>", line 1, in ?
        ValueError: Input strings must be a multiple of 16 in length

        Comment

        • luca72

          #5
          Re: Pycrypto


          Thanks

          Luca

          Comment

          • Laszlo Nagy

            #6
            Re: Pycrypto

            [color=blue]
            > Not in this implementation:
            > py> from Crypto.Cipher import AES
            > py> crypt = AES.new('abcdef ghijklmnop', AES.MODE_CBC)
            > py> c = crypt.encrypt(' 1')
            > Traceback (most recent call last):
            > File "<stdin>", line 1, in ?
            > ValueError: Input strings must be a multiple of 16 in length
            >[/color]
            This is strange. In theory, any ECB mode cipher can be used to create a
            CBC mode cipher.
            AFAIK, CBC creates one encrypted block, and uses the one byte from the
            plain text to xor it
            with the last encrypted byte. Finally it shifts the encrypted block.
            This way each input byte will
            have a corresponding output byte, and there is no size limit for the
            plain text.

            Frankly, I could write the CBC mode cipher using the (already existing)
            ECB cipher. Why we have this limitation?

            Laszlo


            Comment

            • luca72

              #7
              Re: Pycrypto


              Excuse me again,
              If the string is not a sting but hex number how i have to proced :

              look this page:


              Regards Luca

              Comment

              • Philippe Martin

                #8
                Re: Pycrypto

                Hi,

                Look at the bin2ascii module.

                Philippe


                luca72 wrote:
                [color=blue]
                >
                > Excuse me again,
                > If the string is not a sting but hex number how i have to proced :
                >
                > look this page:
                > http://www.cs.eku.edu/faculty/styer/...pt/JS-AES.html
                >
                > Regards Luca[/color]

                Comment

                • luca72

                  #9
                  Re: Pycrypto


                  Hello again i have solve doing this:

                  from Crypto.Cipher import AES
                  stri=(chr(int(' 9b',16))+chr(in t('d3',16))+chr (int('2d',16))+ chr(int('24',16 ))+chr(int('af' ,16))+chr(int(' c9',16))+chr(in t('e9',16))+chr (int('d7',16))+ chr(int('46',16 ))+chr(int('69' ,16))+chr(int(' 71',16))+chr(in t('32',16))+chr (int('45',16))+ chr(int('5f',16 ))+chr(int('27' ,16))+chr(int(' 0b',16)))
                  luca = str(stri)
                  crypt = AES.new(luca, AES.MODE_ECB)
                  testo=(chr(int( 'ea',16))+chr(i nt('52',16))+ch r(int('3a',16)) +chr(int('66',1 6))+chr(int('4d ',16))+chr(int( 'ab',16))+chr(i nt('aa',16))+ch r(int('44',16)) +chr(int('76',1 6))+chr(int('d3 ',16))+chr(int( '12',16))+chr(i nt('26',16))+ch r(int('a1',16)) +chr(int('e3',1 6))+chr(int('ba ',16))+chr(int( 'b0',16)))
                  testo = str(testo)
                  c = crypt.encrypt(t esto)

                  I don't know if this is the best way , but anyway it work

                  Regards

                  Luca

                  Comment

                  • Marc 'BlackJack' Rintsch

                    #10
                    Re: Pycrypto

                    In <1150470895.257 327.302460@h76g 2000cwa.googleg roups.com>, luca72 wrote:
                    [color=blue]
                    > Hello again i have solve doing this:
                    >
                    > from Crypto.Cipher import AES
                    > stri=(chr(int(' 9b',16))+chr(in t('d3',16))+chr (int('2d',16))+ chr(int('24',16 ))+chr(int('af' ,16))+chr(int(' c9',16))+chr(in t('e9',16))+chr (int('d7',16))+ chr(int('46',16 ))+chr(int('69' ,16))+chr(int(' 71',16))+chr(in t('32',16))+chr (int('45',16))+ chr(int('5f',16 ))+chr(int('27' ,16))+chr(int(' 0b',16)))
                    > luca = str(stri)
                    > crypt = AES.new(luca, AES.MODE_ECB)
                    > testo=(chr(int( 'ea',16))+chr(i nt('52',16))+ch r(int('3a',16)) +chr(int('66',1 6))+chr(int('4d ',16))+chr(int( 'ab',16))+chr(i nt('aa',16))+ch r(int('44',16)) +chr(int('76',1 6))+chr(int('d3 ',16))+chr(int( '12',16))+chr(i nt('26',16))+ch r(int('a1',16)) +chr(int('e3',1 6))+chr(int('ba ',16))+chr(int( 'b0',16)))
                    > testo = str(testo)
                    > c = crypt.encrypt(t esto)
                    >
                    > I don't know if this is the best way , but anyway it work[/color]

                    In [26]:import binascii

                    In [27]:binascii.unhex lify('ea523a664 dabaa4476d31226 a1e3bab0')
                    Out[27]:'\xeaR:fM\xab\ xaaDv\xd3\x12&\ xa1\xe3\xba\xb0 '

                    Ciao,
                    Marc 'BlackJack' Rintsch

                    Comment

                    • James Stroud

                      #11
                      Re: Pycrypto

                      Laszlo Nagy wrote:[color=blue]
                      >[color=green]
                      >> Not in this implementation:
                      >> py> from Crypto.Cipher import AES
                      >> py> crypt = AES.new('abcdef ghijklmnop', AES.MODE_CBC)
                      >> py> c = crypt.encrypt(' 1')
                      >> Traceback (most recent call last):
                      >> File "<stdin>", line 1, in ?
                      >> ValueError: Input strings must be a multiple of 16 in length
                      >>[/color]
                      >
                      > This is strange. In theory, any ECB mode cipher can be used to create a
                      > CBC mode cipher.
                      > AFAIK, CBC creates one encrypted block, and uses the one byte from the
                      > plain text to xor it
                      > with the last encrypted byte. Finally it shifts the encrypted block.
                      > This way each input byte will
                      > have a corresponding output byte, and there is no size limit for the
                      > plain text.
                      >
                      > Frankly, I could write the CBC mode cipher using the (already existing)
                      > ECB cipher. Why we have this limitation?
                      >
                      > Laszlo
                      >
                      >[/color]

                      CBC mode is cipher block chaining, so it still works as a block cipher,
                      which means that it must be that len(text) % block_size == 0. In other
                      words, CBC does not shift by one byte but by block_size bytes. See:



                      James

                      --
                      James Stroud
                      UCLA-DOE Institute for Genomics and Proteomics
                      Box 951570
                      Los Angeles, CA 90095


                      Comment

                      • luca72

                        #12
                        Re: Pycrypto


                        [color=blue]
                        > In [26]:import binascii
                        >
                        > In [27]:binascii.unhex lify('ea523a664 dabaa4476d31226 a1e3bab0')
                        > Out[27]:'\xeaR:fM\xab\ xaaDv\xd3\x12&\ xa1\xe3\xba\xb0 '
                        >
                        > Ciao,
                        > Marc 'BlackJack' Rintsch[/color]

                        Ciao Marc

                        Grazie Thanks

                        Comment

                        • K.S.Sreeram

                          #13
                          Re: Pycrypto

                          wittempj@hotmai l.com wrote:[color=blue]
                          > ValueError: Input strings must be a multiple of 16 in length[/color]

                          As James Stroud noted, a CBC mode cipher is still a block cipher, and
                          the input *must* be a multiple of the block size.

                          OpenSSL provides a standard padding mechanism so that there are no input
                          size limitations for any cipher.

                          Have a look at http://tachyon.in/ncrypt/
                          It provides access to the OpenSSL ciphers (including padding).

                          Regards
                          Sreeram


                          -----BEGIN PGP SIGNATURE-----
                          Version: GnuPG v1.4.2.2 (MingW32)
                          Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

                          iD8DBQFElAdqrgn 0plK5qqURAuN3AK CIXvFZAf+QwNrkP L3Ha08rLrb7PgCf UtBj
                          /V9NX16Jw01FvOSJ YCUWZuA=
                          =Q6hN
                          -----END PGP SIGNATURE-----

                          Comment

                          • luca72

                            #14
                            Re: Pycrypto

                            Hello again

                            You know if is possible save all the encryption process in a text file
                            and not only the result?

                            This will be wery helpful for compare the pycrypto step by step
                            operation, with the hand made operation and see where hand made make a
                            mistake

                            Comment

                            Working...