PKCS#1 v2.1 java 语言实现参考

来源:互联网 发布:久其通用数据平台下载 编辑:程序博客网 时间:2024/04/29 21:01

    PKCS#1 v2.1 逐渐已经替代 PKCS#1 v1.5成为了应用选择的主要加密/签名方案。近期看了一下这规范。发现新增的OAEP加密、PSS签名网上的资源不是很多。所以弄了这么个工程,一个目的是导读,一个目的是验证对算法的理解,这里共享给大家,希望对大家理解这个规范有所帮助:
    基于BigInteger类用java封装的PKCS#1 v2.1 全算法实现,模块与规范一一对应。包含 I2OSP OS2IP RSAEP RSADP RSASP1 RSAVP1 RSAES-OAEP RSAES-PKCS1_v1_5 RSASSA-PSS RSASSA-PKCS1-v1_5 以及 MGF SourceAlgrithm等规范定义的模块。并在注释中对应文档各个部分。并部分实现了规范附带的TestVect的测试脚本。
    用C的朋友,请参考开源项目openssl对应的源代码。
本实现完全以学习/导读为目的,在性能上没有做太多的考虑和优化。请不要直接使用到实际项目中,建议使用sun自带的Cipher等类。
bug反馈: zoudeqiang1979@tsinghua.org.cn

猛戳版本V1.0

附录:
TestVect类对应于官网上提供的测试脚本,由于录入大数组实在痛苦,所以部分实现。有心人可以帮忙补全。分析打印结果与测试脚本参考数据,可以确认算法正确性。
TestVect.testvect为编译开关,只在测试模式的时候开启,用以用指定数组替换随机数seed和salt,并打印log。为true的时候,请配合test_seed和test_salt使用。正常运行的时候,请改为false.


package com.broadthinking.pkcs.pkcs_1.test;import com.broadthinking.pkcs.pkcs_1.PKCS1Exception;/** * This directory contains test vectors for RSAES-OAEP and RSASSA-PSS as defined * in PKCS #1 v2.1. *  * The files: *  * readme.txt This file. *  * oaep-vect.txt Test vectors for RSAES-OAEP encryption. *  * oaep-int.txt Intermediate values for RSAES-OAEP encryption and RSA decryption * with CRT. Also, DER-encoded RSAPrivateKey and RSAPublicKey types. *  * pss-vect.txt Test vectors for RSASSA-PSS signing. *  * pss-int.txt Intermediate values for RSASSA-PSS signing. *  * @author CaesarZou *  */public class TestVect {public static void printHex(String Message, byte [] M) {printHex(Message, M, 0, M.length);}public static void printHex(String Message, byte [] M, int offset , int length) {System.out.print("# " + Message);System.out.print(": ");for(int i=0;i<length;i++) {if((i%16)==0) {System.out.println();}System.out.print(String.format("%02x ", M[offset+i]));}System.out.println();System.out.println();}public static void main(String [] args) throws PKCS1Exception {OAEP_Int.Test();OAEP_Vect.Test();PSS_Int.Test();PSS_Vect.Test();}public static final boolean test_vec = true;public static byte [] test_seed = null;public static byte [] test_salt = null;}


原创粉丝点击