使用清除密钥

时间:2018-03-05 09:34:16

标签: encryption db2 cobol zos 3des

我正在尝试使用以下界面编写Cobol程序:

目标

接收明确​​的加密密钥和明文,并使用3DES算法返回加密文本。

输入

  • CLEAR_KEY:一个32个字符的十六进制字符串,用作加密密钥。
  • CLEAR_TEXT:一个16个字符的字符串。

输出:

  • CYPHERED_TEXT:一个16个字符的字符串。

我可以访问DB2ICSF callable services

我尝试了这三种方法:

使用CSNBSYE

77 CSNBSYE                  PIC X(7) VALUE 'CSNBSYE'.
01 CSNBSYE-PARAMETERS.
   02 RETURN-CODE           PIC 9(8) COMP.
   02 REASON-CODE           PIC 9(8) COMP.
   02 EXIT-DATA-LENGTH      PIC 9(8) COMP.
   02 EXIT-DATA             PIC X(32).
   02 RULE-ARRAY-COUNT      PIC 9(8) COMP.
   02 RULE-ARRAY            PIC X(8).
   02 KEY-IDENTIFIER-LENGTH PIC 9(8) COMP.
   02 KEY-IDENTIFIER        PIC X(32).
   02 KEY-PARMS-LENGTH      PIC 9(8) COMP.
   02 KEY-PARMS             PIC X(32).
   02 BLOCK-SIZE            PIC 9(8) COMP.
   02 INIT-VECTOR-LENGTH    PIC 9(8) COMP.
   02 INIT-VECTOR           PIC X(8).
   02 CHAIN-DATA-LENGTH     PIC 9(8) COMP.
   02 CHAIN-DATA            PIC X(16).
   02 CLEAR-TEXT-LENGTH     PIC 9(8) COMP.
   02 CLEAR-TEXT            PIC X(16).
   02 CYPHERED-TEXT-LENGTH  PIC 9(8) COMP.
   02 CYPHERED-TEXT         PIC X(16).
   02 OPTIONAL-DATA-LENGTH  PIC 9(8) COMP.
   02 OPTIONAL-DATA         PIC X(32).

INITIALIZE CSNBSYE-PARAMETERS.

MOVE 1                                  TO RULE-ARRAY-COUNT.
MOVE 'DES     '                         TO RULE-ARRAY.
MOVE 16                                 TO KEY-IDENTIFIER-LENGTH.
MOVE '2DF65FD88EA9E17E3C66950387F91DE2' TO KEY-IDENTIFIER.
MOVE 8                                  TO BLOCK-SIZE
                                           INIT-VECTOR-LENGTH.
MOVE ALL ZEROS                          TO INIT-VECTOR.
MOVE 16                                 TO CHAIN-DATA-LENGTH.
MOVE LOW-VALUES                         TO CHAIN-DATA.
MOVE 16                                 TO CLEAR-TEXT-LENGTH
                                           CYPHERED-TEXT-LENGTH.
MOVE ALL ZEROS                          TO CLEAR-TEXT.

CALL CSNBSYE USING RETURN-CODE,
                   REASON-CODE,           
                   EXIT-DATA-LENGTH,      
                   EXIT-DATA,             
                   RULE-ARRAY-COUNT,      
                   RULE-ARRAY,            
                   KEY-IDENTIFIER-LENGTH, 
                   KEY-IDENTIFIER,        
                   KEY-PARMS-LENGTH,      
                   KEY-PARMS,             
                   BLOCK-SIZE,           
                   INIT-VECTOR-LENGTH,    
                   INIT-VECTOR,           
                   CHAIN-DATA-LENGTH,    
                   CHAIN-DATA,            
                   CLEAR-TEXT-LENGTH,     
                   CLEAR-TEXT,            
                   CYPHERED-TEXT-LENGTH,  
                   CYPHERED-TEXT,         
                   OPTIONAL-DATA-LENGTH,  
                   OPTIONAL-DATA.

使用CSNBECO

