@@ -24,6 +24,8 @@ <h2>AES-EAX Encryption</h2>
2424< input id ="password2 " value ="" />
2525< input id ="password3 " value ="" />
2626< br /> < br />
27+ Encryption Algorithm: < select id ="algorithm "> < option value ="aex "> AES-EAX</ option > < option value ="ocb "> OCB</ option > < option value ="gcm "> GCM</ option > </ select > < br />
28+ with Compression: < select id ="compression "> < option value ="uncompressed "> uncompressed</ option > < option value ="zip "> zip</ option > < option value ="zlib "> zlib</ option > </ select > < br />
2729Message: < button id ="btnEncrypt "> encrypt</ button > < br />
2830< textarea id ="inputText " class ="txtarea "> test 1234</ textarea >
2931< br />
@@ -93,31 +95,54 @@ <h2>AES-EAX Encryption</h2>
9395}
9496
9597( async ( ) => {
96- doEncrypt = async function ( ) {
97- var usepwds = Array ( ) ;
98-
99- var passwd1 = $ ( '#password1' ) . val ( ) ;
100- var passwd2 = $ ( '#password2' ) . val ( ) ;
101- var passwd3 = $ ( '#password3' ) . val ( ) ;
102-
103- if ( passwd1 != "" ) usepwds . push ( passwd1 ) ;
104- if ( passwd2 != "" ) usepwds . push ( passwd2 ) ;
105- if ( passwd3 != "" ) usepwds . push ( passwd3 ) ;
106-
107- //console.log(usepwds);
108-
109- var cleartext = $ ( '#inputText' ) . val ( ) ;
98+ var preferredCompressionAlgorithm ;
99+ var preferredAEADAlgorithm ;
100+
101+ selectAlgorithm = function ( ) {
102+ switch ( $ ( '#algorithm' ) . val ( ) ) {
103+ case 'ocb' : preferredAEADAlgorithm = openpgp . enums . aead . ocb // Non-native
104+ break ;
105+ case 'gcm' : preferredAEADAlgorithm = openpgp . enums . aead . experimentalGCM // **Non-standard**, fastest
106+ break ;
107+ default : preferredAEADAlgorithm = openpgp . enums . aead . eax // Default, native
108+ }
109+ }
110+ selectCompression = function ( ) {
111+ switch ( $ ( '#compression' ) . val ( ) ) {
112+ case 'zip' : preferredCompressionAlgorithm = openpgp . enums . compression . zip
113+ break ;
114+ case 'zlib' : preferredCompressionAlgorithm = openpgp . enums . compression . zlib
115+ break ;
116+ default : preferredCompressionAlgorithm = openpgp . enums . compression . uncompressed
117+ }
118+ }
119+ selectAlgorithm ( ) ;
120+ selectCompression ( ) ;
110121
111- const { message } = await openpgp . encrypt ( {
112- message : openpgp . message . fromText ( cleartext ) , // input as Message object
113- passwords : usepwds , // multiple passwords possible
114- armor : false // don't ASCII armor (for Uint8Array output)
115- } ) ;
122+ doEncrypt = async function ( ) {
123+ var usepwds = Array ( ) ;
124+
125+ var passwd1 = $ ( '#password1' ) . val ( ) ;
126+ var passwd2 = $ ( '#password2' ) . val ( ) ;
127+ var passwd3 = $ ( '#password3' ) . val ( ) ;
128+
129+ if ( passwd1 != "" ) usepwds . push ( passwd1 ) ;
130+ if ( passwd2 != "" ) usepwds . push ( passwd2 ) ;
131+ if ( passwd3 != "" ) usepwds . push ( passwd3 ) ;
132+
133+ //console.log(usepwds);
134+
135+ var cleartext = $ ( '#inputText' ) . val ( ) ;
136+
137+ const encrypted = await openpgp . encrypt ( {
138+ message : await openpgp . createMessage ( { text : cleartext } ) , // input as Message object
139+ passwords : usepwds , // multiple passwords possible
140+ format : 'binary' , // don't ASCII armor (for Uint8Array output)
141+ config : { preferredAEADAlgorithm,
142+ preferredCompressionAlgorithm }
143+ } ) ;
116144
117- const encrypted = message . packets . write ( ) ; // get raw encrypted packets as Uint8Array
118- //console.log(message.packets)
119145 $ ( '#encryptedTxt' ) . val ( bytesToBase64 ( encrypted ) ) ;
120-
121146 }
122147
123148 doDecrypt = async function ( ) {
@@ -126,11 +151,18 @@ <h2>AES-EAX Encryption</h2>
126151
127152 var encryptedBytes = new Uint8Array ( _base64ToArrayBuffer ( encryptedd ) ) ;
128153
154+ const message = await openpgp . readMessage ( {
155+ binaryMessage : encryptedBytes // parse message
156+ } ) ;
157+
129158 const { data : decrypted } = await openpgp . decrypt ( {
130- message : await openpgp . message . read ( encryptedBytes ) , // parse encrypted bytes
159+ message,
131160 passwords : [ passwd ] , // decrypt with password
161+ config : { preferredAEADAlgorithm,
162+ preferredCompressionAlgorithm }
132163 } ) ;
133164
165+
134166 //console.log(decrypted)
135167 $ ( '#decryptedTxt' ) . val ( decrypted ) ;
136168 } ;
@@ -162,6 +194,9 @@ <h2>AES-EAX Encryption</h2>
162194 doDecrypt ( )
163195} )
164196
197+ $ ( '#algorithm' ) . change ( ( ) => selectAlgorithm ( ) )
198+ $ ( '#compression' ) . change ( ( ) => selectCompression ( ) )
199+
165200</ script >
166201
167202</ body >
0 commit comments