Hi,
I have the following code that encrypts and then i can un-encrypt this file, which works file with text files i.e. readable characters.
However if i try to do this with a stored printed data file that contains unreadable characters it produces garbage.
Is there any way to do this using this .NET class?
method-id "EncryptFile" public static.
01 ls-fsInput type "System.IO.FileStream".
01 ls-fsEncrypted type "System.IO.FileStream".
01 ls-des type "System.Security.Cryptography.DESCryptoServiceProvider".
01 ls-desencrypt type "System.Security.Cryptography.ICryptoTransform".
01 ls-cryptostream type "System.Security.Cryptography.CryptoStream".
01 ls-bytearrayinput type "System.Byte" occurs any.
01 ls-len binary-long.
01 ls-ToBase64Transform type "System.Security.Cryptography.ToBase64Transform".
procedure division using by value lnk-inputfile as string
lnk-Outputfile as string
lnk-key as string.
set ls-fsInput to type "System.IO.FileStream"::"New"(lnk-inputfile , type "System.IO.FileMode"::"Open" , type "System.IO.FileAccess"::"Read")
set ls-fsEncrypted to type "System.IO.FileStream"::"New"(lnk-Outputfile , type "System.IO.FileMode"::"Create" , type "System.IO.FileAccess"::"Write")
set ls-des to type "System.Security.Cryptography.DESCryptoServiceProvider"::"New"()
*
* *> Set secret key for DES algorithm.
* *> A 64-bit key and an IV are required for this provider.
*>set ls-DES::"Key" to type "System.Text.ASCIIEncoding"::"ASCII"::"GetBytes"(lnk-Key)
set ls-DES::"Key" to type "System.Text.UTF8Encoding"::"UTF8"::"GetBytes"(lnk-Key)
*
* *> Set the initialization vector.
*>set ls-DES::"IV" to type "System.Text.ASCIIEncoding"::"ASCII"::"GetBytes"(lnk-Key)
set ls-DES::"IV" to type "System.Text.UTF8Encoding"::"UTF8"::"GetBytes"(lnk-Key)
*
* *> Create the DES encryptor from this instance.
set ls-desencrypt to ls-DES::"CreateEncryptor"()
*
* *> Create the crypto stream that transforms the file stream by using DES encryption.
set ls-cryptostream to type "System.Security.Cryptography.CryptoStream"::"New"(ls-fsEncrypted , ls-desencrypt , type "System.Security.Cryptography.CryptoStreamMode"::"Write")
*
* *> Read the file text to the byte array.
compute ls-len = ls-fsInput::"Length"
set size of ls-bytearrayinput to ls-len
invoke ls-fsInput::"Read"(ls-bytearrayinput , 0 , ls-bytearrayinput::"Length")
* *> Write out the DES encrypted file.
invoke ls-cryptostream::"Write"(ls-bytearrayinput , 0 , ls-bytearrayinput::"Length")
invoke ls-cryptostream::"Close"()
goback.
end method "EncryptFile".
method-id "DecryptFile" public static.
01 ls-fsRead type "System.IO.FileStream".
01 ls-fsdecrypted type "System.IO.StreamWriter".
01 ls-desdecrypt type "System.Security.Cryptography.ICryptoTransform".
01 ls-des type "System.Security.Cryptography.DESCryptoServiceProvider".
01 ls-cryptostream type "System.Security.Cryptography.CryptoStream".
01 ls-bytearrayinput type "System.Byte" occurs any.
01 ls-len binary-long.
procedure division using by value lnk-inputfile as string
lnk-Outputfile as string
lnk-key as string.
set ls-fsRead to type "System.IO.FileStream"::"New"(lnk-inputfile , type "System.IO.FileMode"::"Open" , type "System.IO.FileAccess"::"Read")
set ls-des to type "System.Security.Cryptography.DESCryptoServiceProvider"::"New"()
*> Set secret key for DES algorithm.
*> A 64-bit key and an IV are required for this provider.
*>set ls-DES::"Key" to type "System.Text.ASCIIEncoding"::"ASCII"::"GetBytes"(lnk-Key)
set ls-DES::"Key" to type "System.Text.UTF8Encoding"::"UTF8"::"GetBytes"(lnk-Key)
*> Set the initialization vector.
*>set ls-DES::"IV" to type "System.Text.ASCIIEncoding"::"ASCII"::"GetBytes"(lnk-Key)
set ls-DES::"IV" to type "System.Text.UTF8Encoding"::"UTF8"::"GetBytes"(lnk-Key)
*> Create the DES decryptor from the DES instance.
set ls-desdecrypt to ls-des::"CreateDecryptor"()
*> Create the crypto stream set to read and to do a DES decryption transform on incoming bytes.
set ls-cryptostream to type "System.Security.Cryptography.CryptoStream"::"New"(ls-fsRead , ls-desdecrypt , type "System.Security.Cryptography.CryptoStreamMode"::"Read")
*> Print out the contents of the decrypted file.
set ls-fsDecrypted to type "System.IO.StreamWriter"::"New"(lnk-OutputFile)
invoke ls-fsDecrypted::"Write"(type "System.IO.StreamReader"::"New"(ls-cryptostream)::"ReadToEnd")
invoke ls-fsDecrypted::"Flush"()
invoke ls-fsDecrypted::"Close"()
goback.
end method "DecryptFile".