Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example of how to encrypt in Python w/ pycrypt
- Loading branch information
Showing
4 changed files
with
276 additions
and
18 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,61 @@ | ||
#!/usr/bin/python | ||
from Crypto.Cipher import AES | ||
from Crypto import Random | ||
|
||
import Crypto.Hash.SHA | ||
import Crypto.Hash.SHA256 | ||
import Crypto.Random | ||
import Crypto.Protocol.KDF | ||
import Crypto.Cipher.AES | ||
import Crypto.Hash.HMAC | ||
import StringIO | ||
from pkcs7 import PKCS7Encoder | ||
|
||
class RNCryptor: | ||
"""Cryptor for RNCryptor""" | ||
def __init__(self, settings): | ||
self.settings = settings | ||
|
||
def encrypt(self, message, key, hmac_key): | ||
|
||
salt_size = 8 | ||
key_length = 32 | ||
iterations = 10000 | ||
HMAC_hash_algo = Crypto.Hash.SHA256 | ||
block_size = Crypto.Cipher.AES.block_size | ||
mode = Crypto.Cipher.AES.MODE_CBC | ||
|
||
def encrypt(self, message, password): | ||
random = Crypto.Random.new() | ||
|
||
encryption_salt = random.read(self.salt_size) | ||
encryption_key = Crypto.Protocol.KDF.PBKDF2(password, encryption_salt, self.key_length, self.iterations) | ||
|
||
hmac_salt = random.read(self.salt_size) | ||
hmac_key = Crypto.Protocol.KDF.PBKDF2(password, hmac_salt, self.key_length, self.iterations) | ||
|
||
iv = random.read(self.block_size) | ||
cipher = Crypto.Cipher.AES.new(encryption_key, self.mode, iv) | ||
|
||
ciphertext = cipher.encrypt(self.pad(message)) | ||
|
||
output = StringIO.StringIO() | ||
output.write(1) # Version 1 | ||
output.write(0) # No options | ||
iv = Random.new().read(AES.block_size) | ||
cipher = AES.new(key, AES.MODE_CBC, iv) | ||
encoder = PKCS7Encoder() | ||
output.write(chr(2)) # Version 2 | ||
output.write(chr(1)) # Password | ||
output.write(encryption_salt) | ||
output.write(hmac_salt) | ||
output.write(iv) | ||
output.write(cipher.encrypt(encoder.encode(message))) | ||
output.write(ciphertext) | ||
|
||
hmac = Crypto.Hash.HMAC.new(hmac_key, output.getvalue(), self.HMAC_hash_algo) | ||
|
||
output.write(hmac.digest()) | ||
|
||
return output.getvalue() | ||
|
||
def pad(self, data): | ||
block_size = self.block_size | ||
return data + (block_size - len(data) % block_size) * chr(block_size - len(data) % block_size) | ||
|
||
def main(): | ||
plaintext = b"Attack at dawn" | ||
key = '000102030405060708090a0b0c0d0e0f'.decode("hex") | ||
hmac_key = '0f0e0d0c0b0a09080706050403020100'.decode("hex") | ||
cipher = RNCryptor("") | ||
ciphertext = cipher.encrypt(plaintext, key, hmac_key) | ||
print ''.join('%02x' % ord(byte) for byte in ciphertext) | ||
password = b"mypassword" | ||
|
||
message = RNCryptor().encrypt(plaintext, password) | ||
print ''.join('%02x' % ord(byte) for byte in message) | ||
|
||
if __name__ == '__main__': | ||
main() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// | ||
// main.m | ||
// rncrypt | ||
// | ||
// Created by Rob Napier on 3/17/13. | ||
// Copyright (c) 2013 Rob Napier. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "RNEncryptor.h" | ||
#import "RNDecryptor.h" | ||
#import <getopt.h> | ||
|
||
NSData *GetDataForHex(NSString *hex) | ||
{ | ||
NSString *hexNoSpaces = [hex stringByReplacingOccurrencesOfString:@" " withString:@""]; | ||
|
||
NSMutableData *data = [[NSMutableData alloc] init]; | ||
unsigned char whole_byte = 0; | ||
char byte_chars[3] = {'\0','\0','\0'}; | ||
int i; | ||
for (i=0; i < [hexNoSpaces length] / 2; i++) { | ||
byte_chars[0] = [hexNoSpaces characterAtIndex:i*2]; | ||
byte_chars[1] = [hexNoSpaces characterAtIndex:i*2+1]; | ||
whole_byte = strtol(byte_chars, NULL, 16); | ||
[data appendBytes:&whole_byte length:1]; | ||
} | ||
return data; | ||
} | ||
|
||
void usage() { | ||
printf("Not like that\n"); | ||
exit(2); | ||
} | ||
|
||
int main(int argc, char * const argv[]) | ||
{ | ||
@autoreleasepool { | ||
|
||
int decrypt_flag; | ||
NSString *password = nil; | ||
NSString *message = nil; | ||
|
||
char ch; | ||
|
||
/* options descriptor */ | ||
struct option longopts[] = { | ||
{ "decrypt", no_argument, &decrypt_flag, 1 }, | ||
{ "password", required_argument, NULL, 'p' }, | ||
{ NULL, 0, NULL, 0 } | ||
}; | ||
|
||
while ((ch = getopt_long(argc, argv, "dp:P", longopts, NULL)) != -1) | ||
switch (ch) { | ||
case 'd': | ||
decrypt_flag = 1; | ||
break; | ||
case 'p': | ||
password = [NSString stringWithUTF8String:optarg]; | ||
break; | ||
default: | ||
usage(); | ||
} | ||
argc -= optind; | ||
argv += optind; | ||
|
||
if (argc != 1) { | ||
usage(); | ||
} | ||
|
||
message = [NSString stringWithUTF8String:argv[0]]; | ||
|
||
NSError *error; | ||
if (decrypt_flag) { | ||
NSData *decryptedData = [RNDecryptor decryptData:GetDataForHex(message) | ||
withPassword:password | ||
error:&error]; | ||
if (error) { | ||
NSLog(@"Failed: %@", error); | ||
exit(1); | ||
} | ||
else { | ||
NSString *string = [[NSString alloc] initWithData:decryptedData encoding:NSUTF8StringEncoding]; | ||
if (string) { | ||
printf("%s\n", [string UTF8String]); | ||
} | ||
else { | ||
printf("%s\n", [[decryptedData description] UTF8String]); | ||
} | ||
} | ||
} | ||
return 0; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// | ||
// Prefix header for all source files of the 'rncrypt' target in the 'rncrypt' project | ||
// | ||
|
||
#ifdef __OBJC__ | ||
#import <Foundation/Foundation.h> | ||
#endif |