Page 1 of 1

hide a private key in a random pad file

Posted: Sun Apr 24, 2011 5:41 pm
by idle
A basic method to obscure a private key in a random pad file

while it's not particularly useful as it is, with a bit of thought you can make it useful

Code: Select all

  ;hide a secret key in a random pad file 
 ;************************************************
 ;Note you need to verify that the pad doesn't collide
 ;so either try changing the salt or increase the size
 ;of the pad. 
 ;************************************************

Procedure hidekey(file.s,PublicKey.s,PrivateKey.s,size.i,salt=12345)
  Protected result.q,num.i
  result = salt
  fn = OpenFile(#PB_Any,file)
  If fn
     For a = 0 To size
        num = Random(74)+48
        If num > 57 And num < 65 
          num + 7
        ElseIf num > 90 And num < 97 
          num + 7
        EndIf   
        WriteByte(fn,num)
    Next
    
    For a = 1 To Len(publicKey)
      result = CRC32Fingerprint(@publickey,Len(publickey),result) & $FFFFFF
      result % size
      FileSeek(fn,result)
      byte = Asc(Mid(privatekey,a,1))
      WriteByte(fn,byte)
    Next
    CloseFile(fn)
EndIf   
EndProcedure     

Procedure.s GetKey(file.s,publickey.s,salt=12345)
  Protected key.s,result.q,size
  result=salt
  
  fn = OpenFile(#PB_Any,file)
  size = Lof(fn)-1
  If fn
    For a = 1 To Len(publickey)
      result = CRC32Fingerprint(@publickey,Len(publickey),result) & $FFFFFF
      result % size
      FileSeek(fn,result)
      key + Chr(ReadByte(fn))
  Next
  CloseFile(fn)
EndIf   

ProcedureReturn key
   
EndProcedure

Define publickey.s,privatekey.s,sfile.s,private.s,public.s,result.s
Global dump.s=Space(10000)

private = "my super secret password key to an aes encypted file"
public = "the one I give to the users" 

PrivateKey = MD5Fingerprint(@private,Len(private));
PublicKey = MD5Fingerprint(@public,Len(public));

sfile = "randomfile.dat"

hidekey(sfile,publickey,privatekey,10000,111)

Debug "public key " + publickey
Debug  "private key "  + privatekey

result  = getkey(sfile,publickey,111)
If result = privatekey 
  Debug "got the key " + result  
Else 
  Debug "failed " + result 
EndIf   
Debug "---------------------------------------------------"
Debug "the pad file"
fn = OpenFile(#PB_Any,sfile)
ReadData(fn,@dump,2048)
Debug dump
CloseFile(fn)