@@ -8,7 +8,6 @@ export const RSA = {
88 . replace ( / - - - - - E N D [ ^ - ] + - - - - - / g, '' )
99 . replace ( / \s + / g, '' ) ;
1010 } ,
11-
1211 // 格式化私钥为标准 PEM 格式
1312 formatPrivateKey : function ( pem ) {
1413 if ( pem . includes ( '-----BEGIN' ) ) {
@@ -300,5 +299,111 @@ export const RSA = {
300299 return false ;
301300 }
302301 }
302+ } ,
303+ } ;
304+
305+
306+ // 跑不通,暂时不用
307+ export const RSA2 = {
308+ // 清理 PEM 格式,提取 base64 内容
309+ cleanPEM : function ( pem ) {
310+ // 移除头部和尾部标记
311+ pem = pem . replace ( / - - - - - B E G I N [ A - Z 0 - 9 ] + - - - - - / g, "" )
312+ . replace ( / - - - - - E N D [ A - Z 0 - 9 ] + - - - - - / g, "" ) ;
313+ // 移除所有空格和换行符
314+ pem = pem . replace ( / \s / g, "" ) ;
315+ return pem ;
316+ } ,
317+ importPrivateKey : function ( pem ) {
318+ const binaryDer = Uint8Array . from ( Buffer . from ( this . cleanPEM ( pem ) , 'base64' ) ) ;
319+
320+ // 导入私钥
321+ const importedKey = crypto . subtle . importKey (
322+ "pkcs8" ,
323+ binaryDer ,
324+ {
325+ name : "RSA-PKCS1-v1_5" ,
326+ hash : "SHA-256"
327+ } ,
328+ false , // 不可导出
329+ [ "decrypt" ]
330+ ) ;
331+ return importedKey ;
332+ } ,
333+ importPublicKey : function ( pem ) {
334+ const binaryDer = Uint8Array . from ( Buffer . from ( this . cleanPEM ( pem ) , 'base64' ) ) ;
335+ const importedKey = crypto . subtle . importKey (
336+ "spki" , // 使用 spki 格式
337+ binaryDer , // DER 格式的公钥
338+ {
339+ name : "RSA-PKCS1-v1_5" ,
340+ hash : "SHA-256" // 指定哈希算法
341+ } ,
342+ false , // 不可导出
343+ [ "encrypt" ] // 公钥通常用于加密和验证签名
344+ ) ;
345+ return importedKey ;
346+ } ,
347+
348+ // 分段加密
349+ encryptMergedData : function ( publicKey , data ) {
350+ // 计算每个段的最大长度
351+ // 对于 RSA-PKCS1-v1_5,加密段的最大长度 = 密钥长度(字节) - 11
352+ const modulusLengthBytes = ( publicKey . algorithm . modulusLength + 7 ) >> 3 ;
353+ //const modulusLengthBytes = 117;
354+ const segmentLength = modulusLengthBytes - 11 ;
355+ // 将数据编码为Uint8Array
356+ const dataBuffer = new TextEncoder ( ) . encode ( data ) ;
357+ if ( dataBuffer . length > segmentLength ) {
358+ const segments = [ ] ;
359+ for ( let i = 0 ; i < dataBuffer . length ; i += segmentLength ) {
360+ const segment = dataBuffer . slice ( i , i + segmentLength ) ;
361+ segments . push ( crypto . subtle . encrypt ( { name : 'RSA-PKCS1-v1_5' } , publicKey , segment ) ) ;
362+ }
363+ return Buffer . concat ( segments . map ( b => Buffer . from ( b ) ) ) . toString ( 'base64' ) ;
364+ }
365+ return Buffer . from ( crypto . subtle . encrypt ( { name : "RSA-PKCS1-v1_5" } , publicKey , dataBuffer ) ) . toString ( 'base64' ) ;
366+ } ,
367+
368+ // 分段解密
369+ decryptMergedData : function ( privateKey , mergedData ) {
370+ const segmentLength = ( privateKey . algorithm . modulusLength + 7 ) >> 3 ; // 每个段的长度
371+ //const segmentLength = 256;
372+ if ( mergedData . length > segmentLength ) {
373+ const segments = [ ] ;
374+ for ( let i = 0 ; i < mergedData . length ; i += segmentLength ) {
375+ const segment = mergedData . slice ( i , i + segmentLength ) ;
376+ segments . push ( Buffer . from ( crypto . subtle . decrypt ( { name : 'RSA-PKCS1-v1_5' } , privateKey , segment ) ) ) ;
377+ }
378+ return Buffer . concat ( segments ) . toString ( 'utf8' ) ;
379+ }
380+ return Buffer . from ( crypto . subtle . decrypt ( { name : "RSA-PKCS1-v1_5" } , privateKey , mergedData ) ) . toString ( 'utf8' ) ;
381+ } ,
382+ decode : function ( data , key ) {
383+ try {
384+ const mergedDataArray = Uint8Array . from ( Buffer . from ( data , 'base64' ) ) ;
385+ const privateKey = this . importPrivateKey ( key ) ;
386+ console . log ( privateKey ) ;
387+ //console.time("RSA");
388+ const decryptedData = this . decryptMergedData ( privateKey , mergedDataArray ) ;
389+ //console.timeEnd("RSA");
390+ return Buffer . from ( decryptedData ) . toString ( ) ;
391+ } catch ( error ) {
392+ console . error ( "解密过程中发生错误:" , error ) ;
393+ throw error ;
394+ }
395+ } ,
396+ encode : function ( plainText , publicKeyPem ) {
397+ try {
398+ const publicKey = this . importPublicKey ( publicKeyPem ) ;
399+ //console.time("RSA加密");
400+ const encryptedData = this . encryptMergedData ( publicKey , plainText ) ;
401+ const encryptedBase64 = Buffer . from ( encryptedData ) . toString ( 'base64' ) ;
402+ //console.timeEnd("RSA加密");
403+ return encryptedBase64 ;
404+ } catch ( error ) {
405+ console . error ( "加密过程中发生错误:" , error ) ;
406+ throw error ;
407+ }
303408 }
304409} ;
0 commit comments