Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple Triple-DES-ECB Test
using System;
using System.Text;
using System.Security.Cryptography;
namespace TripleDES
{
class Program
{
private string encrypt(string clearText, string secretKey)
{
try
{
TripleDESCryptoServiceProvider tripleDESProvider = new TripleDESCryptoServiceProvider();
byte[] byteKey = Encoding.UTF8.GetBytes(secretKey.PadRight(24, '\0'));
if(byteKey.Length > 24)
{
byte[] bytePass = new byte[24];
Buffer.BlockCopy(byteKey, 0, bytePass, 0, 24);
byteKey = bytePass;
}
byte[] byteText = Encoding.UTF8.GetBytes(clearText);
tripleDESProvider.Key = byteKey;
tripleDESProvider.Mode = CipherMode.ECB;
byte[] byteMessage = tripleDESProvider.CreateEncryptor().TransformFinalBlock(byteText, 0, byteText.Length);
return Convert.ToBase64String(byteMessage);
}
catch(Exception ex)
{
return ex.Message;
}
}
private string decrypt(string data, string secretKey)
{
try
{
byte[] byteData = Convert.FromBase64String(data);
byte[] byteKey = Encoding.UTF8.GetBytes(secretKey.PadRight(24, '\0'));
if (byteKey.Length > 24)
{
byte[] bytePass = new byte[24];
Buffer.BlockCopy(byteKey, 0, bytePass, 0, 24);
byteKey = bytePass;
}
TripleDESCryptoServiceProvider tripleDESProvider = new TripleDESCryptoServiceProvider();
tripleDESProvider.Key = byteKey;
tripleDESProvider.Mode = CipherMode.ECB;
byte[] byteText = tripleDESProvider.CreateDecryptor().TransformFinalBlock(byteData, 0, byteData.Length);
return Encoding.UTF8.GetString(byteText);
}
catch(Exception ex)
{
return ex.Message;
}
}
static void Main(string[] args)
{
string clearText = "BSプレミアム20日放送";
string secretKey = "SecretKey";
string data = new TripleDES.Program().encrypt(clearText, secretKey);
clearText = new TripleDES.Program().decrypt(data, secretKey);
Console.WriteLine("Encrypted String: " + data);
Console.WriteLine(clearText);
Console.ReadLine();
}
}
}
#!/usr/bin/env python3
# coding: utf-8
import base64
import pyDes
from Crypto.Cipher import DES3
def encrypt_3des(clear_text, key):
clear_text_byte = clear_text.encode('utf-8')
key_byte = key.encode('utf-8')
key_byte = key_byte.ljust(24, "\0".encode('utf-8'))
if len(key_byte) > 24:
key_byte = key_byte[:24]
k = pyDes.triple_des(key_byte, pyDes.ECB, IV = None, pad = None, padmode = pyDes.PAD_PKCS5)
d = k.encrypt(clear_text_byte)
return base64.b64encode(d).decode('utf-8')
def encrypt_3des_crypto(clear_text, key):
key_byte = key.encode('utf-8')
key_byte = key_byte.ljust(24, "\0".encode('utf-8'))
if len(key_byte) > 24:
key_byte = key_byte[:24]
# PKCS#5
pad_len = 8 - len(clear_text) % 8
padding = chr(pad_len) * pad_len
clear_text += padding
cryptor = DES3.new(key_byte, DES3.MODE_ECB)
data = cryptor.encrypt(clear_text)
return base64.b64encode(data).decode('utf-8')
def decrypt_3des(data, key):
data_byte = base64.b64decode(data.encode('utf-8'))
key_byte = key.encode('utf-8')
key_byte = key_byte.ljust(24, "\0".encode('utf-8'))
if len(key_byte) > 24:
key_byte = key_byte[:24]
k = pyDes.triple_des(key_byte, pyDes.ECB, IV = None, pad = None, padmode = pyDes.PAD_PKCS5)
d = k.decrypt(data_byte)
return d.decode('utf-8')
def decrypt_3des_crypto(data, key):
data_byte = base64.b64decode(data.encode('utf-8'))
key_byte = key.encode('utf-8')
key_byte = key_byte.ljust(24, "\0".encode('utf-8'))
if len(key_byte) > 24:
key_byte = key_byte[:24]
cryptor = DES3.new(key_byte, DES3.MODE_ECB)
c_text = cryptor.decrypt(data_byte)
# PKCS#5
pad_len = ord(c_text.decode('utf-8')[-1])
clear_text = c_text.decode('utf-8')[:-pad_len]
return clear_text
def main():
clear_text = "BSプレミアム20日放送"
key = "SecretKey"
data = encrypt_3des(clear_text, key)
print("Encrypted String: " + data)
print(decrypt_3des(data, key))
data = encrypt_3des_crypto(clear_text, key)
print("Encrypted String: " + data)
print(decrypt_3des_crypto(data, key))
if __name__ == "__main__":
main()
echo -n "BSプレミアム20日放送" | openssl enc -e -des-ede3 -K "5365637265744B6579000000000000000000000000000000" -nosalt -a
echo "tpGP6UtYB5MLZwFG89V+TEIzM2CB8xMC+AFgZr4sezU=" | openssl enc -d -des-ede3 -K "5365637265744B6579000000000000000000000000000000" -nosalt -a
import java.util.Arrays;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
// import javax.xml.bind.DatatypeConverter;
public class TripleDESTest {
private String encrypt(String clearText, String secretKey) {
try {
byte[] bytePass = secretKey.getBytes("utf-8");
byte[] byteKey = Arrays.copyOf(bytePass, 24);
// System.out.println(DatatypeConverter.printHexBinary(byteKey));
SecretKey key = new SecretKeySpec(byteKey, "DESede");
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] byteText = clearText.getBytes("utf-8");
byte[] buf = cipher.doFinal(byteText);
byte[] byteBase64 = Base64.getEncoder().encode(buf);
String data = new String(byteBase64);
return data;
}
catch(Exception ex) {
return ex.getMessage();
}
}
private String decrypt(String data, String secretKey) {
try {
byte[] byteData = Base64.getDecoder().decode(data.getBytes("utf-8"));
byte[] bytePass = secretKey.getBytes("utf-8");
byte[] byteKey = Arrays.copyOf(bytePass, 24);
SecretKey key = new SecretKeySpec(byteKey, "DESede");
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] byteText = cipher.doFinal(byteData);
String clearText = new String(byteText, "utf-8");
return clearText;
}
catch(Exception ex) {
return ex.getMessage();
}
}
public static void main(String[] args) {
String clearText = "BSプレミアム20日放送";
String secretKey = "SecretKey";
String data = new TripleDESTest().encrypt(clearText, secretKey);
clearText = new TripleDESTest().decrypt(data, secretKey);
System.out.println("Encrypted String: " + data);
System.out.println(clearText);
}
}
<?php
function encrypt_3des($clear_text, $key) {
$encrypt_text = openssl_encrypt($clear_text, "DES-EDE3", $key, OPENSSL_RAW_DATA, "");
$data = base64_encode($encrypt_text);
return $data;
}
function decrypt_3des($data, $key) {
$encrypt_text = base64_decode($data);
$clear_text = openssl_decrypt($encrypt_text, "DES-EDE3", $key, OPENSSL_RAW_DATA, "");
return $clear_text;
}
$clear_text = "BSプレミアム20日放送";
$key = "SecretKey";
$data = encrypt_3des($clear_text, $key);
echo "Encrypted String: ".encrypt_3des($clear_text, $key)."\n";
echo decrypt_3des($data, $key)."\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.