Created
September 21, 2012 22:31
-
-
Save sandromello/3764308 to your computer and use it in GitHub Desktop.
MD5 and TripleDES - C# and Python implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Security.Cryptography; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var privatekey = "RN4nog0661VvjzphNg48372t747365mNfSxirueSDYstqsqeQn?="; | |
var skey = "e99308c2e3373fb041775a0c813f397d"; | |
var c = new Program(); | |
var encrypted = c.crypto(skey, privatekey, "encrypt"); | |
Console.WriteLine("ENCRYPTED: {0}", encrypted); | |
var decrypted = c.crypto(encrypted, privatekey, "decrypt"); | |
Console.WriteLine("DECRYPTED: {0}", decrypted); | |
} | |
public string crypto(string sk, string privatekey, string type) | |
{ | |
byte[] results; | |
System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding(); | |
MD5CryptoServiceProvider hashProvider = new MD5CryptoServiceProvider(); | |
byte[] tdeskey = hashProvider.ComputeHash(UTF8.GetBytes(privatekey)); | |
TripleDESCryptoServiceProvider tripDES = new TripleDESCryptoServiceProvider(); | |
tripDES.Key = tdeskey; | |
tripDES.Mode = CipherMode.ECB; | |
tripDES.Padding = PaddingMode.PKCS7; | |
byte[] data = null; | |
ICryptoTransform enc = null; | |
switch (type) | |
{ | |
case "encrypt": | |
enc = tripDES.CreateEncryptor(); | |
data = UTF8.GetBytes(sk); | |
break; | |
case "decrypt": | |
enc = tripDES.CreateDecryptor(); | |
data = Convert.FromBase64String(sk); | |
break; | |
} | |
try | |
{ | |
results = enc.TransformFinalBlock(data, 0, data.Length); | |
} | |
finally | |
{ | |
tripDES.Clear(); | |
hashProvider.Clear(); | |
} | |
if (type == "decrypt") | |
return UTF8.GetString(results); | |
else | |
return Convert.ToBase64String(results); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var privatekey = "RN4nog0661VvjzphNg48372t747365mNfSxirueSDYstqsqeQn?="; | |
var skey = "e99308c2e3373fb041775a0c813f397d"; | |
var c = new Program(); | |
var encrypted = c.crypto(skey, privatekey, "encrypt"); | |
Console.WriteLine("ENCRYPTED: {0}", encrypted); | |
var decrypted = c.crypto(encrypted, privatekey, "decrypt"); | |
Console.WriteLine("DECRYPTED: {0}", decrypted); | |
} | |
public string crypto(string sk, string privatekey, string type) | |
{ | |
byte[] results; | |
System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding(); | |
MD5CryptoServiceProvider hashProvider = new MD5CryptoServiceProvider(); | |
byte[] tdeskey = hashProvider.ComputeHash(UTF8.GetBytes(privatekey)); | |
TripleDESCryptoServiceProvider tripDES = new TripleDESCryptoServiceProvider(); | |
tripDES.Key = tdeskey; | |
tripDES.Mode = CipherMode.ECB; | |
tripDES.Padding = PaddingMode.PKCS7; | |
byte[] data = null; | |
ICryptoTransform enc = null; | |
switch (type) | |
{ | |
case "encrypt": | |
enc = tripDES.CreateEncryptor(); | |
data = UTF8.GetBytes(sk); | |
break; | |
case "decrypt": | |
enc = tripDES.CreateDecryptor(); | |
data = Convert.FromBase64String(sk); | |
break; | |
} | |
try | |
{ | |
results = enc.TransformFinalBlock(data, 0, data.Length); | |
} | |
finally | |
{ | |
tripDES.Clear(); | |
hashProvider.Clear(); | |
} | |
if (type == "decrypt") | |
return UTF8.GetString(results); | |
else | |
return Convert.ToBase64String(results); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Crypto.Cipher import DES3 | |
import hashlib | |
privatekey = "RN4nog0661VvjzphNg48372t747365mNfSxirueSDYstqsqeQn?=" | |
skey = "e99308c2e3373fb041775a0c813f397d" | |
BS = DES3.block_size | |
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS) | |
unpad = lambda s : s[0:-ord(s[-1])] | |
m = hashlib.md5() | |
m.update(privatekey) | |
cipher = DES3.new(m.digest()) | |
enc = cipher.encrypt(pad(skey)) | |
dec = unpad(cipher.decrypt(enc)) | |
print 'ENCRYPTED: {0}',enc.encode('base64') | |
print 'DECRYPTED: {0}',dec |
Can you share javascript equivalent of same code, I am trying to create using crypto-js
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can anyone share java equivalent code for the above C# code