Posted in Sybase, T-SQL

Crypto in the personal privacy world

I was recently presented with The Code Book and while leafing through the pages during a moment of boredom I managed to create a simple crypto using the Julius Caesar character substitution method using Sybase T-SQL. And though I’m sure that the resulting encrypted text could be broken with brute force, it would not be easy, nor very worthwhile for most messaging.

In this world of spying and privacy invasion from all directions, a little personal crypto might be in order, particularly if one is inclined to be suspicious of commercial algorithms. and while this stored procedure is simple, it can be enhanced, feel free to use it for your own needs.

UPDATE: This is stronger than used Here: BA jihadist relied on Jesus-era encryption

CREATE PROCEDURE dbo.simple_crypto
(
@key_word varchar(25),
@message varchar(255),
@direction varchar(8)
)
as

set nocount on

declare
@key_input varchar(20),
@key varchar(27),
@key2 char(27),
@olumn int, @olumnk int,
@input varchar(255),
@olumnb int,
@output varchar(255),
@out char(1), @a_char char(1),
@undone varchar(255),
@count int

select @count = 97, @olumnk = 1
select @key_input = lower(@key_word) –‘branedy’
select @input = lower(@message)

create table #alpha (a_char char(1))

while @count <= 122
begin
insert into #alpha
values (char(@count))

select @count = @count + 1
end

select @key = char(32)

while @olumnk <= datalength(@key_input)
begin
select @out = substring(@key_input, @olumnk, 1)

delete from #alpha where a_char = @out

if @@rowcount = 1
begin
select @key = @key + @out
end

select @olumnk = @olumnk+1

end

select @key = @key + a_char from #alpha

drop table #alpha

select
@olumn = 1,
@olumnb = 1,
@key2 = ‘abcdefghijklmnopqrstuvwxyz{‘

—————–encrypt

if @direction = ‘Cipher’
begin

while @olumnb <= datalength(@input)
begin
select @output = @output + char(charindex(substring(@input, @olumnb,1), @key) + 96)
select @olumnb = @olumnb+1
end

select @input + ‘ = ‘ + @output as ‘Encrypted’

end
——————————————-decrypt

if @direction = ‘Decipher’
begin

select @output =@message

while @olumn <= datalength(@output)
begin
select
@undone = @undone+ char(ascii(substring(@key, charindex(substring(@output, @olumn,1), @key2),1))) –,
select @olumn = @olumn+1
end

select @output + ‘ = ‘ + @undone as ‘Decrypted’
end

——————————–

select ‘From the key ‘ + @key

return
GO

Author:

With 40 years experience in software development, systems design and engineering and IT operations, and Infrastructure Architecture issues. I am versed in multiple programming languages, Operating Systems and RDBMS, I have work experience ranging from microcomputers and PC’s to multiprocessor mid range Unix systems and clusters. I have experience with both wireless and wired network protocols and mediums. And I've help migrate systems into the Amazon EC2 Cloud from self hosted configurations. I collect old working computers, I'm a published Astro-photographer, I tutor, and teach almost every subject I am knowledgeable in. I have had one internet email or another since 1991. I developed Gopher sites prior to the formation of HTTP/HTML and a few websites since then. I wrote my first 'database' on a DEC PDP-11 for the DECUS Library in 1984. Specialties I specialize in Database systems, and am familiar with almost all types of RDBMS and ISAM systems short of Mainframes. I habitually reverse engineer and document everything I touch.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s