小编scu*_*ker的帖子

C#BouncyCastle - 使用公钥/私钥进行RSA加密

我需要在C#中加密数据,以便将其传递给Java.Java代码属于第三方但我得到了相关的来源,所以我决定,当Java使用Bouncy Castle库时,我将使用C#端口.

解密工作正常.但是,只有当我使用私钥加密而不使用公钥时,解密才有效.使用公钥时,解密失败unknown block type.

显然加密时内部加密RsaEncryptWithPrivate使用公钥,所以我不明白为什么这两种加密方法在功能上不相同:

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.OpenSsl;

public class EncryptionClass
{       
    public string RsaEncryptWithPublic(string clearText
        , string publicKey)
    {
        var bytesToEncrypt = Encoding.UTF8.GetBytes(clearText);

        var encryptEngine = new Pkcs1Encoding(new RsaEngine());

        using (var txtreader = new StringReader(publicKey))
        {
            var keyParameter = (AsymmetricKeyParameter)new PemReader(txtreader).ReadObject();

            encryptEngine.Init(true, keyParameter);
        }

        var encrypted = Convert.ToBase64String(encryptEngine.ProcessBlock(bytesToEncrypt, 0, bytesToEncrypt.Length));
        return encrypted;

    }

    public string RsaEncryptWithPrivate(string clearText
        , string privateKey)
    {
        var bytesToEncrypt = Encoding.UTF8.GetBytes(clearText);

        var encryptEngine = new …
Run Code Online (Sandbox Code Playgroud)

c# encryption cryptography rsa bouncycastle

19
推荐指数
3
解决办法
2万
查看次数

Spring Boot Security:在Windows中有效,但不适用于Docker / Alpine

我有一个庞大的旧式定制CMS,需要移植到Docker。它最初是作为Tomcat 6 / Ubuntu 14中的WAR部署的,但是我(很有耐心)设法使它作为独立的JAR运行(根据公司要求)。该代码(与所有旧项目一样)很丑陋且笨拙。

这在我的Windows机器上运行良好(足够好):

mvn clean install && java -jar target/appname-1.0.12-SNAPSHOT.jar --spring.profiles.active=qa
Run Code Online (Sandbox Code Playgroud)

这样,我便可以运行一个实例,如果我在浏览器中加载该实例,则可以授予我未经授权的权限访问多个页面,并需要其他用户登录。基本上是功能齐全的版本。

我在Alpine上看到的症状(以及在OSX上的简短运行)如下:

  • Spring Boot执行器端点按预期方式工作(在文件夹下/actuator
  • 已明确列入白名单的任何其他端点都给出404。
  • 所有未列入白名单302的端点都按预期方式重定向到登录页面,但是其本身却给出404找不到。

我将发布整个POM(带有删节的部分)和整个扩展的类WebSecurityConfigurerAdapter,即使它们很长-我对此表示歉意,但是可能更清晰的眼光会吸引一些明显的东西。

主应用程序类非常空,并带有@SpringBootApplication和注释。@ComponentScan

扩展WebSecurityConfigurerAdapter的类:

package com.REDACTED.app.config;

import com.REDACTED.app.authentication.AjaxAwareLoginUrlAuthenticationEntryPoint;
import com.REDACTED.app.authentication.CmsPermissionEvaluator;
import com.REDACTED.app.authentication.REDACTEDRedeemAuthenticationProvider;
import com.REDACTED.app.authentication.REDACTEDRetrievalFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.PermissionEvaluator;
import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration
    .WebSecurityConfigurerAdapter;
import org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler;
import org.springframework.security.web.authentication.*;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    public static final String LOGIN = "/login";
    public static …
Run Code Online (Sandbox Code Playgroud)

java spring docker spring-boot

5
推荐指数
0
解决办法
201
查看次数

标签 统计

bouncycastle ×1

c# ×1

cryptography ×1

docker ×1

encryption ×1

java ×1

rsa ×1

spring ×1

spring-boot ×1