Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I found the below code online, but in its original state, the encryption was not using any padding (Padding.None), and was encrypting in blocks of 8. I wasn't able to get this code to properly encrypt / decrypt data so I was forced to have it encrypt the full length of the text. Is there something fundamentally wrong with what I have done? If so, could someone please show me the proper way to encrypt text in blocks of 8?

// 168-bit (three-key) 3DES (Triple-DES) encrypt a single 8-byte block (ECB mode)
// plain-text should be 8-bytes, key should be 24 bytes.
public byte[] TripleDesEncryptOneBlock(byte[] plainText, byte[] key)
{
    // Create a new 3DES key.
    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    // Set the KeySize = 192 for 168-bit DES encryption.
    // The msb of each byte is a parity bit, so the key length is actually 168 bits.
    des.KeySize = 192;
    des.Key = key;
    des.Mode = CipherMode.ECB;
    des.Padding = PaddingMode.None;
    ICryptoTransform ic = des.CreateEncryptor();
    byte[] enc = ic.TransformFinalBlock(plainText, 0, 8);
    return enc;
}
public byte[] TripleDesDecryptBlock(byte[] plainText, byte[] key)
{
    // Create a new 3DES key.
    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    // Set the KeySize = 192 for 168-bit DES encryption.
    // The msb of each byte is a parity bit, so the key length is actually 168 bits.
    des.KeySize = 192;
    des.Key = key;
    des.Mode = CipherMode.ECB;
    des.Padding = PaddingMode.None;
    ICryptoTransform ic = des.CreateDecryptor();
    byte[] dec = ic.TransformFinalBlock(plainText, 0, 8);
    return dec;
}
// Encrypt Text
textBox5.Text = ByteToString(TripleDesEncryptOneBlock(StringToByte(textBox5.Text),  
    StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));
// Decrypt Text
textBox5.Text = ByteToString(TripleDesDecryptBlock(StringToByte(textBox5.Text),  
    StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));
