Triple DES Şifreleme

Elektronik ödeme sistemlerinde sıklıkla kullanılan TDEA(Data Encryption Algorithm) veya genel adıyla Triple DES şifreleme ve şifre çözme yapabilen java ve C# sınıfları aşağıdadır. Şifreleme işlemi yapıldıktan sonra veri Base64 kodlandığı için web servisleri üzerinden gönderilebilir.

package project1;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;

public class TripleDes {

    public String encrypt(String message, String key) throws Exception {
        // Create secret key
        byte[] bKey = new byte[24];
        for (int i = 0; i < key.length() && i < bKey.length; i++) {
            bKey[i] = (byte)key.charAt(i);
        }
        SecretKey secretKey = new SecretKeySpec(bKey, "DESede");

        // Initialize encoder
        Cipher desCipher = Cipher.getInstance("DESede");
        desCipher.init(Cipher.ENCRYPT_MODE, secretKey);

        // Encode DESede
        byte[] byteCipherText = desCipher.doFinal(message.getBytes());

        // Encode BASE64 and return
        return new BASE64Encoder().encode(byteCipherText);
    }

    public String decrypt(String message, String key) throws Exception {
        // Create secret key
        byte[] bKey = new byte[24];
        for (int i = 0; i < key.length() && i < bKey.length; i++) {
            bKey[i] = (byte)key.charAt(i);
        }
        SecretKey secretKey = new SecretKeySpec(bKey, "DESede");

        // Initialize decoder
        Cipher desCipher = Cipher.getInstance("DESede");
        desCipher.init(Cipher.DECRYPT_MODE, secretKey, desCipher.getParameters());

        // Decode BASE64
        byte[] decodedMessage = new BASE64Decoder().decodeBuffer(message);

        // Decode DESede and return
        return new String(desCipher.doFinal(decodedMessage));
    }
}
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Project1
{
    internal static class TripleDes
    {
        internal static string Decrypt(string cipher, string key)
        {
            byte[] cipherBytes = Convert.FromBase64String(cipher);

            MD5CryptoServiceProvider md5Csb = new MD5CryptoServiceProvider();
            byte[] keyBytes = md5Csb.ComputeHash(Encoding.UTF8.GetBytes(key));

            string decryptedData;

            using (MemoryStream ms = new MemoryStream())
            {
                TripleDES tdes = TripleDES.Create();
                tdes.Key = keyBytes;
                tdes.Mode = CipherMode.ECB;
                using (CryptoStream cs = new CryptoStream(ms, tdes.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(cipherBytes, 0, cipherBytes.Length);
                    cs.FlushFinalBlock();
                    byte[] messageBytes = ms.ToArray();
                    decryptedData = Encoding.UTF8.GetString(messageBytes);
                }
            }

            return decryptedData;
        }

        internal static string Encrypt(string message, string key)
        {
            byte[] messageBytes = Encoding.UTF8.GetBytes(message);

            MD5CryptoServiceProvider md5Csb = new MD5CryptoServiceProvider();
            byte[] keyBytes = md5Csb.ComputeHash(Encoding.UTF8.GetBytes(key));

            string encryptedData;
            using (MemoryStream ms = new MemoryStream())
            {
                TripleDES tdes = TripleDES.Create();
                tdes.Key = keyBytes;
                tdes.Mode = CipherMode.ECB;
                using (CryptoStream cs = new CryptoStream(ms, tdes.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(messageBytes, 0, messageBytes.Length);
                    cs.FlushFinalBlock();
                    byte[] cipherBytes = ms.ToArray();
                    encryptedData = Convert.ToBase64String(cipherBytes);
                }
            }

            return encryptedData;
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *