pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

使用 Java 進行 RSA 加密/解密

RSA Encryption / Decryption using Java(使用 Java 進行 RSA 加密/解密)
本文介紹了使用 Java 進行 RSA 加密/解密的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在做一個簡單的程序來使用 Java 中的 RSA 算法進行加密/解密.我創建一個密碼對象如下:

I am doing a simple program to encrypt/decrypt using RSA algorithm in Java. I create a cipher object as follows:

//Create a Cipher object
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/NoPadding");

我通過調用 encrypt 函數進行加密:

I do the encryption by calling the encrypt function:

String cipher=encrypt(textByte, pair, rsaCipher);
System.out.println("The Encryption using RSA Algorithm : "+cipher);

而解密為:

//Decryption
String plain=decrypt(Base64.decodeBase64(cipher),pair, rsaCipher);
System.out.println("The Decryption using RSA Algorithm : "+plain);

當我顯示輸出時,解密輸出在原文前返回一個長空格:

When I display the output, the decryption output returns a long space before the original text:

但是,當我將用于創建 Cipher 對象的代碼編輯為://創建一個密碼對象密碼rsaCipher = Cipher.getInstance("RSA");

However, when I edit the code for creating the Cipher object to be: //Create a Cipher object Cipher rsaCipher = Cipher.getInstance("RSA");

即刪除操作模式和填充參數,問題得到解決,輸出變為:

i.e, removed the operation mode and padding arguments, the problem get resolved and the output becomes:

問題出在哪里.在第一種情況下(出現空格時),我指定了 NoPadding?為什么空格會出現在解密的消息中?即使我使用了填充,我希望這不應該發生.

Where is the problem. In the first case (when the space appears), I specified NoPadding? Why the spaces appears in the decrypted message ? Even if I used padding, I expect this should not happen.

這是加密和解密方法:

public static String encrypt(byte[] textBytes, KeyPair pair, Cipher rsaCipher) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException
{
    //get the public key
    PublicKey pk=pair.getPublic(); 


    //Initialize the cipher for encryption. Use the public key.
    rsaCipher.init(Cipher.ENCRYPT_MODE, pk);

    //Perform the encryption using doFinal
    byte[] encByte = rsaCipher.doFinal(textBytes);

    // converts to base64 for easier display.
    byte[] base64Cipher = Base64.encodeBase64(encByte);

    return new String(base64Cipher);
}//end encrypt

public static String decrypt(byte[] cipherBytes, KeyPair pair, Cipher rsaCipher) throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException
{
    //get the public key
    PrivateKey pvk=pair.getPrivate(); 

    //Create a Cipher object
    //Cipher rsaCipher = Cipher.getInstance("RSA/ECB/NoPadding");

    //Initialize the cipher for encryption. Use the public key.
    rsaCipher.init(Cipher.DECRYPT_MODE, pvk);

    //Perform the encryption using doFinal
    byte[] decByte = rsaCipher.doFinal(cipherBytes);

    return new String(decByte);

}//end decrypt

推薦答案

你的問題確實出在padding上.安全 RSA 功能需要某種填充,無論是 PKCS#1 1.5 還是實踐中的 OAEP 填充.此外,還需要找到加密明文的開頭和結尾.

Your problem is indeed with the padding. Some kind of padding, either PKCS#1 1.5 or OAEP padding in practice, is required for secure RSA functionality. Furthermore, it is required to find the start and end of the encrypted plain text.

RSA 的模冪運算是使用大整數執行的.然后將這些操作的結果表示為八位字節字符串.這些八位字節字符串基本上是整數的大端、無符號、固定長度表示.這些整數用 00 值字節填充(這在 RSA 標準中稱為 I2OS 原語).所以你看到的是模冪運算的結果,00 填充仍然存在.

The modular exponentiation of RSA is performed using large integers. The results of these operations are then represented as octet strings. These octet strings are basically big endian, unsigned, fixed length representation of an integer. These integers are left padded with 00 valued bytes (this is called the I2OS primitive in the RSA standard). So what you are seeing is the result of the modular exponentiation, with the 00 padding still in place.

長話短說,始終使用填充方案.如今,OAEP 將更可取.與混合加密方案一起使用,或使用更高級別的容器格式,例如 CMS 或 PGP.

Long story short, always use a padding scheme. Nowadays, OAEP would be preferable. Use it together with hybrid encryption scheme, or use a higher level container format such as CMS or PGP.

這篇關于使用 Java 進行 RSA 加密/解密的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 散热器-电子散热器-型材散热器-电源散热片-镇江新区宏图电子散热片厂家 | 深圳市万色印象美业有限公司| 合肥礼品公司-合肥礼品定制-商务礼品定制公司-安徽柏榽商贸有限公司 | 北京模型公司-工业模型-地产模型-施工模型-北京渝峰时代沙盘模型制作公司 | 沙盘模型公司_沙盘模型制作公司_建筑模型公司_工业机械模型制作厂家 | 不锈钢水管-不锈钢燃气管-卫生级不锈钢管件-不锈钢食品级水管-广东双兴新材料集团有限公司 | 继电器模组-IO端子台-plc连接线-省配线模组厂家-世麦德 | Akribis直线电机_直线模组_力矩电机_直线电机平台|雅科贝思Akribis-杭州摩森机电科技有限公司 | KBX-220倾斜开关|KBW-220P/L跑偏开关|拉绳开关|DHJY-I隔爆打滑开关|溜槽堵塞开关|欠速开关|声光报警器-山东卓信有限公司 | 防爆电机_ybx3系列电机_河南省南洋防爆电机有限公司 | 模具ERP_模具管理系统_模具mes_模具进度管理_东莞市精纬软件有限公司 | 英超直播_英超免费在线高清直播_英超视频在线观看无插件-24直播网 | 广州冷却塔维修厂家_冷却塔修理_凉水塔风机电机填料抢修-广东康明节能空调有限公司 | 网站seo优化_seo云优化_搜索引擎seo_启新网络服务中心 | 无锡网站建设_小程序制作_网站设计公司_无锡网络公司_网站制作 | 常州减速机_减速机厂家_常州市减速机厂有限公司 | 南京租车,南京汽车租赁,南京包车,南京会议租车-南京七熹租车 | 万博士范文网-您身边的范文参考网站Vanbs.com | 郑州宣传片拍摄-TVC广告片拍摄-微电影短视频制作-河南优柿文化传媒有限公司 | 华禹护栏|锌钢护栏_阳台护栏_护栏厂家-华禹专注阳台护栏、楼梯栏杆、百叶窗、空调架、基坑护栏、道路护栏等锌钢护栏产品的生产销售。 | 天津次氯酸钠酸钙溶液-天津氢氧化钠厂家-天津市辅仁化工有限公司 | 广州网站建设_小程序开发_番禺网站建设_佛山网站建设_粤联网络 | 我爱古诗词_古诗词名句赏析学习平台 | 紫外荧光硫分析仪-硫含量分析仪-红外光度测定仪-泰州美旭仪器 | 浙江富广阀门有限公司| 全自动五线打端沾锡机,全自动裁线剥皮双头沾锡机,全自动尼龙扎带机-东莞市海文能机械设备有限公司 | 农产品溯源系统_农产品质量安全追溯系统_溯源系统 | 【365公司转让网】公司求购|转让|资质买卖_股权转让交易平台 | 订做不锈钢_不锈钢定做加工厂_不锈钢非标定制-重庆侨峰金属加工厂 | 石英粉,滑石粉厂家,山东滑石粉-莱州市向阳滑石粉有限公司 | 冰晶石|碱性嫩黄闪蒸干燥机-有机垃圾烘干设备-草酸钙盘式干燥机-常州市宝康干燥 | 哲力实业_专注汽车涂料汽车漆研发生产_汽车漆|修补油漆品牌厂家 长沙一级消防工程公司_智能化弱电_机电安装_亮化工程专业施工承包_湖南公共安全工程有限公司 | 真空泵厂家_真空泵机组_水环泵_旋片泵_罗茨泵_耐腐蚀防爆_中德制泵 | 艺术涂料_进口艺术涂料_艺术涂料加盟_艺术涂料十大品牌 -英国蒙太奇艺术涂料 | 铝镁锰板厂家_进口钛锌板_铝镁锰波浪板_铝镁锰墙面板_铝镁锰屋面-杭州军晟金属建筑材料 | 上海租奔驰_上海租商务车_上海租车网-矢昂汽车服务公司 | 厂房出租_厂房出售_产业园区招商_工业地产 - 中工招商网 | 高效节能电机_伺服主轴电机_铜转子电机_交流感应伺服电机_图片_型号_江苏智马科技有限公司 | 耐力板-PC阳光板-PC板-PC耐力板 - 嘉兴赢创实业有限公司 | 油漆辅料厂家_阴阳脚线_艺术漆厂家_内外墙涂料施工_乳胶漆专用防霉腻子粉_轻质粉刷石膏-魔法涂涂 | 领先的大模型技术与应用公司-中关村科金 |