import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.SecureRandom; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import com.sun.mail.util.BASE64DecoderStream; import com.sun.mail.util.BASE64EncoderStream; public class util { /** * 传入名文和公钥钥对数据进行RSA解密 * <br>返回值:String * <br>@param src * <br>@param pubkey * <br>@return */ public static String rsaEncoding(String src,PublicKey pubkey){ try { Cipher cip = Cipher.getInstance("RSA"); cip.init(cip.ENCRYPT_MODE, pubkey); byte[] by = cip.doFinal(src.getBytes()); return new String(BASE64EncoderStream.encode(by)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (NoSuchPaddingException e) { throw new RuntimeException(e); } catch (InvalidKeyException e) { throw new RuntimeException(e); } catch (IllegalBlockSizeException e) { throw new RuntimeException(e); } catch (BadPaddingException e) { throw new RuntimeException(e); } } /** * 传入RSA密文和私钥对数据进行解密 * <br>返回值:String * <br>@param sec * <br>@param privkey * <br>@return */ public static String rsaDeEncoding(String sec,PrivateKey privkey){ try { Cipher cip = Cipher.getInstance("RSA"); cip.init(cip.DECRYPT_MODE, privkey); byte[] by = BASE64DecoderStream.decode(sec.getBytes()); return new String(cip.doFinal(by)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (NoSuchPaddingException e) { throw new RuntimeException(e); } catch (InvalidKeyException e) { throw new RuntimeException(e); } catch (IllegalBlockSizeException e) { throw new RuntimeException(e); } catch (BadPaddingException e) { throw new RuntimeException(e); } } /** * 传入字符串、密钥,并加密字符串(对称加密加密),支持:DES、AES、DESede(3DES) * <br>返回值:String 密文 * <br>@param src * <br>@param key * <br>@param method(DES、AES、DESede) * <br>@return */ //对称加密加密 public static String doubKeyEncoding(String src,String keysrc,String method) { SecretKey key; try { //生成密钥 KeyGenerator kg = KeyGenerator.getInstance(method); //初始化此密钥生成器。 kg.init(new SecureRandom(keysrc.getBytes("utf-8"))); key = kg.generateKey(); //加密 Cipher ciph = Cipher.getInstance(method); ciph.init(Cipher.ENCRYPT_MODE, key); ciph.update(src.getBytes("utf-8")); //使用64进行编码,一避免出现丢数据情景 byte[] by = BASE64EncoderStream.encode(ciph.doFinal()); return new String(by); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (NoSuchPaddingException e) { throw new RuntimeException(e); } catch (InvalidKeyException e) { throw new RuntimeException(e); } catch (IllegalBlockSizeException e) { throw new RuntimeException(e); } catch (BadPaddingException e) { throw new RuntimeException(e); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } /** * 传入字符串、密钥、加密方式,并解密字符串(对称加密解密密),支持:DES、AES、DESede(3DES) * <br>生成时间:2014年5月2日 下午1:12:13 * <br>返回值:String 密钥原文 * <br>@param sec * <br>@param key * <br>@param method(DES、AES、DESede) * <br>@return */ public static String doubKeyDencoding(String sec,String keysrc,String method) { SecretKey key; try { //生成密钥 KeyGenerator kg = KeyGenerator.getInstance(method); //初始化此密钥生成器。 kg.init(new SecureRandom(keysrc.getBytes("utf-8"))); key = kg.generateKey(); //加密 Cipher ciph = Cipher.getInstance(method); ciph.init(ciph.DECRYPT_MODE, key); //使用64进行解码,一避免出现丢数据情景 byte[] by = BASE64DecoderStream.decode(sec.getBytes()); ciph.update(by); return new String(ciph.doFinal()); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (NoSuchPaddingException e) { throw new RuntimeException(e); } catch (InvalidKeyException e) { throw new RuntimeException(e); } catch (IllegalBlockSizeException e) { throw new RuntimeException(e); } catch (BadPaddingException e) { throw new RuntimeException(e); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } /** * 单向信息加密(信息摘要),支持:md5、md2、SHA(SHA-1,SHA1)、SHA-256、SHA-384、SHA-512, * <br>返回值:String 加密后的密文 * <br>@param src 传入加密字符串(明文) * <br>@param method 指定算法(md5、md2、SHA(SHA-1,SHA1)、SHA-256、SHA-384、SHA-512) * <br>@return */ public static String ecodingPasswd(String src,String method){ try { //信息摘要算法 MessageDigest md5 = MessageDigest.getInstance(method); md5.update(src.getBytes()); byte[] encoding = md5.digest(); //使用64进行编码,一避免出现丢数据情景 return new String(BASE64EncoderStream.encode(encoding)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e+"加密失败!!"); } } }
更多java加密算法分享(rsa解密、对称加密、md5加密)相关文章请关注PHP中文网!

JVM'SperformanceIsCompetitiveWithOtherRuntimes,operingabalanceOfspeed,安全性和生产性。1)JVMUSESJITCOMPILATIONFORDYNAMICOPTIMIZAIZATIONS.2)c提供NativePernativePerformanceButlanceButlactsjvm'ssafetyFeatures.3)

JavaachievesPlatFormIndependencEthroughTheJavavIrtualMachine(JVM),允许CodeTorunonAnyPlatFormWithAjvm.1)codeisscompiledIntobytecode,notmachine-specificodificcode.2)bytecodeisisteredbytheybytheybytheybythejvm,enablingcross-platerssectectectectectross-eenablingcrossectectectectectection.2)

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java实现“一次编写,到处运行”通过编译成字节码并在Java虚拟机(JVM)上运行。1)编写Java代码并编译成字节码。2)字节码在任何安装了JVM的平台上运行。3)使用Java原生接口(JNI)处理平台特定功能。尽管存在挑战,如JVM一致性和平台特定库的使用,但WORA大大提高了开发效率和部署灵活性。

JavaachievesPlatFormIndependencethroughTheJavavIrtualMachine(JVM),允许Codetorunondifferentoperatingsystemsswithoutmodification.thejvmcompilesjavacodeintoplatform-interploplatform-interpectentbybyteentbytybyteentbybytecode,whatittheninternterninterpretsandectectececutesoneonthepecificos,atrafficteyos,Afferctinginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginging

JavaispoperfulduetoitsplatFormitiondence,对象与偏见,RichstandardLibrary,PerformanceCapabilities和StrongsecurityFeatures.1)Platform-dimplighandependectionceallowsenceallowsenceallowsenceallowsencationSapplicationStornanyDevicesupportingJava.2)

Java的顶级功能包括:1)面向对象编程,支持多态性,提升代码的灵活性和可维护性;2)异常处理机制,通过try-catch-finally块提高代码的鲁棒性;3)垃圾回收,简化内存管理;4)泛型,增强类型安全性;5)ambda表达式和函数式编程,使代码更简洁和表达性强;6)丰富的标准库,提供优化过的数据结构和算法。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

禅工作室 13.0.1
功能强大的PHP集成开发环境

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具