- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在下面的代码中,我使用 Bouncy CaSTLe 签署了一条消息:
import org.bouncycastle.cms.CMSProcessableByteArray;
import org.bouncycastle.cms.CMSSignedData;
import org.bouncycastle.cms.CMSSignedDataGenerator;
import org.bouncycastle.cms.CMSTypedData;
import org.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.operator.ContentSigner;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder;
import org.bouncycastle.util.encoders.Base64;
import java.io.FileInputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Security;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.spec.PKCS8EncodedKeySpec;
public class Sign {
public static void main(String[] args) throws Exception {
Security.addProvider(new BouncyCastleProvider());
String certPath = "certPath";
FileInputStream inPublic = new FileInputStream(certPath);
CertificateFactory factory = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) factory.generateCertificate(inPublic);
String keyPrivatePath = "keyPath";
Path path = Paths.get(keyPrivatePath);
Files.readAllBytes(Paths.get(keyPrivatePath));
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(Files.readAllBytes(Paths.get(keyPrivatePath)));
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey privateKey = kf.generatePrivate(spec);
CMSProcessableByteArray msg = new CMSProcessableByteArray("My message".getBytes());
CMSSignedDataGenerator sGen = new CMSSignedDataGenerator();
ContentSigner sha1Signer = new JcaContentSignerBuilder("SHA1withRSA").setProvider("BC").build(privateKey);
sGen.addSignerInfoGenerator(
new JcaSignerInfoGeneratorBuilder(
new JcaDigestCalculatorProviderBuilder().setProvider("BC").build()
).build(sha1Signer, cert)
);
CMSSignedData sd = sGen.generate(msg);
CMSTypedData cmsBytes = new CMSProcessableByteArray(sd.getEncoded());
// How to reconstruct a CMSSignedData from cmsBytes again?
byte[] bytes = (byte[]) cmsBytes.getContent();
CMSSignedData retrieved = new CMSSignedData(bytes);
System.out.println(retrieved.getSignedContent()); // Doesn't work, is null
}
}
我的问题是如何仅使用该对象的 ASN.1 编码的字节数组来检索原始 CMSSignedData
(想要读取原始消息并验证它)。
我问这个问题的原因是我想解密某个加密和签名的消息。我能够解密此消息,但它会生成 ASN.1 编码的字节数组(它确实对应于我的原始消息),但我无法进一步处理此解密的消息。
最佳答案
您可以使用类org.bouncycaSTLe.asn1.cms.ContentInfo
和org.bouncycaSTLe.asn1.ASN1Sequence
:
CMSTypedData cmsBytes = new CMSProcessableByteArray(sd.getEncoded());
byte[] bytes = (byte[]) cmsBytes.getContent();
// reconstruct CMSSignedData from the byte array
ContentInfo ci = ContentInfo.getInstance(ASN1Sequence.fromByteArray(bytes));
CMSSignedData sig = new CMSSignedData(ci);
另请注意,您必须创建一个 CMSSignedData
,并将内容封装在签名中,因此您必须更改此设置:
CMSSignedData sd = sGen.generate(msg);
对此:
CMSSignedData sd = sGen.generate(msg, true);
关于java - 从 Bouncy CaSTLe 中的 ASN.1 编码检索 CMSSignedData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44704289/
我正在实现 hangFire,它是我项目中的作业调度库。 我面临着与此 link 中相同的问题 然而,将 LifestylePerWebRequest() 替换为 HybridPerWebReques
在我的项目中,我需要同时使用 CaSTLe.Windsor 和 Moq dll。 Windsor 要求 CaSTLe.Core 也在项目中被引用。 当我尝试使用 CaSTLe.Core 中的方法时,问
我已将我的解决方案从城堡版本 1.0 更新到版本 3.0,现在我收到此错误: Type Castle.Facilities.FactorySupport.FactorySupportFacility
我的应用程序使用“SignalR”客户端/服务器通信框架。如果您不熟悉它,服务器端应用程序通常包含一个或多个“集线器”类(类似于 asmx 网络服务),每个类都提供可由客户端调用的方法。在启动期间,客
假设我有这样的组件 public class MyComponent { public MyComponent(string name) { } } 我基本上想让提供的构造函数
我想知道是否有一些最佳实践来实现我需要的功能。 我有一个 Web 应用程序,它在启动期间通过反射扫描某些程序集(插件)并针对公共(public)内核注册它们的依赖项。 外部库可能需要相同的依赖项。 例
我想知道是否有一些最佳实践来实现我需要的功能。 我有一个 Web 应用程序,它在启动期间通过反射扫描某些程序集(插件)并针对公共(public)内核注册它们的依赖项。 外部库可能需要相同的依赖项。 例
是否有关于如何使用 CaSTLe Windsor 的自动事务管理的任何简单示例? documentation似乎缺少一些信息。我看到有用于 nHibernate、ActiveRecord 等的工具……
这是一次回答,但从那时起下载站点已经改变(显然)。我根本找不到 CaSTLe.MicroKernel。 2.51 不能再下载,它不在 2.52 中。或 2.53。 最佳答案 没有Castle.Micr
我正在将我们的项目从 .Net 2 升级到 .Net4.5,同时我将尽可能多的引用推送到 NuGet 并确保版本是最新的。 我在运行其中一项测试时遇到问题 测试类: public cl
当我开始使用 Windsor 时,我认为 DI 会很简单。现在它让我越来越困惑。 在我看来,存储库是一个具有单例生命周期的类。我应该有一个 FooRepository 实例来在应用程序的生命周期内将
我有一个接口(interface) ISession,其实例由不同的 session 工厂生成,具体取决于类所属的命名空间。 我的组件注册示例: IWindsorContainer container
我正在关注 example by José F. Romaniello使用 NHibernate 进行 session 管理。这是一篇非常好的文章,但是我在 NHibernate、Windsor 和
我有一个看似简单的用例。有一个 ICsvReader 组件。让我们在这里简单地将它命名为 Reader。我们加载了一组已知的 CSV 文件,其中一些有标题,有些没有。目前有多个阅读器:Reader_S
试图找出这件事的真正原因,但并没有太多乐趣! Type is not resolved for member 'Castle.MicroKernel.Lifestyle.Scoped.CallCont
这是我在 Global.asax 中的代码 WindsorContainer container = new WindsorContainer(); container.Register(Compo
我正在尝试了解如何使用 CaSTLe ActiveRecord 执行自定义查询。 我能够运行返回我的实体的简单查询,但我真正需要的是如下所示的查询(带有自定义字段集): 选择 count(1) 作为
在使用 ArrayResolver 时,如果我注册了一个接口(interface)的多个实现和一个依赖于所述接口(interface)数组的类,我希望数组解析器注入(inject)所有可以成功解析的接
我为服务注册了两个组件: container.Register( Component.For().Named("FirstChoice").ImplementedBy... Compo
有没有人有一些使用城堡 Windsor InstallerFactory 来订购安装程序的示例代码? 似乎无法在文档或其他地方找到它。 干杯 最佳答案 您只能使用 InstallerFactory与
我是一名优秀的程序员,十分优秀!