Quantcast
Channel: Visual COBOL
Viewing all articles
Browse latest Browse all 5819

Forum Post: RE: Encrypt And Un-Encrypt a Print file

$
0
0
Well, I have my version working, and it doesn't appear to have a problem with files of any sort. Here's the source.       $set sourceformat(variable)       $set ilusing(System)       $set ilusing(System.IO)       $set ilusing(System.Security.Cryptography)       $set ilusing(System.Text)        class-id descrypt.        1 inputFile type FileStream static.        1 outputFile type FileStream static.        1 des type DESCryptoServiceProvider static value new DESCryptoServiceProvider.        method-id Main(args as string occurs any) static.            if args::Length 4                invoke self::Syntax            end-if            declare encrypting as condition-value            evaluate args(1)            when "encrypt"                set encrypting to true            when "decrypt"                set encrypting to false            when other                invoke self::Syntax            end-evaluate            set inputFile to new FileStream(args(3), type FileMode::Open, type FileAccess::Read)            set outputFile to new FileStream(args(4), type FileMode::Create, type FileAccess::Write)            declare keySize as binary-long = des::BlockSize / 8            declare keyBlock as binary-char unsigned occurs any            set size of keyBlock to keySize            declare keyBytes = type UTF8Encoding::UTF8::GetBytes(args(2))            declare copyLen = function min(size of keyBlock, size of keyBytes)            invoke type Array::Copy(keyBytes, 0, keyBlock, 0, copyLen)            set des::Key des::IV to keyBlock            set des::IV to keyBlock            if encrypting                invoke self::Encrypt            else                invoke self::Decrypt            end-if        end method Main.        method-id Syntax static.            display "Usage: descrypt encrypt|decrypt key input-file output-file"            invoke type Environment::Exit(1)        end method Syntax.        method-id Encrypt static.            declare cryptoStream = new CryptoStream(outputFile, des::CreateEncryptor, type CryptoStreamMode::Write)            invoke inputFile::CopyTo(cryptoStream)            invoke cryptoStream::Close            invoke outputFile::Close        end method Encrypt.        method-id Decrypt static.            declare cryptoStream = new CryptoStream(inputFile, des::CreateDecryptor, type CryptoStreamMode::Read)            invoke cryptoStream::CopyTo(outputFile)            invoke cryptoStream::Close            invoke outputFile::Close        end method Decrypt.        end class descrypt. Some things to note: This was tested with Enterprise Developer 2.2 Update 1 and .NET Framework 4.5. I've refactored common code into the Main method. Type and method names do not need to be quoted in current versions of managed COBOL. Use the new operator rather than invoking a constructor directly ( type X ::New(...) ). The CopyTo method (of Stream and its subclasses) is the simplest way to read an entire stream and write it to another stream. The COBOL type binary-char unsigned is the same as the .NET type Byte . My Main method creates an array of the correct size, then copies the key bytes into it, to ensure the Key and IV properties receive an array of the correct size.

Viewing all articles
Browse latest Browse all 5819

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>