@@ -338,11 +338,11 @@ static Map<String, String[]> allSupportedCiphers() {
338
338
339
339
modes = cipherModes ("DESede" );
340
340
if ( modes != null ) {
341
- supportedCiphers .put ( "DES-EDE" , new String [] { "DES" , "CBC " , "EDE" , "DESede/CBC " } );
342
- supportedCiphers .put ( "DES-EDE-CBC" , supportedCiphers . get ( "DES- EDE" ) );
341
+ supportedCiphers .put ( "DES-EDE" , new String [] { "DES" , "ECB " , "EDE" , "DESede/ECB " } );
342
+ supportedCiphers .put ( "DES-EDE-CBC" , new String [] { "DES" , "CBC" , " EDE", "DESede/CBC" } );
343
343
supportedCiphers .put ( "DES-EDE-CFB" , new String [] { "DES" , "CBC" , "EDE" , "DESede/CFB" } );
344
344
supportedCiphers .put ( "DES-EDE-OFB" , new String [] { "DES" , "CBC" , "EDE" , "DESede/OFB" } );
345
- supportedCiphers .put ( "DES-EDE3" , new String [] { "DES" , "CBC " , "EDE3" , "DESede/CBC " });
345
+ supportedCiphers .put ( "DES-EDE3" , new String [] { "DES" , "ECB " , "EDE3" , "DESede/ECB " });
346
346
for ( final String mode : modes ) {
347
347
supportedCiphers .put ( "DES-EDE3-" + mode , new String [] { "DES" , mode , "EDE3" , "DESede/" + mode });
348
348
}
@@ -427,18 +427,21 @@ private static Algorithm osslToJava(final String osslName, final String padding)
427
427
alg .realName = algVals [3 ];
428
428
alg .realNameNeedsPadding = true ;
429
429
alg .padding = getPaddingType (padding , cryptoMode );
430
+
431
+ System .out .println (osslName + " alg = " + alg );
432
+
430
433
return alg ;
431
434
}
432
435
433
- String cryptoBase , cryptoVersion = null , cryptoMode = "CBC" , realName ;
434
- String paddingType ;
436
+ String cryptoBase , cryptoVersion = null , cryptoMode , realName ;
437
+ String paddingType = null ;
435
438
436
439
// EXPERIMENTAL: if there's '/' assume it's a "real" JCE name :
437
440
if ( osslName .indexOf ('/' ) != -1 ) {
438
441
// e.g. "DESedeWrap/CBC/NOPADDING"
439
442
final List names = StringHelper .split ((CharSequence ) osslName , '/' );
440
443
cryptoBase = (String ) names .get (0 );
441
- if ( names .size () > 1 ) cryptoMode = (String ) names .get (1 );
444
+ cryptoMode = names .size () > 1 ? (String ) names .get (1 ) : "CBC" ;
442
445
paddingType = getPaddingType (padding , cryptoMode );
443
446
if ( names .size () > 2 ) paddingType = (String ) names .get (2 );
444
447
Algorithm alg = new Algorithm (cryptoBase , null , cryptoMode );
@@ -449,7 +452,7 @@ private static Algorithm osslToJava(final String osslName, final String padding)
449
452
450
453
int s = osslName .indexOf ('-' ); int i = 0 ;
451
454
if (s == -1 ) {
452
- cryptoBase = osslName ;
455
+ cryptoBase = osslName ; cryptoMode = null ;
453
456
}
454
457
else {
455
458
cryptoBase = osslName .substring (i , s );
@@ -471,19 +474,19 @@ private static Algorithm osslToJava(final String osslName, final String padding)
471
474
cryptoBase = cryptoBase .toUpperCase (); // allways upper e.g. "AES"
472
475
if ( cryptoMode != null ) cryptoMode = cryptoMode .toUpperCase ();
473
476
474
- boolean realNameSet = false ;
477
+ boolean realNameSet = false ; boolean setDefaultCryptoMode = true ;
475
478
476
479
if ( "BF" .equals (cryptoBase ) ) realName = "Blowfish" ;
477
480
else if ( "CAST" .equals (cryptoBase ) ) realName = "CAST5" ;
478
481
else if ( cryptoBase .startsWith ("DES" ) ) {
479
482
if ( "DES3" .equals (cryptoBase ) ) {
480
- cryptoBase = "DES" ; realName = "DESede" ; // cryptoVersion = cryptoMode; cryptoMode = "CBC";
483
+ cryptoBase = "DES" ; realName = "DESede" ; cryptoVersion = "EDE3" ; // cryptoMode = "CBC";
481
484
}
482
485
else if ( "EDE3" .equalsIgnoreCase (cryptoVersion ) || "EDE" .equalsIgnoreCase (cryptoVersion ) ) {
483
- realName = "DESede" ;
486
+ realName = "DESede" ; if ( cryptoMode == null ) cryptoMode = "ECB" ;
484
487
}
485
488
else if ( "EDE3" .equalsIgnoreCase (cryptoMode ) || "EDE" .equalsIgnoreCase (cryptoMode ) ) {
486
- realName = "DESede" ; cryptoVersion = cryptoMode ; cryptoMode = "CBC " ;
489
+ realName = "DESede" ; cryptoVersion = cryptoMode ; cryptoMode = "ECB " ;
487
490
}
488
491
else realName = "DES" ;
489
492
}
@@ -513,12 +516,14 @@ else if ( cryptoBase.length() > 8 && cryptoBase.startsWith("CAMELLIA") ) {
513
516
cryptoVersion = cryptoMode ;
514
517
}
515
518
cryptoMode = null ; // padding = null;
519
+ setDefaultCryptoMode = false ;
516
520
// cryptoMode = "NONE"; paddingType = "NoPadding";
517
521
realNameSet = true ;
518
522
}
519
523
}
520
524
521
- paddingType = getPaddingType (padding , cryptoMode );
525
+ if ( cryptoMode == null && setDefaultCryptoMode ) cryptoMode = "CBC" ;
526
+ if ( paddingType == null ) paddingType = getPaddingType (padding , cryptoMode );
522
527
523
528
if ( cryptoMode != null ) {
524
529
//if ( ! KNOWN_BLOCK_MODES.contains(cryptoMode) ) {
@@ -551,9 +556,8 @@ String getPadding() {
551
556
}
552
557
553
558
private static String getPaddingType (final String padding , final String cryptoMode ) {
554
-
559
+ //if ( "ECB".equals(cryptoMode) ) return "NoPadding";
555
560
// TODO check cryptoMode CFB/OFB
556
-
557
561
final String defaultPadding = "PKCS5Padding" ;
558
562
559
563
if ( padding == null ) return defaultPadding ;
@@ -615,8 +619,11 @@ public int getIvLength() {
615
619
//else if ( "DES".equals(base) ) {
616
620
// ivLength = 8;
617
621
//}
618
- else if ( "RC4" .equals (base ) ) {
619
- ivLength = 8 ;
622
+ //else if ( "RC4".equals(base) ) {
623
+ // ivLength = 8;
624
+ //}
625
+ else if ( "ECB" .equals (mode ) ) {
626
+ ivLength = 0 ;
620
627
}
621
628
else {
622
629
ivLength = 8 ;
@@ -639,11 +646,9 @@ public int getKeyLength() {
639
646
}
640
647
if ( keyLen == -1 ) {
641
648
if ( "DES" .equals (base ) ) {
642
- if ( "EDE3" .equalsIgnoreCase (version ) ) {
643
- keyLen = 24 ;
644
- } else {
645
- keyLen = 8 ;
646
- }
649
+ if ( "EDE" .equalsIgnoreCase (version ) ) keyLen = 16 ;
650
+ else if ( "EDE3" .equalsIgnoreCase (version ) ) keyLen = 24 ;
651
+ else keyLen = 8 ;
647
652
}
648
653
else if ( "RC4" .equals (base ) ) {
649
654
keyLen = 16 ;
@@ -715,22 +720,23 @@ public Cipher(Ruby runtime, RubyClass type) {
715
720
private String padding ;
716
721
717
722
private void dumpVars (final PrintStream out , final String header ) {
718
- out .println (this .toString () + ' ' + header );
719
- out .println (" name = " + name );
720
- out .println (" cryptoBase = " + cryptoBase );
721
- out .println (" cryptoVersion = " + cryptoVersion );
722
- out .println (" cryptoMode = " + cryptoMode );
723
- out .println (" padding_type = " + paddingType );
724
- out .println (" realName = " + realName );
725
- out .println (" keyLength = " + keyLength );
726
- out .println (" ivLength = " + ivLength );
727
- out .println (" cipher.alg = " + cipher == null ? null : cipher .getAlgorithm ());
728
- out .println (" cipher.blockSize = " + cipher == null ? null : cipher .getBlockSize ());
729
- out .println (" encryptMode = " + encryptMode );
730
- out .println (" cipherInited = " + cipherInited );
731
- out .println (" key.length = " + (key == null ? 0 : key .length ));
732
- out .println (" iv.length = " + (realIV == null ? 0 : realIV .length ));
733
- out .println (" padding = " + padding );
723
+ out .println (this .toString () + ' ' + header +
724
+ "\n " +
725
+ " name = " + name +
726
+ " cryptoBase = " + cryptoBase +
727
+ " cryptoVersion = " + cryptoVersion +
728
+ " cryptoMode = " + cryptoMode +
729
+ " padding_type = " + paddingType +
730
+ " realName = " + realName +
731
+ " keyLength = " + keyLength +
732
+ " ivLength = " + ivLength +
733
+ "\n " +
734
+ " cipher.alg = " + (cipher == null ? null : cipher .getAlgorithm ()) +
735
+ " cipher.blockSize = " + (cipher == null ? null : cipher .getBlockSize ()) +
736
+ " encryptMode = " + encryptMode + " cipherInited = " + cipherInited +
737
+ " key.length = " + (key == null ? 0 : key .length ) +
738
+ " iv.length = " + (realIV == null ? 0 : realIV .length ) +
739
+ " padding = " + padding );
734
740
}
735
741
736
742
@ JRubyMethod (required = 1 , visibility = Visibility .PRIVATE )
0 commit comments