小编Syn*_*ps3的帖子

没有'self'的Python调用方法

所以我刚开始用python编程,我不明白"自我"背后的整个推理.我知道它几乎像一个全局变量一样使用,因此数据可以在类中的不同方法之间传递.当你在同一个类中调用另一个方法时,我不明白为什么你需要使用它.如果我已经上课,为什么我要告诉它?

例如,如果我有:为什么我需要self.thing()?

class bla:
    def hello(self):
        self.thing()

    def thing(self):
        print "hello"
Run Code Online (Sandbox Code Playgroud)

python class self

29
推荐指数
3
解决办法
6万
查看次数

C#中的BouncyCastle内存中PGP加密

我一直在尝试使用BouncyCastle库来进行PGP加密/解密.我有一些代码需要修改才能使用流 - 没有文件.

我尝试删除PgpUtilities.WriteFileToLiteralData(),然后使其返回流,但它不起作用(输出流为空).

这里要更清楚的是该方法应该是什么:

public static Stream EncryptFile(MemoryStream inputStream, PgpPublicKey encKey, bool withIntegrityCheck)
Run Code Online (Sandbox Code Playgroud)

这是我需要修改的代码:

private static void EncryptFile(Stream outputStream, string fileName, PgpPublicKey encKey, bool armor, bool withIntegrityCheck)
{

    if (armor)
        outputStream = new ArmoredOutputStream(outputStream);

    try
    {
        MemoryStream bOut = new MemoryStream();
        PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator(
        CompressionAlgorithmTag.Zip);
        PgpUtilities.WriteFileToLiteralData(
        comData.Open(bOut),
        PgpLiteralData.Binary,
        new FileInfo(fileName));
        comData.Close();
        PgpEncryptedDataGenerator cPk = new PgpEncryptedDataGenerator(
        SymmetricKeyAlgorithmTag.Cast5, withIntegrityCheck, new SecureRandom());
        cPk.AddMethod(encKey);
        byte[] bytes = bOut.ToArray();
        Stream cOut = cPk.Open(outputStream, bytes.Length);
        cOut.Write(bytes, 0, bytes.Length);
        cOut.Close();
        if (armor)
            outputStream.Close(); …
Run Code Online (Sandbox Code Playgroud)

c# encryption bouncycastle pgp

8
推荐指数
2
解决办法
9111
查看次数

BouncyCastle C# PublicKey 与 GnuPG 不同

我正在尝试从 GnuPG 使用 BouncyCastle C# 库创建的密钥环获取公共 PGP 密钥。我已经使用以下代码使其半工作。问题是它输出的公钥长度大约是真实公钥的一半,而且最后几个字节也不同。我只是想得到真正的钥匙。

更新:值得注意的是,我生成的密钥环只有一个公钥,但我从 bouncycastle 中得到了两个。我还发现,如果将第二个密钥插入到从末尾开始的前几个字符中,它会生成几乎原始的密钥。只有末尾的几个字符不同。那么为什么有两个钥匙,为什么会发生这种情况呢?我缺少什么?

GnuPG 密钥环不兼容吗?

另请注意,此处显示的代码仅获取最后一个密钥。我现在将每个添加到列表中。

这是我的代码:

public static string ReadKey(string pubkeyFile)
{
    string theKey;
    Stream fs = File.OpenRead(pubkeyFile);

    //
    // Read the public key rings
    //
    PgpPublicKeyRingBundle pubRings = new PgpPublicKeyRingBundle(PgpUtilities.GetDecoderStream(fs));
    fs.Close();

    foreach (PgpPublicKeyRing pgpPub in pubRings.GetKeyRings())
    {
        pgpPub.GetPublicKey();

        foreach (PgpPublicKey pgpKey in pgpPub.GetPublicKeys())
        {
            //AsymmetricKeyParameter pubKey = pgpKey.GetKey();
            //SubjectPublicKeyInfo k = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pubKey);
            //byte[] keyData = k.ToAsn1Object().GetDerEncoded();
            //byte[] keyData = k.GetEncoded();
            byte[] keyData = pgpKey.GetEncoded();
            theKey = Convert.ToBase64String(keyData);
        }
    } …
Run Code Online (Sandbox Code Playgroud)

c# encryption bouncycastle pgp openpgp

3
推荐指数
1
解决办法
3670
查看次数

在C中用strtok拆分字符串

我无法弄清楚为什么第二个while循环没有执行.它有一个访问冲突结果[count] = atoi ...我认为添加strcpy会有所帮助,因为我意识到原来的字符串被修改,但它没有任何区别.另外,我实际上使用的是C++,但大多数源都是在C中需要速度的.

int* split(const char* str, const char* delim)
{
    char* tok;
    int* result;
    int count = 0;

    char* oldstr = (char*)malloc(sizeof(str));
    strcpy(oldstr, str);

    tok = strtok((char*)str, delim);
    while (tok != NULL)
    {
        count++;
        tok = strtok(NULL, delim);
    }

    result = (int*)malloc(sizeof(int) * count);

    count = 0;
    tok = strtok((char*)oldstr, delim);
    while (tok != NULL)
    {
        result[count] = atoi(tok);
        count++;
        tok = strtok(NULL, delim);
    }
    return result;
}
Run Code Online (Sandbox Code Playgroud)

c arrays strtok

0
推荐指数
1
解决办法
767
查看次数

标签 统计

bouncycastle ×2

c# ×2

encryption ×2

pgp ×2

arrays ×1

c ×1

class ×1

openpgp ×1

python ×1

self ×1

strtok ×1