2010-09-10 54 views
1

我从需要解密的外部服务获取加密字符串,然后使用BouncyCastle API重新加密。BouncyCastle解密有效,但不是加密?

我已经成功地得到解密工作正常,但加密似乎并没有工作。当我尝试解密由我的加密方法生成的字符串时,我得到一个带有“未知块类型”消息的InvalidCipherTextException

这是我的解密代码,它成功地解密文本从我与交互服务:

string Decrypt(string value) 
{ 
    string Signature = "My_Signature"; 
    RsaKeyParameters keyParams = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(Signature)); 
    IBufferedCipher cipher = CipherUtilities.GetCipher("RSA/NONE/PKCS1Padding"); 
    cipher.Init(false, keyParams); 

    byte[] secretBytes = Convert.FromBase64String(value); 
    byte[] decrypted = cipher.DoFinal(secretBytes); 

    return Encoding.Default.GetString(decrypted); 
} 

这是我的加密方法,它似乎并没有产生一个加密的字符串,我的解密方法可以处理:

string Encrypt(string value) 
{ 
    string Signature = "My_Signature"; 
    RsaKeyParameters keyParams = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(Signature)); 
    IBufferedCipher cipher = CipherUtilities.GetCipher("RSA/NONE/PKCS1Padding"); 
    cipher.Init(true, keyParams); 

    byte[] secretBytes = Encoding.Default.GetBytes(value); 
    byte[] encrypted = cipher.DoFinal(secretBytes); 

    return Convert.ToBase64String(encrypted); 
} 

我不确定我错过了什么使这项工作。有什么明显的我似乎在这里失踪?

回答

1

我假设你Signature -string实际上包含的base64编码公钥的?

我不会给你一个完整的课程上Public-key cryptography,但请记住,你必须使用公共密钥来加密和私钥来解密。看起来你正试图用同一把钥匙来完成这两件事。