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

Advertisements