Ruby中没有外部宝石的简单加密 [英] Simple Encryption in Ruby without external gems

查看:108
本文介绍了Ruby中没有外部宝石的简单加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些文本字符串的简单加密。我想创建优惠券代码,使它们看起来很酷,所以随后创建的代码应该看起来非常不同。 (除了看起来很酷,不应该很容易猜到一个代码。)但是我想要能够再次解密它们。所以算法必须是可逆的。

I need a simple encryption for some text strings. I want to create coupon codes and make them look cool so subsequently created code should look very different. (And besides looking cool, it shouldn't be easy to guess a code.) But I want to be able to decrypt them again. So the algorithm must be reversible.

我已经尝试了一些移动位的东西,所以他们看起来很随机。但是后来的两个代码(只有一点不同)当然看起来很相似。

I alread tried some stuff with moving bits around so they look kind of random already. But two subsequent codes (just one bit different) of course look very similar.

有任何建议?我想这样做,而不使用外部宝石。

Any suggestions? I would like to do that without using external gems.

Philip

推荐答案

我想最简单的方法是一个* x + b(mod 2 ^ n)

I guess the simplest way is a * x + b (mod 2^n)

请注意,你需要计算一个2 ^ n的倒数,你可以用Wolfram Alpha使用扩展GCD。

Note that you need to calculate the inverse of a on 2^n, you can to that with Wolfram Alpha using extended GCD.

这篇关于Ruby中没有外部宝石的简单加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