[My Related question on stack overflow](httphttps://stackoverflow.com/questions/6990286/triple-des-encryption)

I found the below code online, but in its original state, the encryption was not using any padding (Padding.None), and was encrypting in blocks of 8. I wasn't able to get this code to properly encrypt / decrypt data so I was forced to have it encrypt the full length of the text. Is there something fundamentally wrong with what I have done? If so, could someone please show me the proper way to encrypt text in blocks of 8?

// 168-bit (three-key) 3DES (Triple-DES) encrypt a single 8-byte block (ECB mode)
// plain-text should be 8-bytes, key should be 24 bytes.
public byte[] TripleDesEncryptOneBlock(byte[] plainText, byte[] key)
{
    // Create a new 3DES key.
    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    // Set the KeySize = 192 for 168-bit DES encryption.
    // The msb of each byte is a parity bit, so the key length is actually 168 bits.
    des.KeySize = 192;
    des.Key = key;
    des.Mode = CipherMode.ECB;
    des.Padding = PaddingMode.None;
    ICryptoTransform ic = des.CreateEncryptor();
    byte[] enc = ic.TransformFinalBlock(plainText, 0, 8);
    return enc;
}
public byte[] TripleDesDecryptBlock(byte[] plainText, byte[] key)
{
    // Create a new 3DES key.
    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    // Set the KeySize = 192 for 168-bit DES encryption.
    // The msb of each byte is a parity bit, so the key length is actually 168 bits.
    des.KeySize = 192;
    des.Key = key;
    des.Mode = CipherMode.ECB;
    des.Padding = PaddingMode.None;
    ICryptoTransform ic = des.CreateDecryptor();
    byte[] dec = ic.TransformFinalBlock(plainText, 0, 8);
    return dec;
}
// Encrypt Text
textBox5.Text = ByteToString(TripleDesEncryptOneBlock(StringToByte(textBox5.Text),  
    StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));
// Decrypt Text
textBox5.Text = ByteToString(TripleDesDecryptBlock(StringToByte(textBox5.Text),  
    StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));
[My Related question on stack overflow](http://stackoverflow.com/questions/6990286/triple-des-encryption)

I found the below code online, but in its original state, the encryption was not using any padding (Padding.None), and was encrypting in blocks of 8. I wasn't able to get this code to properly encrypt / decrypt data so I was forced to have it encrypt the full length of the text. Is there something fundamentally wrong with what I have done? If so, could someone please show me the proper way to encrypt text in blocks of 8?

// 168-bit (three-key) 3DES (Triple-DES) encrypt a single 8-byte block (ECB mode)
// plain-text should be 8-bytes, key should be 24 bytes.
public byte[] TripleDesEncryptOneBlock(byte[] plainText, byte[] key)
{
    // Create a new 3DES key.
    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    // Set the KeySize = 192 for 168-bit DES encryption.
    // The msb of each byte is a parity bit, so the key length is actually 168 bits.
    des.KeySize = 192;
    des.Key = key;
    des.Mode = CipherMode.ECB;
    des.Padding = PaddingMode.None;
    ICryptoTransform ic = des.CreateEncryptor();
    byte[] enc = ic.TransformFinalBlock(plainText, 0, 8);
    return enc;
}
public byte[] TripleDesDecryptBlock(byte[] plainText, byte[] key)
{
    // Create a new 3DES key.
    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    // Set the KeySize = 192 for 168-bit DES encryption.
    // The msb of each byte is a parity bit, so the key length is actually 168 bits.
    des.KeySize = 192;
    des.Key = key;
    des.Mode = CipherMode.ECB;
    des.Padding = PaddingMode.None;
    ICryptoTransform ic = des.CreateDecryptor();
    byte[] dec = ic.TransformFinalBlock(plainText, 0, 8);
    return dec;
}
// Encrypt Text
textBox5.Text = ByteToString(TripleDesEncryptOneBlock(StringToByte(textBox5.Text),  
    StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));
// Decrypt Text
textBox5.Text = ByteToString(TripleDesDecryptBlock(StringToByte(textBox5.Text),  
    StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));
[My Related question on stack overflow](https://stackoverflow.com/questions/6990286/triple-des-encryption)
added 134 characters in body; edited tags
Source Link
Cryptographeur
  • 4.3k
  • 2
  • 29
  • 40

I found the below code online, but in its original state, the encryption was not using any padding (Padding.None), and was encrypting in blocks of 8. I wasn't able to get this code to properly encrypt / decrypt data so I was forced to have it encrypt the full length of the text. Is there something fundementallyfundamentally wrong with what I have done? If so, could someone please show me the propperproper way to encrypt text in blocks of 8?

// 168-bit (three-key) 3DES (Triple-DES) encrypt a single 8-byte block (ECB mode)
// plain-text should be 8-bytes, key should be 24 bytes.
public byte[] TripleDesEncryptOneBlock(byte[] plainText, byte[] key)
{
    // Create a new 3DES key.
    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    // Set the KeySize = 192 for 168-bit DES encryption.
    // The msb of each byte is a parity bit, so the key length is actually 168 bits.
    des.KeySize = 192;
    des.Key = key;
    des.Mode = CipherMode.ECB;
    des.Padding = PaddingMode.None;
    ICryptoTransform ic = des.CreateEncryptor();
    byte[] enc = ic.TransformFinalBlock(plainText, 0, 8);
    return enc;
}
public byte[] TripleDesDecryptBlock(byte[] plainText, byte[] key)
{
    // Create a new 3DES key.
    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    // Set the KeySize = 192 for 168-bit DES encryption.
    // The msb of each byte is a parity bit, so the key length is actually 168 bits.
    des.KeySize = 192;
    des.Key = key;
    des.Mode = CipherMode.ECB;
    des.Padding = PaddingMode.None;
    ICryptoTransform ic = des.CreateDecryptor();
    byte[] dec = ic.TransformFinalBlock(plainText, 0, 8);
    return dec;
}
// Encrypt Text
textBox5.Text = ByteToString(TripleDesEncryptOneBlock(StringToByte(textBox5.Text),  
    StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));
// Decrypt Text
textBox5.Text = ByteToString(TripleDesDecryptBlock(StringToByte(textBox5.Text),  
    StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));
[My Related question on stack overflow](http://stackoverflow.com/questions/6990286/triple-des-encryption)

I found the below code online, but in its original state, the encryption was not using any padding (Padding.None), and was encrypting in blocks of 8. I wasn't able to get this code to properly encrypt / decrypt data so I was forced to have it encrypt the full length of the text. Is there something fundementally wrong with what I have done? If so, could someone please show me the propper way to encrypt text in blocks of 8?

// 168-bit (three-key) 3DES (Triple-DES) encrypt a single 8-byte block (ECB mode)
// plain-text should be 8-bytes, key should be 24 bytes.
public byte[] TripleDesEncryptOneBlock(byte[] plainText, byte[] key)
{
    // Create a new 3DES key.
    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    // Set the KeySize = 192 for 168-bit DES encryption.
    // The msb of each byte is a parity bit, so the key length is actually 168 bits.
    des.KeySize = 192;
    des.Key = key;
    des.Mode = CipherMode.ECB;
    des.Padding = PaddingMode.None;
    ICryptoTransform ic = des.CreateEncryptor();
    byte[] enc = ic.TransformFinalBlock(plainText, 0, 8);
    return enc;
}
public byte[] TripleDesDecryptBlock(byte[] plainText, byte[] key)
{
    // Create a new 3DES key.
    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    // Set the KeySize = 192 for 168-bit DES encryption.
    // The msb of each byte is a parity bit, so the key length is actually 168 bits.
    des.KeySize = 192;
    des.Key = key;
    des.Mode = CipherMode.ECB;
    des.Padding = PaddingMode.None;
    ICryptoTransform ic = des.CreateDecryptor();
    byte[] dec = ic.TransformFinalBlock(plainText, 0, 8);
    return dec;
}
// Encrypt Text
textBox5.Text = ByteToString(TripleDesEncryptOneBlock(StringToByte(textBox5.Text),  
    StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));
// Decrypt Text
textBox5.Text = ByteToString(TripleDesDecryptBlock(StringToByte(textBox5.Text),  
    StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));

I found the below code online, but in its original state, the encryption was not using any padding (Padding.None), and was encrypting in blocks of 8. I wasn't able to get this code to properly encrypt / decrypt data so I was forced to have it encrypt the full length of the text. Is there something fundamentally wrong with what I have done? If so, could someone please show me the proper way to encrypt text in blocks of 8?

// 168-bit (three-key) 3DES (Triple-DES) encrypt a single 8-byte block (ECB mode)
// plain-text should be 8-bytes, key should be 24 bytes.
public byte[] TripleDesEncryptOneBlock(byte[] plainText, byte[] key)
{
    // Create a new 3DES key.
    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    // Set the KeySize = 192 for 168-bit DES encryption.
    // The msb of each byte is a parity bit, so the key length is actually 168 bits.
    des.KeySize = 192;
    des.Key = key;
    des.Mode = CipherMode.ECB;
    des.Padding = PaddingMode.None;
    ICryptoTransform ic = des.CreateEncryptor();
    byte[] enc = ic.TransformFinalBlock(plainText, 0, 8);
    return enc;
}
public byte[] TripleDesDecryptBlock(byte[] plainText, byte[] key)
{
    // Create a new 3DES key.
    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    // Set the KeySize = 192 for 168-bit DES encryption.
    // The msb of each byte is a parity bit, so the key length is actually 168 bits.
    des.KeySize = 192;
    des.Key = key;
    des.Mode = CipherMode.ECB;
    des.Padding = PaddingMode.None;
    ICryptoTransform ic = des.CreateDecryptor();
    byte[] dec = ic.TransformFinalBlock(plainText, 0, 8);
    return dec;
}
// Encrypt Text
textBox5.Text = ByteToString(TripleDesEncryptOneBlock(StringToByte(textBox5.Text),  
    StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));
// Decrypt Text
textBox5.Text = ByteToString(TripleDesDecryptBlock(StringToByte(textBox5.Text),  
    StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));
[My Related question on stack overflow](http://stackoverflow.com/questions/6990286/triple-des-encryption)
Tuned code-formatting for readability.
Source Link
e-sushi
  • 18.1k
  • 12
  • 85
  • 235
       // 168-bit (three-key) 3DES (Triple-DES) encrypt a single 8-byte block (ECB mode)
        // plain-text should be 8-bytes, key should be 24 bytes.
        public byte[] TripleDesEncryptOneBlock(byte[] plainText, byte[] key)
        {
            // Create a new 3DES key.
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
 
            // Set the KeySize = 192 for 168-bit DES encryption.
            // The msb of each byte is a parity bit, so the key length is actually 168 bits.
 
            des.KeySize = 192;
            des.Key = key;
            des.Mode = CipherMode.ECB;
            des.Padding = PaddingMode.None;
 
            ICryptoTransform ic = des.CreateEncryptor();
 
            byte[] enc = ic.TransformFinalBlock(plainText, 0, 8);
 
            return enc;
        }
 
        public byte[] TripleDesDecryptBlock(byte[] plainText, byte[] key)
        {
            // Create a new 3DES key.
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
 
            // Set the KeySize = 192 for 168-bit DES encryption.
            // The msb of each byte is a parity bit, so the key length is actually 168 bits.
            des.KeySize = 192;
            des.Key = key;
            des.Mode = CipherMode.ECB;
            des.Padding = PaddingMode.None;
 
            ICryptoTransform ic = des.CreateDecryptor();
 
            byte[] dec = ic.TransformFinalBlock(plainText, 0, 8);
 
            return dec;
        }
 
// Encrypt Text
textBox5.Text = ByteToString(TripleDesEncryptOneBlock(StringToByte(textBox5.Text),  
    StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));
 
// Decrypt Text
textBox5.Text = ByteToString(TripleDesDecryptBlock(StringToByte(textBox5.Text),  
    StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));

Thank you,

Evan

       // 168-bit (three-key) 3DES (Triple-DES) encrypt a single 8-byte block (ECB mode)
        // plain-text should be 8-bytes, key should be 24 bytes.
        public byte[] TripleDesEncryptOneBlock(byte[] plainText, byte[] key)
        {
            // Create a new 3DES key.
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
 
            // Set the KeySize = 192 for 168-bit DES encryption.
            // The msb of each byte is a parity bit, so the key length is actually 168 bits.
 
            des.KeySize = 192;
            des.Key = key;
            des.Mode = CipherMode.ECB;
            des.Padding = PaddingMode.None;
 
            ICryptoTransform ic = des.CreateEncryptor();
 
            byte[] enc = ic.TransformFinalBlock(plainText, 0, 8);
 
            return enc;
        }
 
        public byte[] TripleDesDecryptBlock(byte[] plainText, byte[] key)
        {
            // Create a new 3DES key.
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
 
            // Set the KeySize = 192 for 168-bit DES encryption.
            // The msb of each byte is a parity bit, so the key length is actually 168 bits.
            des.KeySize = 192;
            des.Key = key;
            des.Mode = CipherMode.ECB;
            des.Padding = PaddingMode.None;
 
            ICryptoTransform ic = des.CreateDecryptor();
 
            byte[] dec = ic.TransformFinalBlock(plainText, 0, 8);
 
            return dec;
        }
 
// Encrypt Text
textBox5.Text = ByteToString(TripleDesEncryptOneBlock(StringToByte(textBox5.Text), StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));
 
// Decrypt Text
textBox5.Text = ByteToString(TripleDesDecryptBlock(StringToByte(textBox5.Text), StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));

Thank you,

Evan

// 168-bit (three-key) 3DES (Triple-DES) encrypt a single 8-byte block (ECB mode)
// plain-text should be 8-bytes, key should be 24 bytes.
public byte[] TripleDesEncryptOneBlock(byte[] plainText, byte[] key)
{
    // Create a new 3DES key.
    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    // Set the KeySize = 192 for 168-bit DES encryption.
    // The msb of each byte is a parity bit, so the key length is actually 168 bits.
    des.KeySize = 192;
    des.Key = key;
    des.Mode = CipherMode.ECB;
    des.Padding = PaddingMode.None;
    ICryptoTransform ic = des.CreateEncryptor();
    byte[] enc = ic.TransformFinalBlock(plainText, 0, 8);
    return enc;
}
public byte[] TripleDesDecryptBlock(byte[] plainText, byte[] key)
{
    // Create a new 3DES key.
    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    // Set the KeySize = 192 for 168-bit DES encryption.
    // The msb of each byte is a parity bit, so the key length is actually 168 bits.
    des.KeySize = 192;
    des.Key = key;
    des.Mode = CipherMode.ECB;
    des.Padding = PaddingMode.None;
    ICryptoTransform ic = des.CreateDecryptor();
    byte[] dec = ic.TransformFinalBlock(plainText, 0, 8);
    return dec;
}
// Encrypt Text
textBox5.Text = ByteToString(TripleDesEncryptOneBlock(StringToByte(textBox5.Text),  
    StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));
// Decrypt Text
textBox5.Text = ByteToString(TripleDesDecryptBlock(StringToByte(textBox5.Text),  
    StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));
edited tags
Link
ir01
  • 4k
  • 3
  • 22
  • 31
Loading
Source Link
Evan
  • 21
  • 1
  • 1
  • 2
Loading