77 CSNBECO                  PIC X(7) VALUE 'CSNBECO'.
01 CSNBECO-PARAMETERS.
   02 RETURN-CODE           PIC 9(8) COMP.
   02 REASON-CODE           PIC 9(8) COMP.
   02 EXIT-DATA-LENGTH      PIC 9(8) COMP.
   02 EXIT-DATA             PIC X(32).
   02 CLEAR-KEY             PIC X(32).
   02 CLEAR-TEXT            PIC X(16).
   02 CYPHERED-TEXT         PIC X(16).

INITIALIZE CSNBECO-PARAMETERS.

MOVE '2DF65FD88EA9E17E3C66950387F91DE2' TO CLEAR-KEY.
MOVE ALL ZEROS                          TO CLEAR-TEXT.

CALL CSNBSYE USING RETURN-CODE,
                   REASON-CODE,           
                   EXIT-DATA-LENGTH,      
                   EXIT-DATA,             
                   CLEAR-KEY,          
                   CLEAR-TEXT,            
                   CYPHERED-TEXT.

使用DB2's ENCRYPT_TDES

01 WS.
   02 CLEAR-TEXT            PIC X(16).
   02 CYPHERED-TEXT         PIC X(16).

   MOVE ALL ZEROS TO CLEAR-TEXT.

   EXEC SQL
        SELECT ENCRYPT_TDES(:CLEAR-TEXT, '2DF65FD88EA9E17E3C66950387F91DE2')
        INTO   :CYPHERED-TEXT
        FROM   SYSIBM.SYSDUMMY1
   END-EXEC.

但这些方法都没有返回我预期的结果。我期望的结果与本网站的测试结果相同:http://tripledes.online-domain-tools.com/,其中包含以下数据:

输入类型:文字

输入文字:0000000000000000(HEX)

功能:3DES

模式:CBC

键:2DF65FD88EA9E17E3C66950387F91DE2(HEX)

初始向量:00 00 00 00 00 00 00 00

加密!

加密文本(结果):87 30 e1 ef 98 3d f2 b4(HEX)| 。 0áï=ò'(STRING)

我的问题是:如何使用IBM提供的任何工具在Cobol程序中获得上述结果?

谢谢!

2 个答案:

答案 0 :(得分:5)

您的大多数混淆似乎来自于您将十六进制字符串误认为字节值这一事实。例如。当你传递一个以'2DF65FD88EA9E17E3C66950387F91DE2'X开头的32字节字符串时,你认为你传递了CSNBSYE一个16字节的密钥'F2C4C6F6F5C6C429F8'X - 即你传递的字符的EBCDIC表示。要使用字节值的实际十六进制表示,您必须在文字的结束撇号后追加X

另请注意,在使用ZERO时,PIC X中的'F0'X项会导致LOW-VALUE项结果'00'

另一点是,您似乎将网站的3DES结果与CSNBECO或CSNBSYE的DES结果进行比较 - 但它们是不同的密码,因此应返回不同的结果。

最后但并非最不重要ENCRYPT_TDES:此功能正在使用3DES,但它不接受普通密钥。相反,第二个参数是经过哈希处理以获取最终加密密钥的密码。

因此,在您调查的替代方案中,似乎只有CSNBSYE符合您的要求,但您必须研究其确切的参数格式和用法。

答案 1 :(得分:-1)

我做到了!代码如下:

