Playing with simple encryption math in Python

Probably every real programmer absorbed this decades ago in CS101. I learned it from an episode of NOVA I watched with my son last night, “The Rise of the Hackers“: The basis for most public/private key encryption, which we use for practically all internet data privacy these days, is a simple mathematical fact discovered (I think) by the RSA mathematicians:

It’s really, really hard to find the factors of a semi-prime, the product of two prime numbers. That is, it’s easy to multiply two primes together but hard to reverse engineer, such as you need to do when you hack encryption keys. Computers can start trying to compute the factors of a large semi-prime, but it will take them a long time—”thousands of computers millions of years” for the types of semi-primes we’re talking about. Factorable factors like 20000 x 350400, easy. Primes as factors, like 547 x 953, computationally too intensive for hackers, even with the latest hardware. So the semi-prime becomes the public key and its factors the private keys.

This fact seemed so irresistible and elegant to me (a teachable moment for my son! Something I could dust the old Python off for) that I got my laptop out and, as we watched the segment on quantum cryptography, created a little test program:

import time
def factor(sp):
    start = time.time()
    for p1 in range(1,sp):
        for p2 in range(1, sp):
            if p1 * p2 == sp:
                print "factors:", p1, p2
    print "time:", time.time() - start, '\n'

# factor(487*691)
# factor(997*991)

So the factor function uses nested loops to compute the factors of a given number, and it uses the time module to figure out how long each computation takes from start to finish. There are better ways to do this function, like with reduce code or using set to remove x/y y/x dupes, but it works and it’s legible. The bottom part tests the function by supplying semi-primes (via multiplication, so factor(229*101) is equivalent to factor(23129) because of the rules of precedence) and other numbers, and prints the resulting times.

Check it out! (I put the results below.) When you start getting into the semi-primes, it can thousands of times longer to factor, and these are low numbers. I commented out the 487*691 and 997*991 tests because they wouldn’t complete after like 10 minutes!

It’s so great to see mathematical principles applied in this way, to see the simple origins of complex systems like cryptography, and to noodle with code to get how things are working.

factors: 3 5
factors: 5 3
time: 0.0390000343323 

factors: 3 355
factors: 5 213
factors: 15 71
factors: 71 15
factors: 213 5
factors: 355 3
time: 0.213999986649 

factors: 101 229
factors: 229 101
time: 65.7490000725 

factors: 89 307
factors: 307 89
time: 81.3059999943 

factors: 2 90000
factors: 3 60000
factors: 4 45000
factors: 5 36000
factors: 6 30000
factors: 8 22500
factors: 9 20000
factors: 10 18000
factors: 12 15000
factors: 15
etc etc

TekMountain Mentor

I’m genuinely honored to have been invited on as a Mentor at TekMountain, a forward-looking tech incubator here in Wilmington, NC, where I live. I’ll co-work my first real day there today!

TekMountain has been a fantastic host, thought leader, and citizen in this region since it started up a couple years ago, partnering with Cucalorus on Cucalorus Connect, a new film sub-festival celebrating tech and entrepreneurship, hosting innumerable public forums and meetups on technology and business, taking the lead here in opposition to NC’s loathsome HB2 bill, chartering Cape Fear Women in Tech, tons of stuff.

I imagine I’ve been tapped for my credentials as an IBMer, dilettante technologist, and local gadfly. To earn my keep, I’m proposing to convene, assist or participate in the following projects, many of which we’ve sort of got underway already.

Projects and ideas

  • With John Cornelius at Wide Open Tech, UNCW Psychology chair and brain guy Julian Keith, Watson University IBMer Mike Orr, and Tanner Clayton, we convened a tech talk on artificial intelligence at Tekmountain last year that was really well attended, energetic, and thoughtful–and that only scratched the surface of this topic.Mike and Julian, the two panelists, have already plotted a follow-up and I really want to bring it to TekMountain and enlarge the conversation even more.


  • In that context or some other, I’d like to show off some of IBM’s Cognitive Computing APIs, services like Emotion Analysis and others in our Watson Developer Cloud that you can build into cognitive applications on IBM’s Bluemix cloud development platform.


  • More generally, I think I can demonstrate a bit about Bluemix development, like my man Jeff Sloyer, who works as an evangelist at IBM and at an incubator in Raleigh.


  • Blockchain is white-hot right now. I’d love to talk about hyperledgers, blockchain applications, and how this technology works with transactions, security, and business processes. I’d of course have to learn this stuff first.


  • In similar fashion (i.e., learn by doing, learn by presenting), I  think we could talk about the Swift programming language, which Apple has open-sourced and IBM is building into the enterprise, Linux, and the server side). Maybe also Whiskwhat it has to do with Swift, “server-less” architectures, Internet of Things, and stuff like that.


  • I’m already working on putting Wilmington on the very short list of cities that will host one of Sandy Carter’s tremendous Hackathons


  • I’d love of course to write for TekMountain, and more about the area and technology generally.


  • I really want to do something with programming for kids at TekMountain. For the last few years, I’ve lead tech club and “code camps” at my son’s elementary and in the summer, using MIT’s  Scratch programming environment to introduce kids to coding and, like, systems thinking type stuff, design.


  • I’d also like to connect TekMountain and the Cape Fear Economic Development Council, where I’m a director, in ways beyond the friendly and partner-ish relationship we have now. These two organizations want many of the same things and have great individuals and brain power to combine.


  • Just for fun, and since I’m the guy in the area who in the past has organized informal lunches of IBMers in the region, and there are many (Kure Beach, Ogden, Wrightsville, etc.), I thought it’d be fun to have an IBMer mixer at TekMountain, show my colleagues the facility, the co-working, TekMountain’s new brewing powers.

I’m working on a project that I hope you will find interesting and maybe even help out with: is (will be) a site for investing in and shorting viral phrases, or memes. Like a stock market for memes. A game. But maybe with real money. Like: “I’m shorting ‘In da club’ and putting money on ‘Starfish'”, and so on. It’d be so awesome.

Like the now-deceased Popdex (“WTF happened to Popdex?“), which allowed you to build a portfolio of celebrities and earn as those celebrities became more celebrated, lose as they lost relevance. But for words and phrases. Memetrade uses tools like Google Trends,, and others to track the rise and fall of expressions. Expressions like “It’s complicated”, which as everyone knows became a coy, self-regarding way for Facebookers to list their relationship status and then, inevitably, the name of a rom-com movie that probably didn’t do well because it was conceived as a vehicle for the cool meme. I’m picturing the focus groups and executive pitches here. Don’t launch movies because everybody is saying this thing around the office or the school yard, like “It’s complicated”. It’s not enough.


But what happened to “It’s complicated” in the early twentieth century just before 1920 in the chart above? What was “it”? Freud’s psychoanalytic method? Andre Gide’s fiction?

“Hella” is also a meme that inspired this. It won’t go away and it was one of those that I would have shorted immediately and lost my shirt on.

So I haven’t built that much—just got a Django server running, got the domain, been poking around for tracking sources and good APIs. Want to do some building? would be a great mix of linguistics, pop culture, collaboration, building things on the web generally.