77 CT-cENC-ROUTINE                  PIC X(7) VALUE 'CSNBSYE'.
05 WS-ENC.                                        
   10 WS-ENC-nRETURN-CODE           PIC 9(8) COMP.
   10 WS-ENC-nREASON-CODE           PIC 9(8) COMP.
   10 WS-ENC-nEXIT-DATA-LENGTH      PIC 9(8) COMP.
   10 WS-ENC-cEXIT-DATA             PIC X(4).     
   10 WS-ENC-nRULE-ARRAY-COUNT      PIC 9(8) COMP.
   10 WS-ENC-RULE-ARRAY.                          
      15 WS-ENC-cRULE-ALGO          PIC X(8).     
   10 WS-ENC-cKEY-IDENT-LENGTH      PIC 9(8) COMP.
   10 WS-ENC-cKEY-IDENT             PIC X(32).    
   10 WS-ENC-nKEY-PARMS-LENGTH      PIC 9(8) COMP.
   10 WS-ENC-nKEY-PARMS             PIC X(64).    
   10 WS-ENC-nBLOCK-SIZE            PIC 9(8) COMP.
   10 WS-ENC-nINIT-VECTOR-LENGTH    PIC 9(8) COMP.
   10 WS-ENC-cINIT-VECTOR           PIC X(16).    
   10 WS-ENC-nCHAIN-DATA-LENGTH     PIC 9(8) COMP.
   10 WS-ENC-cCHAIN-DATA            PIC X(32).    
   10 WS-ENC-nCLEAR-TEXT-LENGTH     PIC 9(8) COMP.
   10 WS-ENC-cCLEAR-TEXT            PIC X(16).    
   10 WS-ENC-nCYPHER-TEXT-LENGTH    PIC 9(8) COMP.
   10 WS-ENC-cCYPHER-TEXT           PIC X(16).    
   10 WS-ENC-nOPTIONAL-DATA-LENGTH  PIC 9(8) COMP.
   10 WS-ENC-cOPTIONAL-DATA         PIC X(32).    

INITIALIZE WS-ENC                                          

MOVE 1                          TO WS-ENC-nRULE-ARRAY-COUNT
MOVE 'DES'                      TO WS-ENC-cRULE-ALGO       

EXEC SQL                                                 
     SELECT VARCHAR_BIT_FORMAT('2DF65FD88EA9E17E3C66950387F91DE2')
     INTO   :WS-ENC-cKEY-IDENT                                
     FROM   SYSIBM.SYSDUMMY1                             
END-EXEC       

MOVE 16                         TO WS-ENC-cKEY-IDENT-LENGTH   
MOVE 8                          TO WS-ENC-nBLOCK-SIZE                                          
                                   WS-ENC-nINIT-VECTOR-LENGTH
MOVE ALL ZEROS                  TO WS-ENC-cINIT-VECTOR       

MOVE LENGTH OF WS-ENC-cCHAIN-DATA                            
                                TO WS-ENC-nCHAIN-DATA-LENGTH 
MOVE LOW-VALUES                 TO WS-ENC-cCHAIN-DATA        

MOVE LENGTH OF WS-ENC-cCLEAR-TEXT                            
                                TO WS-ENC-nCLEAR-TEXT-LENGTH 
                                   WS-ENC-nCYPHER-TEXT-LENGTH
MOVE '0000000000000000'         TO WS-ENC-cCLEAR-TEXT        

CALL CT-cENC-ROUTINE USING WS-ENC-nRETURN-CODE,         
                           WS-ENC-nREASON-CODE,         
                           WS-ENC-nEXIT-DATA-LENGTH,    
                           WS-ENC-cEXIT-DATA,           
                           WS-ENC-nRULE-ARRAY-COUNT,    
                           WS-ENC-RULE-ARRAY,           
                           WS-ENC-cKEY-IDENT-LENGTH,    
                           WS-ENC-cKEY-IDENT,           
                           WS-ENC-nKEY-PARMS-LENGTH,    
                           WS-ENC-nKEY-PARMS,           
                           WS-ENC-nBLOCK-SIZE,          
                           WS-ENC-nINIT-VECTOR-LENGTH,  
                           WS-ENC-cINIT-VECTOR,         
                           WS-ENC-nCHAIN-DATA-LENGTH,   
                           WS-ENC-cCHAIN-DATA,          
                           WS-ENC-nCLEAR-TEXT-LENGTH,   
                           WS-ENC-cCLEAR-TEXT,          
                           WS-ENC-nCYPHER-TEXT-LENGTH,  
                           WS-ENC-cCYPHER-TEXT          
                           WS-ENC-nOPTIONAL-DATA-LENGTH,
                           WS-ENC-cOPTIONAL-DATA    

所以,缺少的是:1)将32字节的十六进制字符串转换为它的16字节字符串表示。 2)链数据大小为32.